Back to index

extremetuxracer  0.5beta
translation.cpp
Go to the documentation of this file.
00001 /* 
00002  * ETRacer
00003  * Copyright (C) 2007-2008 The ETRacer Team <www.extremetuxracer.com>
00004  *
00005  * PPRacer 
00006  * Copyright (C) 2004-2005 Volker Stroebel <volker@planetpenguin.de>
00007  *
00008  * This program is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU General Public License
00010  * as published by the Free Software Foundation; either version 2
00011  * of the License, or (at your option) any later version.
00012  * 
00013  * This program is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016  * GNU General Public License for more details.
00017  * 
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program; if not, write to the Free Software
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00021  */
00022 
00023 #include "translation.h"
00024 
00025 /* THIS REQUIRES TO BE GETTEXT COMPATIBLE
00026 #include "etracer.h"
00027 #include "game_config.h"
00028 
00029 
00030 Translation translation;
00031 
00032 Translation::Translation()
00033 {
00034        //nothing here
00035 }
00036 
00037 void
00038 Translation::getLanguages()
00039 {
00040        char buff[BUFF_LEN];
00041 
00042        sprintf(buff, "%s/translations/languages.tcl",
00043                             getparam_data_dir());
00044        
00045        if ( Tcl_EvalFile( tclInterp, buff ) != TCL_OK) {
00046               std::cerr << " error evalating language-settings file " << buff
00047                             << " : " << Tcl_GetStringResult (tclInterp ) << std::endl;
00048        }
00049 }
00050 
00052 void
00053 Translation::load(const char* language)
00054 {
00055        m_translations.clear();     
00056        
00057        char buff[BUFF_LEN];
00058 
00059        sprintf(buff, "%s/translations/%s.tcl", 
00060                             getparam_data_dir(), language);    
00061        if ( Tcl_EvalFile( tclInterp, buff ) != TCL_OK ) {
00062               std::cerr << "error evalating language file " << buff
00063                             << " : " << Tcl_GetStringResult( tclInterp ) << std::endl;
00064        }      
00065 }
00066 
00068 const char*
00069 Translation::getTranslation(const char* string)
00070 {
00071        std::map<std::string, std::string>::iterator it;
00072        
00073        if((it=m_translations.find(string))!=m_translations.end()){
00074               return (*it).second.c_str();
00075        }else{
00076               return string;
00077        }
00078 }
00079 
00081 void 
00082 Translation::setTranslation(const char* string, const char* translation)
00083 {
00084        m_translations[string]=translation;
00085 }
00086 
00087 void
00088 Translation::addLanguage(const char* language, const char* name)
00089 {
00090        language_t lang;
00091        lang.language=language;
00092        lang.name=name;
00093        m_languages.push_back(lang);
00094 }
00095 
00096 static int pp_translate_string_cb ( ClientData cd, Tcl_Interp *ip, 
00097                        int argc, CONST84 char *argv[])
00098 {
00099        if ( argc != 3 ) {
00100         Tcl_AppendResult(ip, argv[0], ": invalid number of arguments\n", 
00101                       "Usage: ", argv[0], " <string> <translated string>",
00102                       (char *)0 );
00103         return TCL_ERROR;
00104     } 
00105        
00106        translation.setTranslation(argv[1],argv[2]);
00107        
00108        return TCL_OK;
00109 }
00110 
00111 static int pp_translate_language_cb ( ClientData cd, Tcl_Interp *ip, 
00112                        int argc, CONST84 char *argv[])
00113 {
00114        if ( argc != 3 ) {
00115         Tcl_AppendResult(ip, argv[0], ": invalid number of arguments\n", 
00116                       "Usage: ", argv[0], " <language> <language-name>",
00117                       (char *)0 );
00118         return TCL_ERROR;
00119     } 
00120        
00121        translation.addLanguage(argv[1],argv[2]);
00122        
00123        return TCL_OK;
00124 }
00125 
00126 void register_translation_callbacks( Tcl_Interp *ip )
00127 {
00128     Tcl_CreateCommand (ip, "pp_translate_string", pp_translate_string_cb,   0,0);
00129     Tcl_CreateCommand (ip, "pp_translate_language", pp_translate_language_cb,   0,0);
00130 }
00131 */