Game Development Reference
In-Depth Information
How to do it...
1.
For each language we want to support, we need to prepare a set of translated strings.
We store these strings in a ile. An example for the English-Russian language pair
would be the Localizer-ru.txt ile:
Hello~ Привет
Good Bye~ Пока
2.
The ~ character is used as a delimiter between the original phrase and its
translations. The original phrase can be used as a key, and it is stored with
its translation in a global std::map container:
std::map<std::string, std::string> g_Translations;
g_Translations["Original phrase"] = "Translation"
3.
Let us suppose we have a locale name in a global variable:
std::string g_LocaleName;
4.
We only need to implement the LocalizeString() function, which uses the
g_Translations map:
std::string LocalizeString( const std::string& Str ) const
{
auto i = g_Translations.find( Str );
return (i != g_Translations.end()) ? i->second : Str;
}
5.
The LoadLocale() routine uses the global g_LocaleName variable and
loads the required translation table skipping the lines without the ~ character:
void LoadLocale()
{
g_Translations.clear();
const std::string FileName( g_LocalePath + "/Localizer-"
+ g_LocaleName + ".txt" );
if ( !g_FS->FileExists( FileName ) ) { return; }
auto Stream = g_FS->CreateReader( FileName );
while ( !Stream->Eof() )
{
std::string L = Stream->ReadLine();
size_t Pos = L.find( "~" );
if ( Pos == std::string::npos ) { continue; }
g_Translations[ L.substr(0, Pos) ]
= L.substr(Pos + 1);
}
}
 
Search WWH ::




Custom Search