[Super-tux-commit] supertux/lib/lisp parser.cpp,1.2,1.3 parser.h,1.1,1.2
Brought to you by:
wkendrick
From: Matze B. <mat...@us...> - 2004-12-02 01:42:08
|
Update of /cvsroot/super-tux/supertux/lib/lisp In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv539/lib/lisp Modified Files: parser.cpp parser.h Log Message: Made lispfiles translatable. This time the translations are stored externally in .po files in the same directory as the lisp files. So you can have translation files for complete level subsets now. Because Ricardo added a hacked solution before and because people already used that, someone has to go over all the maps now, extract the translations and put them into separate files, I'm too lazy to do that now, I just translated some files for testing. Translation-Patches are always welcome ;-) Index: parser.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/lisp/parser.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- parser.cpp 29 Nov 2004 14:46:36 -0000 1.2 +++ parser.cpp 2 Dec 2004 01:41:57 -0000 1.3 @@ -23,21 +23,28 @@ #include <stdexcept> #include <fstream> #include <cassert> +#include <iostream> +#include "app/setup.h" +#include "app/tinygettext.h" #include "parser.h" #include "lisp.h" namespace lisp { -Parser::Parser() - : lexer(0) +Parser::Parser(bool translate) + : lexer(0), dictionary_manager(0), dictionary(0) { + if(translate) { + dictionary_manager = new TinyGetText::DictionaryManager(); + } } Parser::~Parser() { delete lexer; + delete dictionary_manager; } Lisp* @@ -49,6 +56,12 @@ msg << "Parser problem: Couldn't open file '" << filename << "'."; throw std::runtime_error(msg.str()); } + + if(dictionary_manager) { + dictionary_manager->add_directory(SuperTux::FileSystem::dirname(filename)); + dictionary = & (dictionary_manager->get_dictionary()); + } + return parse(in); } @@ -96,6 +109,31 @@ break; } + if(token == Lexer::TOKEN_SYMBOL && + strcmp(lexer->getString(), "_") == 0) { + // evaluate translation function (_ str) in place here + token = lexer->getNextToken(); + if(token != Lexer::TOKEN_STRING) + throw new std::runtime_error("Expected string after '(_'"); + + result = new Lisp(Lisp::TYPE_STRING); + if(dictionary) { + std::string translation = dictionary->translate(lexer->getString()); + std::cout << "Translated '" << lexer->getString() << "' -> '" + << translation << "'\n"; + result->v.string = new char[translation.size()+1]; + memcpy(result->v.string, translation.c_str(), translation.size()+1); + } else { + size_t len = strlen(lexer->getString()) + 1; + result->v.string = new char[len]; + memcpy(result->v.string, lexer->getString(), len); + } + token = lexer->getNextToken(); + if(token != Lexer::TOKEN_CLOSE_PAREN) + throw new std::runtime_error("Expected ')' after '(_ string'"); + break; + } + Lisp* cur = result; do { cur->v.cons.car = read(); Index: parser.h =================================================================== RCS file: /cvsroot/super-tux/supertux/lib/lisp/parser.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- parser.h 28 Nov 2004 14:57:45 -0000 1.1 +++ parser.h 2 Dec 2004 01:41:57 -0000 1.2 @@ -23,6 +23,11 @@ #include <string> #include "lexer.h" +namespace TinyGetText { +class Dictionary; +class DictionaryManager; +} + namespace lisp { @@ -31,7 +36,7 @@ class Parser { public: - Parser(); + Parser(bool translate = true); ~Parser(); Lisp* parse(const std::string& filename); @@ -41,6 +46,8 @@ Lisp* read(); Lexer* lexer; + TinyGetText::DictionaryManager* dictionary_manager; + TinyGetText::Dictionary* dictionary; Lexer::TokenType token; }; |