2010-03-20 13:11:46 +01:00
// ==============================================================
// This file is part of Glest (www.glest.org)
//
2010-04-11 03:25:06 +02:00
// Copyright (C) 2001-2008 Martio 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"
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 {
// =====================================================
// class Lang
// =====================================================
Lang & Lang : : getInstance ( ) {
static Lang lang ;
return lang ;
2010-03-21 06:33:13 +01:00
}
2010-03-20 13:11:46 +01:00
2011-04-05 20:39:47 +02:00
void Lang : : loadStrings ( const string & language ) {
2010-03-20 13:11:46 +01:00
this - > language = language ;
2011-04-05 20:39:47 +02:00
loadStrings ( language , strings , true ) ;
}
void Lang : : loadStrings ( const string & language , Properties & properties , bool fileMustExist ) {
properties . clear ( ) ;
2010-12-09 21:41:11 +01:00
string data_path = getGameReadWritePath ( GameConstants : : path_data_CacheLookupKey ) ;
2011-04-05 20:39:47 +02:00
string languageFile = data_path + " data/lang/ " + language + " .lng " ;
if ( fileMustExist = = false & & fileExists ( languageFile ) = = false ) {
return ;
}
properties . load ( languageFile ) ;
2010-03-20 13:11:46 +01:00
}
void Lang : : loadScenarioStrings ( const string & scenarioDir , const string & scenarioName ) {
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
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 ) ) {
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 ) ) {
scenarioStrings . load ( path ) ;
}
}
}
2011-04-05 20:39:47 +02:00
bool Lang : : hasString ( const string & s , string language ) {
2011-01-14 18:57:37 +01:00
bool hasString = false ;
try {
2011-04-05 20:39:47 +02:00
if ( language ! = " " ) {
if ( otherLanguageStrings . find ( language ) = = otherLanguageStrings . end ( ) ) {
loadStrings ( language , otherLanguageStrings [ language ] , false ) ;
}
string result = otherLanguageStrings [ language ] . getString ( s ) ;
hasString = true ;
}
else {
string result = strings . getString ( s ) ;
hasString = true ;
}
2011-01-14 18:57:37 +01:00
}
catch ( exception & ex ) {
if ( strings . getpath ( ) ! = " " ) {
2011-04-26 18:02:34 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] for language [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) , language . c_str ( ) ) ;
2011-01-14 18:57:37 +01:00
}
}
return hasString ;
}
2011-04-05 20:39:47 +02:00
bool Lang : : isLanguageLocal ( string compareLanguage ) const {
return ( compareLanguage = = language ) ;
}
string Lang : : get ( const string & s , string language ) {
try {
string result = " " ;
if ( language ! = " " ) {
if ( otherLanguageStrings . find ( language ) = = otherLanguageStrings . end ( ) ) {
loadStrings ( language , otherLanguageStrings [ language ] , false ) ;
}
result = otherLanguageStrings [ language ] . getString ( s ) ;
replaceAll ( result , " \\ n " , " \n " ) ;
}
else {
result = strings . getString ( s ) ;
replaceAll ( result , " \\ n " , " \n " ) ;
}
2010-07-03 08:48:24 +02:00
return result ;
2010-03-20 13:11:46 +01:00
}
2010-10-06 22:22:06 +02:00
catch ( exception & ex ) {
if ( strings . getpath ( ) ! = " " ) {
2011-04-26 18:02:34 +02:00
SystemFlags : : OutputDebug ( SystemFlags : : debugError , " In [%s::%s Line: %d] Error [%s] language [%s] \n " , __FILE__ , __FUNCTION__ , __LINE__ , ex . what ( ) , language . c_str ( ) ) ;
2010-10-06 22:22:06 +02:00
}
2010-03-20 13:11:46 +01:00
return " ??? " + s + " ??? " ;
}
}
2010-10-06 22:22:06 +02:00
string Lang : : getScenarioString ( const string & s ) {
2010-03-20 13:11:46 +01:00
try {
return scenarioStrings . getString ( s ) ;
}
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 + " ??? " ;
}
}
} } //end namespace