2010-03-20 13:11:46 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2011-12-14 08:40:48 +01:00
// Copyright (C) 2001-2008 Martiño Figueroa
2010-03-20 13:11:46 +01:00
//
2010-03-21 06:33:13 +01:00
// You can redistribute this code and/or modify it under
// the terms of the GNU General Public License as published
// by the Free Software Foundation; either version 2 of the
2010-03-20 13:11:46 +01:00
// License, or (at your option) any later version
// ==============================================================
# include "lang.h"
# include <stdexcept>
# include "logger.h"
# include "util.h"
# include "platform_util.h"
2010-12-09 21:41:11 +01:00
# include "game_constants.h"
# include "game_util.h"
2011-06-08 09:18:06 +02:00
# include "platform_common.h"
2011-10-25 20:39:11 +02:00
# include "conversion.h"
# include "gl_wrap.h"
# include "core_data.h"
# include "renderer.h"
2011-12-01 00:35:54 +01:00
# include <algorithm>
2012-05-02 22:46:47 +02:00
# include "config.h"
2010-03-20 13:11:46 +01:00
# include "leak_dumper.h"
using namespace std ;
using namespace Shared : : Util ;
using namespace Shared : : Platform ;
namespace Glest { namespace Game {
2011-11-02 18:17:28 +01:00
const char * DEFAULT_LANGUAGE = " english " ;
2011-12-05 01:02:32 +01:00
2010-03-20 13:11:46 +01:00
// =====================================================
// class Lang
// =====================================================
2011-06-08 09:18:06 +02:00
Lang : : Lang ( ) {
language = " " ;
is_utf8_language = false ;
}
Lang & Lang : : getInstance ( ) {
2010-03-20 13:11:46 +01:00
static Lang lang ;
return lang ;
2010-03-21 06:33:13 +01:00
}
2010-03-20 13:11:46 +01:00
2011-12-05 01:02:32 +01:00
string Lang : : getDefaultLanguage ( ) const {
return DEFAULT_LANGUAGE ;
}
2011-11-19 04:33:01 +01:00
void Lang : : loadStrings ( string uselanguage , bool loadFonts ,
2011-11-15 19:38:13 +01:00
bool fallbackToDefault ) {
2012-11-06 05:50:54 +01:00
if ( uselanguage . length ( ) = = 2 | | ( uselanguage . length ( ) = = 5 & & uselanguage [ 2 ] = = ' - ' ) ) {
2011-11-19 04:33:01 +01:00
uselanguage = getLanguageFile ( uselanguage ) ;
}
2011-11-02 18:17:28 +01:00
bool languageChanged = ( uselanguage ! = this - > language ) ;
this - > language = uselanguage ;
2011-11-15 19:38:13 +01:00
loadStrings ( uselanguage , strings , true , fallbackToDefault ) ;
2011-10-25 20:39:11 +02:00
if ( languageChanged = = true ) {
Font : : resetToDefaults ( ) ;
Lang & lang = Lang : : getInstance ( ) ;
if ( lang . hasString ( " FONT_BASE_SIZE " ) ) {
Font : : baseSize = strToInt ( lang . get ( " FONT_BASE_SIZE " ) ) ;
}
if ( lang . hasString ( " FONT_SCALE_SIZE " ) ) {
Font : : scaleFontValue = strToFloat ( lang . get ( " FONT_SCALE_SIZE " ) ) ;
}
if ( lang . hasString ( " FONT_SCALE_CENTERH_FACTOR " ) ) {
Font : : scaleFontValueCenterHFactor = strToFloat ( lang . get ( " FONT_SCALE_CENTERH_FACTOR " ) ) ;
}
if ( lang . hasString ( " FONT_CHARCOUNT " ) ) {
// 256 for English
// 30000 for Chinese
Font : : charCount = strToInt ( lang . get ( " FONT_CHARCOUNT " ) ) ;
}
if ( lang . hasString ( " FONT_TYPENAME " ) ) {
Font : : fontTypeName = lang . get ( " FONT_TYPENAME " ) ;
}
if ( lang . hasString ( " FONT_CHARSET " ) ) {
// Example values:
// DEFAULT_CHARSET (English) = 1
// GB2312_CHARSET (Chinese) = 134
Shared : : Platform : : charSet = strToInt ( lang . get ( " FONT_CHARSET " ) ) ;
}
if ( lang . hasString ( " FONT_MULTIBYTE " ) ) {
Font : : fontIsMultibyte = strToBool ( lang . get ( " FONT_MULTIBYTE " ) ) ;
}
if ( lang . hasString ( " FONT_RIGHTTOLEFT " ) ) {
Font : : fontIsRightToLeft = strToBool ( lang . get ( " FONT_RIGHTTOLEFT " ) ) ;
}
if ( lang . hasString ( " MEGAGLEST_FONT " ) ) {
//setenv("MEGAGLEST_FONT","/usr/share/fonts/truetype/ttf-japanese-gothic.ttf",0); // Japanese
# if defined(WIN32)
string newEnvValue = " MEGAGLEST_FONT= " + lang . get ( " MEGAGLEST_FONT " ) ;
_putenv ( newEnvValue . c_str ( ) ) ;
# else
setenv ( " MEGAGLEST_FONT " , lang . get ( " MEGAGLEST_FONT " ) . c_str ( ) , 0 ) ;
# endif
}
2012-01-05 23:35:05 +01:00
if ( lang . hasString ( " MEGAGLEST_FONT_FAMILY " ) ) {
# if defined(WIN32)
string newEnvValue = " MEGAGLEST_FONT_FAMILY= " + lang . get ( " MEGAGLEST_FONT_FAMILY " ) ;
_putenv ( newEnvValue . c_str ( ) ) ;
# else
setenv ( " MEGAGLEST_FONT_FAMILY " , lang . get ( " MEGAGLEST_FONT_FAMILY " ) . c_str ( ) , 0 ) ;
# endif
}
2011-10-25 20:39:11 +02:00
// if( lang.hasString("FONT_YOFFSET_FACTOR")) {
// FontMetrics::DEFAULT_Y_OFFSET_FACTOR = strToFloat(lang.get("FONT_YOFFSET_FACTOR"));
// }
# if defined(WIN32)
// Win32 overrides for fonts (just in case they must be different)
if ( lang . hasString ( " FONT_BASE_SIZE_WINDOWS " ) ) {
// 256 for English
// 30000 for Chinese
Font : : baseSize = strToInt ( lang . get ( " FONT_BASE_SIZE_WINDOWS " ) ) ;
}
if ( lang . hasString ( " FONT_SCALE_SIZE_WINDOWS " ) ) {
Font : : scaleFontValue = strToFloat ( lang . get ( " FONT_SCALE_SIZE_WINDOWS " ) ) ;
}
if ( lang . hasString ( " FONT_SCALE_CENTERH_FACTOR_WINDOWS " ) ) {
Font : : scaleFontValueCenterHFactor = strToFloat ( lang . get ( " FONT_SCALE_CENTERH_FACTOR_WINDOWS " ) ) ;
}
if ( lang . hasString ( " FONT_HEIGHT_TEXT_WINDOWS " ) ) {
2011-10-25 22:34:13 +02:00
Font : : langHeightText = lang . get ( " FONT_HEIGHT_TEXT_WINDOWS " , Font : : langHeightText . c_str ( ) ) ;
2011-10-25 20:39:11 +02:00
}
if ( lang . hasString ( " FONT_CHARCOUNT_WINDOWS " ) ) {
// 256 for English
// 30000 for Chinese
Font : : charCount = strToInt ( lang . get ( " FONT_CHARCOUNT_WINDOWS " ) ) ;
}
if ( lang . hasString ( " FONT_TYPENAME_WINDOWS " ) ) {
Font : : fontTypeName = lang . get ( " FONT_TYPENAME_WINDOWS " ) ;
}
if ( lang . hasString ( " FONT_CHARSET_WINDOWS " ) ) {
// Example values:
// DEFAULT_CHARSET (English) = 1
// GB2312_CHARSET (Chinese) = 134
Shared : : Platform : : charSet = strToInt ( lang . get ( " FONT_CHARSET_WINDOWS " ) ) ;
}
if ( lang . hasString ( " FONT_MULTIBYTE_WINDOWS " ) ) {
Font : : fontIsMultibyte = strToBool ( lang . get ( " FONT_MULTIBYTE_WINDOWS " ) ) ;
}
if ( lang . hasString ( " FONT_RIGHTTOLEFT_WINDOWS " ) ) {
Font : : fontIsRightToLeft = strToBool ( lang . get ( " FONT_RIGHTTOLEFT_WINDOWS " ) ) ;
}
if ( lang . hasString ( " MEGAGLEST_FONT_WINDOWS " ) ) {
//setenv("MEGAGLEST_FONT","/usr/share/fonts/truetype/ttf-japanese-gothic.ttf",0); // Japanese
string newEnvValue = " MEGAGLEST_FONT= " + lang . get ( " MEGAGLEST_FONT_WINDOWS " ) ;
_putenv ( newEnvValue . c_str ( ) ) ;
}
// if( lang.hasString("FONT_YOFFSET_FACTOR_WINDOWS")) {
// FontMetrics::DEFAULT_Y_OFFSET_FACTOR = strToFloat(lang.get("FONT_YOFFSET_FACTOR_WINDOWS"));
// }
// end win32
# endif
2011-11-15 19:38:13 +01:00
if ( loadFonts = = true ) {
2011-11-01 22:47:34 +01:00
CoreData & coreData = CoreData : : getInstance ( ) ;
coreData . loadFonts ( ) ;
}
2011-10-25 20:39:11 +02:00
}
2011-04-05 20:39:47 +02:00
}
2011-11-19 04:33:01 +01:00
void Lang : : loadStrings ( string uselanguage , Properties & properties , bool fileMustExist ,
2011-11-15 19:38:13 +01:00
bool fallbackToDefault ) {
2011-04-05 20:39:47 +02:00
properties . clear ( ) ;
2010-12-09 21:41:11 +01:00
string data_path = getGameReadWritePath ( GameConstants : : path_data_CacheLookupKey ) ;
2011-11-02 18:17:28 +01:00
//string languageFile = data_path + "data/lang/" + uselanguage + ".lng";
string languageFile = getGameCustomCoreDataPath ( data_path , " data/lang/ " + uselanguage + " .lng " ) ;
2011-04-05 20:39:47 +02:00
if ( fileMustExist = = false & & fileExists ( languageFile ) = = false ) {
return ;
}
2011-11-15 19:38:13 +01:00
else if ( fileExists ( languageFile ) = = false & & fallbackToDefault = = true ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] path = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , languageFile . c_str ( ) ) ;
2012-04-14 23:21:09 +02:00
//throw megaglest_runtime_error("File NOT FOUND, can't open file: [" + languageFile + "]");
2011-11-15 19:38:13 +01:00
printf ( " Language file NOT FOUND, can't open file: [%s] switching to default language: %s \n " , languageFile . c_str ( ) , DEFAULT_LANGUAGE ) ;
languageFile = getGameCustomCoreDataPath ( data_path , " data/lang/ " + string ( DEFAULT_LANGUAGE ) + " .lng " ) ;
}
2011-06-08 09:18:06 +02:00
is_utf8_language = valid_utf8_file ( languageFile . c_str ( ) ) ;
2011-04-05 20:39:47 +02:00
properties . load ( languageFile ) ;
2010-03-20 13:11:46 +01:00
}
2011-06-08 09:18:06 +02:00
bool Lang : : isUTF8Language ( ) const {
return is_utf8_language ;
}
2013-05-21 22:28:42 +02:00
void Lang : : loadScenarioStrings ( string scenarioDir , string scenarioName , bool isTutorial ) {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] scenarioDir = [%s] scenarioName = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , scenarioDir . c_str ( ) , scenarioName . c_str ( ) ) ;
2010-04-11 03:25:06 +02:00
2013-05-21 22:28:42 +02:00
//printf("Loading scenario scenarioDir [%s] scenarioName [%s]\n",scenarioDir.c_str(),scenarioName.c_str());
2012-10-22 07:08:52 +02:00
// First try to find scenario lng file in userdata
Config & config = Config : : getInstance ( ) ;
2013-05-21 22:28:42 +02:00
vector < string > scenarioPaths ;
if ( isTutorial = = false ) {
scenarioPaths = config . getPathListForType ( ptScenarios ) ;
}
else {
scenarioPaths = config . getPathListForType ( ptTutorials ) ;
}
2012-10-22 07:08:52 +02:00
if ( scenarioPaths . size ( ) > 1 ) {
string & scenarioPath = scenarioPaths [ 1 ] ;
endPathWithSlash ( scenarioPath ) ;
string currentPath = scenarioPath ;
endPathWithSlash ( currentPath ) ;
string scenarioFolder = currentPath + scenarioName + " / " ;
string path = scenarioFolder + scenarioName + " _ " + language + " .lng " ;
//try to load the current language first
if ( fileExists ( path ) ) {
scenarioDir = scenarioPath ;
}
}
2011-03-13 09:23:43 +01:00
string currentPath = scenarioDir ;
endPathWithSlash ( currentPath ) ;
string scenarioFolder = currentPath + scenarioName + " / " ;
2010-04-11 03:25:06 +02:00
string path = scenarioFolder + scenarioName + " _ " + language + " .lng " ;
2010-03-20 13:11:46 +01:00
if ( EndsWith ( scenarioDir , " .xml " ) = = true ) {
2010-04-11 03:25:06 +02:00
scenarioFolder = extractDirectoryPathFromFile ( scenarioDir ) ;
path = scenarioFolder + scenarioName + " _ " + language + " .lng " ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] path = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , path . c_str ( ) ) ;
2010-03-20 13:11:46 +01:00
}
2010-03-21 06:33:13 +01:00
2010-03-20 13:11:46 +01:00
scenarioStrings . clear ( ) ;
2010-03-21 06:33:13 +01:00
2010-03-20 13:11:46 +01:00
//try to load the current language first
2010-03-21 06:33:13 +01:00
if ( fileExists ( path ) ) {
2013-05-21 22:28:42 +02:00
//printf("#2 Loading scenario path [%s]\n",path.c_str());
2010-03-20 13:11:46 +01:00
scenarioStrings . load ( path ) ;
}
else {
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] path not found [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , path . c_str ( ) ) ;
2010-03-20 13:11:46 +01:00
//try english otherwise
2010-04-11 03:25:06 +02:00
path = scenarioFolder + scenarioName + " _english.lng " ;
2011-03-28 05:54:23 +02:00
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] path = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , path . c_str ( ) ) ;
2010-04-11 03:25:06 +02:00
2010-03-20 13:11:46 +01:00
if ( fileExists ( path ) ) {
2013-05-21 22:28:42 +02:00
//printf("#3 Loading scenario path [%s]\n",path.c_str());
2010-03-20 13:11:46 +01:00
scenarioStrings . load ( path ) ;
}
}
}
2012-05-02 22:46:47 +02:00
void Lang : : loadTechTreeStrings ( string techTree ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] techTree = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , techTree . c_str ( ) ) ;
string currentPath = " " ;
Config & config = Config : : getInstance ( ) ;
vector < string > techPaths = config . getPathListForType ( ptTechs ) ;
for ( int idx = 0 ; idx < techPaths . size ( ) ; idx + + ) {
string & techPath = techPaths [ idx ] ;
endPathWithSlash ( techPath ) ;
//printf("techPath [%s]\n",techPath.c_str());
if ( folderExists ( techPath + techTree ) = = true ) {
currentPath = techPath ;
endPathWithSlash ( currentPath ) ;
break ;
}
}
string techTreeFolder = currentPath + techTree + " / " ;
string path = techTreeFolder + " lang/ " + techTree + " _ " + language + " .lng " ;
2012-05-03 16:43:28 +02:00
string pathDefault = techTreeFolder + " lang/ " + techTree + " _default.lng " ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] path = [%s] pathDefault = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , path . c_str ( ) , pathDefault . c_str ( ) ) ;
2012-05-02 22:46:47 +02:00
techTreeStrings . clear ( ) ;
2012-05-03 16:43:28 +02:00
techTreeStringsDefault . clear ( ) ;
2012-05-02 22:46:47 +02:00
//try to load the current language first
if ( fileExists ( path ) ) {
techTreeStrings . load ( path ) ;
}
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] path not found [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , path . c_str ( ) ) ;
//try english otherwise
path = techTreeFolder + " lang/ " + techTree + " _english.lng " ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] path = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , path . c_str ( ) ) ;
if ( fileExists ( path ) ) {
techTreeStrings . load ( path ) ;
}
}
2012-05-03 16:43:28 +02:00
if ( fileExists ( pathDefault ) ) {
techTreeStringsDefault . load ( pathDefault ) ;
}
2012-05-02 22:46:47 +02:00
}
2012-05-11 07:49:47 +02:00
void Lang : : loadTilesetStrings ( string tileset ) {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] tileset = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , tileset . c_str ( ) ) ;
string currentPath = " " ;
Config & config = Config : : getInstance ( ) ;
vector < string > tilesetPaths = config . getPathListForType ( ptTilesets ) ;
for ( int idx = 0 ; idx < tilesetPaths . size ( ) ; idx + + ) {
string & tilesetPath = tilesetPaths [ idx ] ;
endPathWithSlash ( tilesetPath ) ;
//printf("tilesetPath [%s]\n",tilesetPath.c_str());
if ( folderExists ( tilesetPath + tileset ) = = true ) {
currentPath = tilesetPath ;
endPathWithSlash ( currentPath ) ;
break ;
}
}
string tilesetFolder = currentPath + tileset + " / " ;
string path = tilesetFolder + " lang/ " + tileset + " _ " + language + " .lng " ;
string pathDefault = tilesetFolder + " lang/ " + tileset + " _default.lng " ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] path = [%s] pathDefault = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , path . c_str ( ) , pathDefault . c_str ( ) ) ;
tilesetStrings . clear ( ) ;
tilesetStringsDefault . clear ( ) ;
//try to load the current language first
if ( fileExists ( path ) ) {
tilesetStrings . load ( path ) ;
}
else {
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] path not found [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , path . c_str ( ) ) ;
//try english otherwise
path = tilesetFolder + " lang/ " + tileset + " _english.lng " ;
if ( SystemFlags : : getSystemSettingType ( SystemFlags : : debugSystem ) . enabled ) SystemFlags : : OutputDebug ( SystemFlags : : debugSystem , " In [%s::%s Line: %d] path = [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , path . c_str ( ) ) ;
if ( fileExists ( path ) ) {
tilesetStrings . load ( path ) ;
}
}
if ( fileExists ( pathDefault ) ) {
tilesetStringsDefault . load ( pathDefault ) ;
}
}
2011-11-02 18:17:28 +01:00
bool Lang : : hasString ( const string & s , string uselanguage , bool fallbackToDefault ) {
bool result = false ;
2011-01-14 18:57:37 +01:00
try {
2011-11-02 18:17:28 +01:00
if ( uselanguage ! = " " ) {
//printf("#a fallbackToDefault = %d [%s] uselanguage [%s] DEFAULT_LANGUAGE [%s] this->language [%s]\n",fallbackToDefault,s.c_str(),uselanguage.c_str(),DEFAULT_LANGUAGE,this->language.c_str());
if ( otherLanguageStrings . find ( uselanguage ) = = otherLanguageStrings . end ( ) ) {
loadStrings ( uselanguage , otherLanguageStrings [ uselanguage ] , false ) ;
2011-04-05 20:39:47 +02:00
}
2012-10-06 09:06:40 +02:00
//string result2 = otherLanguageStrings[uselanguage].getString(s);
otherLanguageStrings [ uselanguage ] . getString ( s ) ;
2011-11-02 18:17:28 +01:00
//printf("#b result2 [%s]\n",result2.c_str());
result = true ;
2011-04-05 20:39:47 +02:00
}
else {
2012-10-06 09:06:40 +02:00
//string result2 = strings.getString(s);
strings . getString ( s ) ;
2011-11-02 18:17:28 +01:00
result = true ;
2011-04-05 20:39:47 +02:00
}
2011-01-14 18:57:37 +01:00
}
catch ( exception & ex ) {
if ( strings . getpath ( ) ! = " " ) {
2011-11-02 18:17:28 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] for uselanguage [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) , uselanguage . c_str ( ) ) ;
}
//printf("#1 fallbackToDefault = %d [%s] uselanguage [%s] DEFAULT_LANGUAGE [%s] this->language [%s]\n",fallbackToDefault,s.c_str(),uselanguage.c_str(),DEFAULT_LANGUAGE,this->language.c_str());
if ( fallbackToDefault = = true & & uselanguage ! = DEFAULT_LANGUAGE & & this - > language ! = DEFAULT_LANGUAGE ) {
result = hasString ( s , DEFAULT_LANGUAGE , false ) ;
}
else {
2011-01-14 18:57:37 +01:00
}
}
2011-11-02 18:17:28 +01:00
return result ;
2011-01-14 18:57:37 +01:00
}
2011-04-05 20:39:47 +02:00
bool Lang : : isLanguageLocal ( string compareLanguage ) const {
return ( compareLanguage = = language ) ;
}
2011-12-03 04:35:36 +01:00
string Lang : : parseResult ( const string & key , const string & value ) {
if ( value ! = " $USE_DEFAULT_LANGUAGE_VALUE " ) {
return value ;
}
string result = Lang : : get ( key , DEFAULT_LANGUAGE ) ;
return result ;
}
2011-11-02 18:17:28 +01:00
string Lang : : get ( const string & s , string uselanguage , bool fallbackToDefault ) {
2011-04-05 20:39:47 +02:00
try {
string result = " " ;
2011-12-03 04:35:36 +01:00
2011-11-02 18:17:28 +01:00
if ( uselanguage ! = " " ) {
if ( otherLanguageStrings . find ( uselanguage ) = = otherLanguageStrings . end ( ) ) {
loadStrings ( uselanguage , otherLanguageStrings [ uselanguage ] , false ) ;
2011-04-05 20:39:47 +02:00
}
2011-11-02 18:17:28 +01:00
result = otherLanguageStrings [ uselanguage ] . getString ( s ) ;
2011-04-05 20:39:47 +02:00
replaceAll ( result , " \\ n " , " \n " ) ;
}
else {
result = strings . getString ( s ) ;
replaceAll ( result , " \\ n " , " \n " ) ;
}
2011-12-03 04:35:36 +01:00
return parseResult ( s , result ) ; ;
2010-03-20 13:11:46 +01:00
}
2010-10-06 22:22:06 +02:00
catch ( exception & ex ) {
if ( strings . getpath ( ) ! = " " ) {
2011-12-02 23:24:47 +01:00
if ( fallbackToDefault = = false | | SystemFlags : : VERBOSE_MODE_ENABLED ) {
if ( GlobalStaticFlags : : getIsNonGraphicalModeEnabled ( ) = = false ) {
2012-07-26 17:39:44 +02:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] uselanguage [%s] text [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) , uselanguage . c_str ( ) , s . c_str ( ) ) ;
2011-12-02 23:24:47 +01:00
}
}
2011-11-02 18:17:28 +01:00
}
//printf("#2 fallbackToDefault = %d [%s] uselanguage [%s] DEFAULT_LANGUAGE [%s] this->language [%s]\n",fallbackToDefault,s.c_str(),uselanguage.c_str(),DEFAULT_LANGUAGE,this->language.c_str());
2012-09-02 21:33:32 +02:00
//if(fallbackToDefault == true && uselanguage != DEFAULT_LANGUAGE && this->language != DEFAULT_LANGUAGE) {
if ( uselanguage ! = DEFAULT_LANGUAGE & & this - > language ! = DEFAULT_LANGUAGE ) {
2011-11-02 18:17:28 +01:00
return get ( s , DEFAULT_LANGUAGE , false ) ;
2010-10-06 22:22:06 +02:00
}
2011-11-02 18:17:28 +01:00
2010-03-20 13:11:46 +01:00
return " ??? " + s + " ??? " ;
}
2012-09-22 22:13:57 +02:00
return " ??? " + s + " ??? " ;
2010-03-20 13:11:46 +01:00
}
2010-10-06 22:22:06 +02:00
string Lang : : getScenarioString ( const string & s ) {
2010-03-20 13:11:46 +01:00
try {
2011-09-16 05:17:49 +02:00
string result = scenarioStrings . getString ( s ) ;
replaceAll ( result , " \\ n " , " \n " ) ;
return result ;
2010-03-20 13:11:46 +01:00
}
2010-10-06 22:22:06 +02:00
catch ( exception & ex ) {
if ( scenarioStrings . getpath ( ) ! = " " ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
}
2010-03-20 13:11:46 +01:00
return " ??? " + s + " ??? " ;
}
}
2013-02-09 18:18:34 +01:00
bool Lang : : hasScenarioString ( const string & s ) {
bool result = false ;
try {
scenarioStrings . getString ( s ) ;
result = true ;
}
catch ( exception & ex ) {
if ( scenarioStrings . getpath ( ) ! = " " ) {
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
}
}
return result ;
}
2012-05-02 22:46:47 +02:00
string Lang : : getTechTreeString ( const string & s , const char * defaultValue ) {
try {
string result = " " ;
if ( techTreeStrings . hasString ( s ) = = true | | defaultValue = = NULL ) {
2012-05-03 16:43:28 +02:00
if ( techTreeStrings . hasString ( s ) = = false & & techTreeStringsDefault . hasString ( s ) = = true ) {
result = techTreeStringsDefault . getString ( s ) ;
}
else {
result = techTreeStrings . getString ( s ) ;
}
}
else if ( techTreeStringsDefault . hasString ( s ) = = true ) {
result = techTreeStringsDefault . getString ( s ) ;
2012-05-02 22:46:47 +02:00
}
else if ( defaultValue ! = NULL ) {
result = defaultValue ;
}
replaceAll ( result , " \\ n " , " \n " ) ;
return result ;
}
catch ( exception & ex ) {
if ( techTreeStrings . getpath ( ) ! = " " ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
}
return " ??? " + s + " ??? " ;
}
2012-09-22 22:13:57 +02:00
return " ??? " + s + " ??? " ;
2012-05-02 22:46:47 +02:00
}
2012-05-11 07:49:47 +02:00
string Lang : : getTilesetString ( const string & s , const char * defaultValue ) {
try {
string result = " " ;
if ( tilesetStrings . hasString ( s ) = = true | | defaultValue = = NULL ) {
if ( tilesetStrings . hasString ( s ) = = false & & tilesetStringsDefault . hasString ( s ) = = true ) {
result = tilesetStringsDefault . getString ( s ) ;
}
else {
result = tilesetStrings . getString ( s ) ;
}
}
else if ( tilesetStringsDefault . hasString ( s ) = = true ) {
result = tilesetStringsDefault . getString ( s ) ;
}
else if ( defaultValue ! = NULL ) {
result = defaultValue ;
}
replaceAll ( result , " \\ n " , " \n " ) ;
return result ;
}
catch ( exception & ex ) {
if ( tilesetStrings . getpath ( ) ! = " " ) {
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) ) ;
}
return " ??? " + s + " ??? " ;
}
2012-09-22 22:13:57 +02:00
return " ??? " + s + " ??? " ;
2012-05-11 07:49:47 +02:00
}
2011-11-19 04:33:01 +01:00
bool Lang : : fileMatchesISO630Code ( string uselanguage , string testLanguageFile ) {
bool result = false ;
Properties stringsTest ;
stringsTest . load ( testLanguageFile ) ;
try {
string iso639 = stringsTest . getString ( " ISO639-1 " ) ;
if ( iso639 = = uselanguage ) {
result = true ;
}
}
2012-10-15 10:09:28 +02:00
//catch(const exception &ex) {
catch ( . . . ) {
2011-11-19 04:33:01 +01:00
}
return result ;
}
string Lang : : getLanguageFile ( string uselanguage ) {
bool foundMatch = false ;
string result = uselanguage ;
string data_path = getGameReadWritePath ( GameConstants : : path_data_CacheLookupKey ) ;
vector < string > langResults ;
string userDataPath = getGameCustomCoreDataPath ( data_path , " " ) ;
findAll ( userDataPath + " data/lang/*.lng " , langResults , true , false ) ;
for ( unsigned int i = 0 ; i < langResults . size ( ) & & foundMatch = = false ; + + i ) {
string testLanguageFile = userDataPath + " data/lang/ " + langResults [ i ] + " .lng " ;
foundMatch = fileMatchesISO630Code ( uselanguage , testLanguageFile ) ;
if ( foundMatch = = true ) {
result = langResults [ i ] ;
}
}
if ( foundMatch = = false ) {
langResults . clear ( ) ;
findAll ( data_path + " data/lang/*.lng " , langResults , true ) ;
for ( unsigned int i = 0 ; i < langResults . size ( ) & & foundMatch = = false ; + + i ) {
string testLanguageFile = data_path + " data/lang/ " + langResults [ i ] + " .lng " ;
foundMatch = fileMatchesISO630Code ( uselanguage , testLanguageFile ) ;
if ( foundMatch = = true ) {
result = langResults [ i ] ;
}
}
}
return result ;
}
2011-12-01 00:35:54 +01:00
string Lang : : getNativeLanguageName ( string uselanguage , string testLanguageFile ) {
string result = uselanguage ;
2012-12-28 02:02:09 +01:00
static map < string , string > cachedNativeLanguageNames ;
if ( cachedNativeLanguageNames . find ( testLanguageFile ) ! = cachedNativeLanguageNames . end ( ) ) {
result = cachedNativeLanguageNames [ testLanguageFile ] ;
2011-12-01 00:35:54 +01:00
}
2012-12-28 02:02:09 +01:00
else {
Properties stringsTest ;
stringsTest . load ( testLanguageFile ) ;
try {
result = stringsTest . getString ( " NativeLanguageName " ) ;
cachedNativeLanguageNames [ testLanguageFile ] = result ;
2012-12-28 07:34:10 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " Caching native language name for [%s] = [%s] \n " , testLanguageFile . c_str ( ) , result . c_str ( ) ) ;
}
catch ( const exception & ex ) {
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " ERROR Caching native language name for [%s] msg: [%s] \n " , testLanguageFile . c_str ( ) , ex . what ( ) ) ;
2012-12-28 02:02:09 +01:00
}
catch ( . . . ) {
2012-12-28 07:34:10 +01:00
if ( SystemFlags : : VERBOSE_MODE_ENABLED ) printf ( " ERROR Caching native language name for [%s] msg: [UNKNOWN] \n " , testLanguageFile . c_str ( ) ) ;
2012-12-28 02:02:09 +01:00
}
2011-12-01 00:35:54 +01:00
}
return result ;
}
pair < string , string > Lang : : getNavtiveNameFromLanguageName ( string langName ) {
pair < string , string > result ;
//printf("looking for language [%s]\n",langName.c_str());
2011-12-01 01:20:14 +01:00
map < string , string > nativeList = Lang : : getDiscoveredLanguageList ( true ) ;
map < string , string > : : iterator iterMap = nativeList . find ( langName ) ;
if ( iterMap ! = nativeList . end ( ) ) {
result = make_pair ( iterMap - > second , iterMap - > first ) ;
2011-12-01 00:35:54 +01:00
}
return result ;
}
2011-12-01 01:20:14 +01:00
map < string , string > Lang : : getDiscoveredLanguageList ( bool searchKeyIsLangName ) {
2011-12-01 00:35:54 +01:00
map < string , string > result ;
string data_path = getGameReadWritePath ( GameConstants : : path_data_CacheLookupKey ) ;
string userDataPath = getGameCustomCoreDataPath ( data_path , " " ) ;
vector < string > langResults ;
findAll ( userDataPath + " data/lang/*.lng " , langResults , true , false ) ;
for ( unsigned int i = 0 ; i < langResults . size ( ) ; + + i ) {
string testLanguage = langResults [ i ] ;
string testLanguageFile = userDataPath + " data/lang/ " + testLanguage + " .lng " ;
string nativeName = getNativeLanguageName ( testLanguage , testLanguageFile ) ;
2011-12-01 01:20:14 +01:00
if ( searchKeyIsLangName = = false ) {
result [ nativeName ] = testLanguage ;
}
else {
result [ testLanguage ] = nativeName ;
}
2011-12-01 00:35:54 +01:00
}
vector < string > langResults2 ;
findAll ( data_path + " data/lang/*.lng " , langResults2 , true ) ;
if ( langResults2 . empty ( ) & & langResults . empty ( ) ) {
2012-04-14 23:21:09 +02:00
throw megaglest_runtime_error ( " There are no lang files " ) ;
2011-12-01 00:35:54 +01:00
}
for ( unsigned int i = 0 ; i < langResults2 . size ( ) ; + + i ) {
string testLanguage = langResults2 [ i ] ;
if ( std : : find ( langResults . begin ( ) , langResults . end ( ) , testLanguage ) = = langResults . end ( ) ) {
langResults . push_back ( testLanguage ) ;
string testLanguageFile = data_path + " data/lang/ " + testLanguage + " .lng " ;
string nativeName = getNativeLanguageName ( testLanguage , testLanguageFile ) ;
2011-12-01 01:20:14 +01:00
if ( searchKeyIsLangName = = false ) {
result [ nativeName ] = testLanguage ;
}
else {
result [ testLanguage ] = nativeName ;
}
2011-12-01 00:35:54 +01:00
}
}
return result ;
}
2010-03-20 13:11:46 +01:00
} } //end namespace