forked from BoyC/GW2TacO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Language.cpp
177 lines (139 loc) · 3.96 KB
/
Language.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "Language.h"
#include "OverlayConfig.h"
Localization* localization = nullptr;
void Localization::ImportFile( const CString& s )
{
CXMLDocument d;
if ( !d.LoadFromFile( s.GetPointer() ) ) return;
ImportLanguage( d );
}
void Localization::ImportLanguage( CXMLDocument& d )
{
if ( !d.GetDocumentNode().GetChildCount( "TacOLanguageData" ) ) return;
CXMLNode root = d.GetDocumentNode().GetChild( "TacOLanguageData" );
CString language;
if ( root.HasAttribute( "language" ) )
language = root.GetAttributeAsString( "language" );
else
{
LOG_ERR( "[GW2TacO] Language data file didn't specify the language." );
return;
}
language.ToLower();
int langIdx = -1;
for ( int x = 0; x < languages.NumItems(); x++ )
{
if ( languages[ x ].name == language )
langIdx = x;
}
if ( langIdx < 0 )
{
Language lang;
lang.name = language;
langIdx = languages.NumItems();
languages += lang;
}
auto& lang = languages[ langIdx ].dict;
int tokenCount = root.GetChildCount( "token" );
for ( int x = 0; x < tokenCount; x++ )
{
auto& token = root.GetChild( "token", x );
if ( !token.HasAttribute( "key" ) || !token.HasAttribute( "value" ) )
continue;
CString key = token.GetAttributeAsString( "key" );
key.ToLower();
CString value = token.GetAttributeAsString( "value" );
lang[ key ] = value;
ProcessStringForUsedGlyphs( value );
}
ProcessStringForUsedGlyphs( language );
}
Localization::Localization()
{
for ( int x = 0; x <= 0x460; x++ )
usedGlyphs.AddUnique( x );
}
void Localization::SetActiveLanguage( const CString& language )
{
auto lang = language;
lang.ToLower();
for ( int x = 0; x < languages.NumItems(); x++ )
{
if ( languages[ x ].name == lang )
{
activeLanguageIdx = x;
SetConfigString( "language", lang );
LOG_NFO( "[GW2TacO] Setting TacO language to %s", language.GetPointer() );
return;
}
}
activeLanguageIdx = 0;
}
CStringArray Localization::GetLanguages()
{
CStringArray langs;
for ( int x = 0; x < languages.NumItems(); x++ )
langs += languages[ x ].name;
for ( int x = 0; x < langs.NumItems(); x++ )
langs[ x ][ 0 ] = toupper( langs[ x ][ 0 ] );
return langs;
}
void Localization::Import()
{
ImportFile( "TacO_Language_en.xml" );
CFileList list;
list.ExpandSearch( "TacO_Language_*.xml", ".", false );
for ( TS32 x = 0; x < list.Files.NumItems(); x++ )
if ( CString::CompareNoCase( list.Files[ x ].FileName, CString( "TacO_Language_en.xml" ) ) != 0 )
{
ImportFile( list.Files[ x ].Path + list.Files[ x ].FileName );
}
if ( HasConfigString( "language" ) )
{
SetActiveLanguage( GetConfigString( "language" ) );
}
}
CString Localization::Localize( const char* token, const CString& fallback )
{
CString tokenString( token );
tokenString.ToLower();
CString rawToken;
if ( fallback.Length() )
rawToken = fallback;
else
rawToken = CString( "[" ) + tokenString + "]";
if ( activeLanguageIdx >= languages.NumItems() || activeLanguageIdx < 0 )
return rawToken;
auto& lang = languages[ activeLanguageIdx ].dict;
if ( !lang.HasKey( tokenString ) )
{
static CArray<CString> dumpedStrings;
if ( dumpedStrings.Find( tokenString ) < 0 )
{
LOG_WARN( "[GW2TacO] Translation for token '%s' is not available in the %s language.", tokenString.GetPointer(), languages[ activeLanguageIdx ].name.GetPointer() );
dumpedStrings += tokenString;
}
return rawToken;
}
return lang[ tokenString ];
}
CString Localization::Localize( const CString& token, const CString& fallback )
{
return Localize( token.GetPointer(), fallback );
}
int Localization::GetActiveLanguageIndex()
{
return activeLanguageIdx;
}
CArray<int>& Localization::GetUsedGlyphs()
{
return usedGlyphs;
}
void Localization::ProcessStringForUsedGlyphs( CString& string )
{
string.DecodeUtf8( [ & ]( TU32 Char )->TBOOL
{
usedGlyphs.AddUnique( Char );
return true;
} );
}