forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslation_manager_impl.cpp
232 lines (213 loc) · 7.25 KB
/
translation_manager_impl.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#if defined(LOCALIZE)
#include <cstring>
#include "cached_options.h"
#include "debug.h"
#include "filesystem.h"
#include "path_info.h"
#include "translations.h"
#include "translation_manager_impl.h"
std::uint32_t TranslationManager::Impl::Hash( const char *str )
{
std::uint32_t hash = 5381U;
while( *str != '\0' ) {
hash = hash * 33 + ( *str++ );
}
return hash;
}
cata::optional<std::pair<std::size_t, std::size_t>> TranslationManager::Impl::LookupString(
const char *query ) const
{
std::uint32_t hash = Hash( query );
auto it = strings.find( hash );
if( it == strings.end() ) {
return cata::nullopt;
}
for( const std::pair<size_t, size_t> &entry : it->second ) {
const std::size_t document = entry.first;
const std::size_t index = entry.second;
if( strcmp( documents[document].GetOriginalString( index ), query ) == 0 ) {
return cata::optional<std::pair<std::size_t, std::size_t>> { entry };
}
}
return cata::nullopt;
}
std::string TranslationManager::Impl::LanguageCodeOfPath( const std::string &path )
{
const std::size_t end = path.rfind( "/LC_MESSAGES" );
if( end == std::string::npos ) {
return std::string();
}
const std::size_t begin = path.rfind( '/', end - 1 ) + 1;
if( begin == std::string::npos ) {
return std::string();
}
return path.substr( begin, end - begin );
}
void TranslationManager::Impl::ScanTranslationDocuments()
{
DebugLog( D_INFO, DC_ALL ) << "[i18n] Scanning core translations from " << locale_dir();
DebugLog( D_INFO, DC_ALL ) << "[i18n] Scanning mod translations from " << PATH_INFO::user_moddir();
std::vector<std::pair<std::string, std::string>> mo_dirs;
for( const auto &dir : get_files_from_path( "LC_MESSAGES",
PATH_INFO::user_moddir(),
true ) ) {
mo_dirs.emplace_back( dir, ".mo" );
}
for( const auto &dir : get_files_from_path( "LC_MESSAGES", locale_dir(),
true ) ) {
mo_dirs.emplace_back( dir, "cataclysm-dda.mo" );
}
for( const auto &entry : mo_dirs ) {
const auto &dir = entry.first;
const auto &pattern = entry.second;
std::vector<std::string> mo_dir_files = get_files_from_path( pattern, dir, false, true );
for( const auto &file : mo_dir_files ) {
const std::string lang = LanguageCodeOfPath( file );
if( mo_files.count( lang ) == 0 ) {
mo_files[lang] = std::vector<std::string>();
}
mo_files[lang].emplace_back( file );
}
}
}
void TranslationManager::Impl::Reset()
{
documents.clear();
strings.clear();
strings.max_load_factor( 1.0f );
}
TranslationManager::Impl::Impl()
{
current_language_code = "en";
}
std::unordered_set<std::string> TranslationManager::Impl::GetAvailableLanguages()
{
if( mo_files.empty() ) {
ScanTranslationDocuments();
}
std::unordered_set<std::string> languages;
for( const auto &kv : mo_files ) {
languages.insert( kv.first );
}
return languages;
}
void TranslationManager::Impl::SetLanguage( const std::string &language_code )
{
if( mo_files.empty() ) {
ScanTranslationDocuments();
}
if( language_code == current_language_code ) {
return;
}
current_language_code = language_code;
if( mo_files.count( current_language_code ) == 0 ) {
Reset();
return;
}
LoadDocuments( mo_files[current_language_code] );
}
std::string TranslationManager::Impl::GetCurrentLanguage() const
{
return current_language_code;
}
void TranslationManager::Impl::LoadDocuments( const std::vector<std::string> &files )
{
Reset();
for( const std::string &file : files ) {
try {
// Skip loading MO files from TEST_DATA mods if not in test mode
if( !test_mode ) {
if( file.find( "TEST_DATA" ) != std::string::npos ) {
continue;
}
}
if( file_exist( file ) ) {
documents.emplace_back( file );
}
} catch( const InvalidTranslationDocumentException &e ) {
DebugLog( D_ERROR, DC_ALL ) << e.what();
}
}
for( std::size_t document = 0; document < documents.size(); document++ ) {
for( std::size_t i = 0; i < documents[document].Count(); i++ ) {
const char *message = documents[document].GetOriginalString( i );
if( message[0] != '\0' ) {
const std::uint32_t hash = Hash( message );
if( strings.count( hash ) == 0 ) {
strings[hash] = std::vector<std::pair<std::size_t, std::size_t>>( 1 );
}
strings[hash].emplace_back( document, i );
}
}
}
}
const char *TranslationManager::Impl::Translate( const std::string &message ) const
{
return Translate( message.c_str() );
}
const char *TranslationManager::Impl::Translate( const char *message ) const
{
cata::optional<std::pair<std::size_t, std::size_t>> entry = LookupString( message );
if( entry ) {
const std::size_t document = entry->first;
const std::size_t string_index = entry->second;
return documents[document].GetTranslatedString( string_index );
}
return message;
}
const char *TranslationManager::Impl::TranslatePlural( const char *singular, const char *plural,
std::size_t n ) const
{
cata::optional<std::pair<std::size_t, std::size_t>> entry = LookupString( singular );
if( entry ) {
const std::size_t document = entry->first;
const std::size_t string_index = entry->second;
return documents[document].GetTranslatedStringPlural( string_index, n );
}
if( n == 1 ) {
return singular;
} else {
return plural;
}
}
std::string TranslationManager::Impl::ConstructContextualQuery( const char *context,
const char *message ) const
{
std::string query;
query.reserve( strlen( context ) + 1 + strlen( message ) );
query.append( context );
query.append( "\004" );
query.append( message );
return query;
}
const char *TranslationManager::Impl::TranslateWithContext( const char *context,
const char *message ) const
{
std::string query = ConstructContextualQuery( context, message );
cata::optional<std::pair<std::size_t, std::size_t>> entry = LookupString( query.c_str() );
if( entry ) {
const std::size_t document = entry->first;
const std::size_t string_index = entry->second;
return documents[document].GetTranslatedString( string_index );
}
return message;
}
const char *TranslationManager::Impl::TranslatePluralWithContext( const char *context,
const char *singular,
const char *plural,
std::size_t n ) const
{
std::string query = ConstructContextualQuery( context, singular );
cata::optional<std::pair<std::size_t, std::size_t>> entry = LookupString( query.c_str() );
if( entry ) {
const std::size_t document = entry->first;
const std::size_t string_index = entry->second;
return documents[document].GetTranslatedStringPlural( string_index, n );
}
if( n == 1 ) {
return singular;
} else {
return plural;
}
}
#endif // defined(LOCALIZE)