forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
translations.cpp
428 lines (383 loc) · 14.5 KB
/
translations.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "translations.h"
#include <algorithm>
#include "cached_options.h"
#include "cata_libintl.h"
#include "catacharset.h"
#include "json.h"
#include "language.h"
#include "rng.h"
#include "string_utils.h"
#include "text_style_check.h"
// int version/generation that is incremented each time language is changed
// used to invalidate translation cache
static int current_language_version = INVALID_LANGUAGE_VERSION + 1;
int detail::get_current_language_version()
{
return current_language_version;
}
void invalidate_translations()
{
// increment version to invalidate translation cache
do {
current_language_version++;
} while( current_language_version == INVALID_LANGUAGE_VERSION );
}
const char *detail::_translate_internal( const char *msg )
{
return l10n_data::get_library().get( msg );
}
const char *vgettext( const char *msgid, const char *msgid_plural, size_t n )
{
return l10n_data::get_library().get_pl( msgid, msgid_plural, n );
}
const char *pgettext( const char *context, const char *msgid )
{
return l10n_data::get_library().get_ctx( context, msgid );
}
const char *vpgettext( const char *const context, const char *const msgid,
const char *const msgid_plural, const size_t n )
{
return l10n_data::get_library().get_ctx_pl( context, msgid, msgid_plural, n );
}
std::string gettext_gendered( const GenderMap &genders, const std::string &msg )
{
const std::vector<std::string> &language_genders = get_language().genders;
std::vector<std::string> chosen_genders;
for( const auto &subject_genders : genders ) {
// default if no match
std::string chosen_gender = language_genders[0];
for( const std::string &gender : subject_genders.second ) {
if( std::find( language_genders.begin(), language_genders.end(), gender ) !=
language_genders.end() ) {
chosen_gender = gender;
break;
}
}
chosen_genders.push_back( subject_genders.first + ":" + chosen_gender );
}
std::string context = join( chosen_genders, " " );
return pgettext( context.c_str(), msg.c_str() );
}
translation::translation( const plural_tag ) : raw_pl( cata::make_value<std::string>() ) {}
translation::translation( const std::string &ctxt, const std::string &raw )
: ctxt( cata::make_value<std::string>( ctxt ) ), raw( raw ), needs_translation( true )
{
}
translation::translation( const std::string &raw )
: raw( raw ), needs_translation( true )
{
}
translation::translation( const std::string &raw, const std::string &raw_pl,
const plural_tag )
: raw( raw ), raw_pl( cata::make_value<std::string>( raw_pl ) ), needs_translation( true )
{
}
translation::translation( const std::string &ctxt, const std::string &raw,
const std::string &raw_pl, const plural_tag )
: ctxt( cata::make_value<std::string>( ctxt ) ),
raw( raw ), raw_pl( cata::make_value<std::string>( raw_pl ) ), needs_translation( true )
{
}
translation::translation( const std::string &str, const no_translation_tag ) : raw( str ) {}
translation translation::to_translation( const std::string &raw )
{
return { raw };
}
translation translation::to_translation( const std::string &ctxt, const std::string &raw )
{
return { ctxt, raw };
}
translation translation::pl_translation( const std::string &raw, const std::string &raw_pl )
{
return { raw, raw_pl, plural_tag() };
}
translation translation::pl_translation( const std::string &ctxt, const std::string &raw,
const std::string &raw_pl )
{
return { ctxt, raw, raw_pl, plural_tag() };
}
translation translation::no_translation( const std::string &str )
{
return { str, no_translation_tag() };
}
void translation::make_plural()
{
if( needs_translation ) {
// if plural form has not been enabled yet
if( !raw_pl ) {
// copy the singular string without appending "s" to preserve the original behavior
raw_pl = cata::make_value<std::string>( raw );
}
} else if( !raw_pl ) {
// just mark plural form as enabled
raw_pl = cata::make_value<std::string>();
}
// reset the cache
cached_language_version = INVALID_LANGUAGE_VERSION;
cached_translation = nullptr;
}
void translation::add_context( const std::string &ctxt )
{
if( this->ctxt ) {
// if context already exists, add to it
std::string &ctxt_this = *this->ctxt;
ctxt_this += "|";
ctxt_this += ctxt;
} else {
this->ctxt = cata::make_value<std::string>( ctxt );
}
// reset the cache
cached_language_version = INVALID_LANGUAGE_VERSION;
cached_translation = nullptr;
}
void translation::deserialize( JsonIn &jsin )
{
// reset the cache
cached_language_version = INVALID_LANGUAGE_VERSION;
cached_translation = nullptr;
#ifndef CATA_IN_TOOL
bool check_style = false;
std::function<void( const std::string &msg, int offset )> log_error;
bool auto_plural = false;
bool is_str_sp = false;
#endif
if( jsin.test_string() ) {
#ifndef CATA_IN_TOOL
if( test_mode ) {
const int origin = jsin.tell();
check_style = true;
log_error = [&jsin, origin]( const std::string & msg, const int offset ) {
const int previous_pos = jsin.tell();
try {
jsin.seek( origin );
jsin.string_error( msg, offset );
} catch( const JsonError &e ) {
debugmsg( "(json-error)\n%s", e.what() );
}
// seek to previous pos (end of string) so subsequent json input
// can continue.
jsin.seek( previous_pos );
};
}
#endif
ctxt = nullptr;
raw = jsin.get_string();
// if plural form is enabled
if( raw_pl ) {
raw_pl = cata::make_value<std::string>( raw + "s" );
auto_plural = true;
}
needs_translation = true;
} else {
JsonObject jsobj = jsin.get_object();
if( jsobj.has_string( "ctxt" ) ) {
ctxt = cata::make_value<std::string>( jsobj.get_string( "ctxt" ) );
} else {
ctxt = nullptr;
}
if( jsobj.has_member( "str_sp" ) ) {
// same singular and plural forms
raw = jsobj.get_string( "str_sp" );
is_str_sp = true;
// if plural form is enabled
if( raw_pl ) {
raw_pl = cata::make_value<std::string>( raw );
} else {
try {
jsobj.throw_error( "str_sp not supported here", "str_sp" );
} catch( const JsonError &e ) {
debugmsg( "(json-error)\n%s", e.what() );
}
}
} else {
raw = jsobj.get_string( "str" );
// if plural form is enabled
if( raw_pl ) {
if( jsobj.has_string( "str_pl" ) ) {
raw_pl = cata::make_value<std::string>( jsobj.get_string( "str_pl" ) );
} else {
raw_pl = cata::make_value<std::string>( raw + "s" );
auto_plural = true;
}
} else if( jsobj.has_string( "str_pl" ) ) {
try {
jsobj.throw_error( "str_pl not supported here", "str_pl" );
} catch( const JsonError &e ) {
debugmsg( "(json-error)\n%s", e.what() );
}
}
}
needs_translation = true;
#ifndef CATA_IN_TOOL
if( test_mode ) {
check_style = !jsobj.has_member( "//NOLINT(cata-text-style)" );
// Copying jsobj to avoid use-after-free
log_error = [jsobj]( const std::string & msg, const int offset ) {
try {
if( jsobj.has_member( "str" ) ) {
jsobj.get_raw( "str" )->string_error( msg, offset );
} else {
jsobj.get_raw( "str_sp" )->string_error( msg, offset );
}
} catch( const JsonError &e ) {
debugmsg( "(json-error)\n%s", e.what() );
}
};
}
#endif
}
#ifndef CATA_IN_TOOL
// Check text style in translatable json strings.
if( test_mode && check_style ) {
if( raw_pl && !auto_plural && *raw_pl == raw + "s" ) {
log_error( "\"str_pl\" is not necessary here since the "
"plural form can be automatically generated.",
0 );
}
if( !is_str_sp && raw_pl && !auto_plural && raw == *raw_pl ) {
log_error( "Please use \"str_sp\" instead of \"str\" and \"str_pl\" "
"for text with identical singular and plural forms",
0 );
}
if( !raw_pl ) {
// Check for punctuation and spacing. Strings with plural forms are
// curently simple names, for which these checks are not necessary.
const auto text_style_callback =
[&log_error]
( const text_style_fix type, const std::string & msg,
const std::u32string::const_iterator & beg, const std::u32string::const_iterator & /*end*/,
const std::u32string::const_iterator & /*at*/,
const std::u32string::const_iterator & from, const std::u32string::const_iterator & to,
const std::string & fix
) {
std::string err;
switch( type ) {
case text_style_fix::removal:
err = msg + "\n"
+ " Suggested fix: remove \"" + utf32_to_utf8( std::u32string( from, to ) ) + "\"\n"
+ " At the following position (marked with caret)";
break;
case text_style_fix::insertion:
err = msg + "\n"
+ " Suggested fix: insert \"" + fix + "\"\n"
+ " At the following position (marked with caret)";
break;
case text_style_fix::replacement:
err = msg + "\n"
+ " Suggested fix: replace \"" + utf32_to_utf8( std::u32string( from, to ) )
+ "\" with \"" + fix + "\"\n"
+ " At the following position (marked with caret)";
break;
}
log_error( err, std::distance( beg, to ) );
};
const std::u32string raw32 = utf8_to_utf32( raw );
text_style_check<std::u32string::const_iterator>( raw32.cbegin(), raw32.cend(),
fix_end_of_string_spaces::yes, escape_unicode::no, text_style_callback );
}
}
#endif
}
std::string translation::translated( const int num ) const
{
if( !needs_translation || raw.empty() ) {
return raw;
}
// Note1: `raw`, `raw_pl` and `ctxt` are effectively immutable for caching purposes:
// in the places where they are changed, cache is explicitly invalidated
// Note2: if `raw_pl` is defined, `num` becomes part of the "cache key"
// otherwise `num` is ignored (for both translation and cache)
if( cached_language_version != current_language_version ||
( raw_pl && cached_num != num ) || !cached_translation ) {
cached_language_version = current_language_version;
cached_num = num;
if( !ctxt ) {
if( !raw_pl ) {
cached_translation = cata::make_value<std::string>( detail::_translate_internal( raw ) );
} else {
cached_translation = cata::make_value<std::string>(
vgettext( raw.c_str(), raw_pl->c_str(), num ) );
}
} else {
if( !raw_pl ) {
cached_translation = cata::make_value<std::string>( pgettext( ctxt->c_str(), raw.c_str() ) );
} else {
cached_translation = cata::make_value<std::string>(
vpgettext( ctxt->c_str(), raw.c_str(), raw_pl->c_str(), num ) );
}
}
}
return *cached_translation;
}
bool translation::empty() const
{
return raw.empty();
}
bool translation::translated_lt( const translation &that ) const
{
return localized_compare( translated(), that.translated() );
}
bool translation::translated_eq( const translation &that ) const
{
return translated() == that.translated();
}
bool translation::translated_ne( const translation &that ) const
{
return !translated_eq( that );
}
bool translation::operator==( const translation &that ) const
{
return value_ptr_equals( ctxt, that.ctxt ) && raw == that.raw &&
value_ptr_equals( raw_pl, that.raw_pl ) &&
needs_translation == that.needs_translation;
}
bool translation::operator!=( const translation &that ) const
{
return !operator==( that );
}
std::pair<bool, int> translation::legacy_hash() const
{
if( needs_translation && !ctxt && !raw_pl ) {
return {true, djb2_hash( reinterpret_cast<const unsigned char *>( raw.c_str() ) )};
}
// Otherwise the translation must have been added after snippets were changed
// to use string ids only, so the translation doesn't have a legacy hash value.
return {false, 0};
}
translation to_translation( const std::string &raw )
{
return translation::to_translation( raw );
}
translation to_translation( const std::string &ctxt, const std::string &raw )
{
return translation::to_translation( ctxt, raw );
}
translation pl_translation( const std::string &raw, const std::string &raw_pl )
{
return translation::pl_translation( raw, raw_pl );
}
translation pl_translation( const std::string &ctxt, const std::string &raw,
const std::string &raw_pl )
{
return translation::pl_translation( ctxt, raw, raw_pl );
}
translation no_translation( const std::string &str )
{
return translation::no_translation( str );
}
std::ostream &operator<<( std::ostream &out, const translation &t )
{
return out << t.translated();
}
std::string operator+( const translation &lhs, const std::string &rhs )
{
return lhs.translated() + rhs;
}
std::string operator+( const std::string &lhs, const translation &rhs )
{
return lhs + rhs.translated();
}
std::string operator+( const translation &lhs, const translation &rhs )
{
return lhs.translated() + rhs.translated();
}