summaryrefslogtreecommitdiff
path: root/core/translation.cpp
blob: 85e207e08dc85ac35ed7e8554f0eb0909516aaf3 (plain)
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
/*************************************************************************/
/*  translation.cpp                                                      */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                    http://www.godotengine.org                         */
/*************************************************************************/
/* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur.                 */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/
#include "translation.h"
#include "globals.h"
#include "io/resource_loader.h"
#include "os/os.h"

static const char* locale_list[]={
"ar", //  Arabic
"ar_AE", //  Arabic (United Arab Emirates)
"ar_BH", //  Arabic (Bahrain)
"ar_DZ", //  Arabic (Algeria)
"ar_EG", //  Arabic (Egypt)
"ar_IQ", //  Arabic (Iraq)
"ar_JO", //  Arabic (Jordan)
"ar_KW", //  Arabic (Kuwait)
"ar_LB", //  Arabic (Lebanon)
"ar_LY", //  Arabic (Libya)
"ar_MA", //  Arabic (Morocco)
"ar_OM", //  Arabic (Oman)
"ar_QA", //  Arabic (Qatar)
"ar_SA", //  Arabic (Saudi Arabia)
"ar_SD", //  Arabic (Sudan)
"ar_SY", //  Arabic (Syria)
"ar_TN", //  Arabic (Tunisia)
"ar_YE", //  Arabic (Yemen)
"be", //  Belarusian
"be_BY", //  Belarusian (Belarus)
"bg", //  Bulgarian
"bg_BG", //  Bulgarian (Bulgaria)
"ca", //  Catalan
"ca_ES", //  Catalan (Spain)
"cs", //  Czech
"cs_CZ", //  Czech (Czech Republic)
"da", //  Danish
"da_DK", //  Danish (Denmark)
"de", //  German
"de_AT", //  German (Austria)
"de_CH", //  German (Switzerland)
"de_DE", //  German (Germany)
"de_LU", //  German (Luxembourg)
"el", //  Greek
"el_CY", //  Greek (Cyprus)
"el_GR", //  Greek (Greece)
"en", //  English
"en_AU", //  English (Australia)
"en_CA", //  English (Canada)
"en_GB", //  English (United Kingdom)
"en_IE", //  English (Ireland)
"en_IN", //  English (India)
"en_MT", //  English (Malta)
"en_NZ", //  English (New Zealand)
"en_PH", //  English (Philippines)
"en_SG", //  English (Singapore)
"en_US", //  English (United States)
"en_ZA", //  English (South Africa)
"es", //  Spanish
"es_AR", //  Spanish (Argentina)
"es_BO", //  Spanish (Bolivia)
"es_CL", //  Spanish (Chile)
"es_CO", //  Spanish (Colombia)
"es_CR", //  Spanish (Costa Rica)
"es_DO", //  Spanish (Dominican Republic)
"es_EC", //  Spanish (Ecuador)
"es_ES", //  Spanish (Spain)
"es_GT", //  Spanish (Guatemala)
"es_HN", //  Spanish (Honduras)
"es_MX", //  Spanish (Mexico)
"es_NI", //  Spanish (Nicaragua)
"es_PA", //  Spanish (Panama)
"es_PE", //  Spanish (Peru)
"es_PR", //  Spanish (Puerto Rico)
"es_PY", //  Spanish (Paraguay)
"es_SV", //  Spanish (El Salvador)
"es_US", //  Spanish (United States)
"es_UY", //  Spanish (Uruguay)
"es_VE", //  Spanish (Venezuela)
"et", //  Estonian
"et_EE", //  Estonian (Estonia)
"fi", //  Finnish
"fi_FI", //  Finnish (Finland)
"fr", //  French
"fr_BE", //  French (Belgium)
"fr_CA", //  French (Canada)
"fr_CH", //  French (Switzerland)
"fr_FR", //  French (France)
"fr_LU", //  French (Luxembourg)
"ga", //  Irish
"ga_IE", //  Irish (Ireland)
"hi", //  Hindi (India)
"hi_IN", //  Hindi (India)
"hr", //  Croatian
"hr_HR", //  Croatian (Croatia)
"hu", //  Hungarian
"hu_HU", //  Hungarian (Hungary)
"in", //  Indonesian
"in_ID", //  Indonesian (Indonesia)
"is", //  Icelandic
"is_IS", //  Icelandic (Iceland)
"it", //  Italian
"it_CH", //  Italian (Switzerland)
"it_IT", //  Italian (Italy)
"iw", //  Hebrew
"iw_IL", //  Hebrew (Israel)
"ja", //  Japanese
"ja_JP", //  Japanese (Japan)
"ja_JP_JP", //  Japanese (Japan,JP)
"ko", //  Korean
"ko_KR", //  Korean (South Korea)
"lt", //  Lithuanian
"lt_LT", //  Lithuanian (Lithuania)
"lv", //  Latvian
"lv_LV", //  Latvian (Latvia)
"mk", //  Macedonian
"mk_MK", //  Macedonian (Macedonia)
"ms", //  Malay
"ms_MY", //  Malay (Malaysia)
"mt", //  Maltese
"mt_MT", //  Maltese (Malta)
"nl", //  Dutch
"nl_BE", //  Dutch (Belgium)
"nl_NL", //  Dutch (Netherlands)
"no", //  Norwegian
"no_NO", //  Norwegian (Norway)
"no_NO_NY", //  Norwegian (Norway,Nynorsk)
"pl", //  Polish
"pl_PL", //  Polish (Poland)
"pt", //  Portuguese
"pt_BR", //  Portuguese (Brazil)
"pt_PT", //  Portuguese (Portugal)
"ro", //  Romanian
"ro_RO", //  Romanian (Romania)
"ru", //  Russian
"ru_RU", //  Russian (Russia)
"sk", //  Slovak
"sk_SK", //  Slovak (Slovakia)
"sl", //  Slovenian
"sl_SI", //  Slovenian (Slovenia)
"sq", //  Albanian
"sq_AL", //  Albanian (Albania)
"sr", //  Serbian
"sr_BA", //  Serbian (Bosnia and Herzegovina)
"sr_CS", //  Serbian (Serbia and Montenegro)
"sr_ME", //  Serbian (Montenegro)
"sr_RS", //  Serbian (Serbia)
"sv", //  Swedish
"sv_SE", //  Swedish (Sweden)
"th", //  Thai
"th_TH", //  Thai (Thailand)
"th_TH_TH", //  Thai (Thailand,TH)
"tr", //  Turkish
"tr_TR", //  Turkish (Turkey)
"uk", //  Ukrainian
"uk_UA", //  Ukrainian (Ukraine)
"vi", //  Vietnamese
"vi_VN", //  Vietnamese (Vietnam)
"zh", //  Chinese
"zh_CN", //  Chinese (China)
"zh_HK", //  Chinese (Hong Kong)
"zh_SG", //  Chinese (Singapore)
"zh_TW", //  Chinese (Taiwan)
0
};

static const char* locale_names[]={
"Arabic",
"Arabic (United Arab Emirates)",
"Arabic (Bahrain)",
"Arabic (Algeria)",
"Arabic (Egypt)",
"Arabic (Iraq)",
"Arabic (Jordan)",
"Arabic (Kuwait)",
"Arabic (Lebanon)",
"Arabic (Libya)",
"Arabic (Morocco)",
"Arabic (Oman)",
"Arabic (Qatar)",
"Arabic (Saudi Arabia)",
"Arabic (Sudan)",
"Arabic (Syria)",
"Arabic (Tunisia)",
"Arabic (Yemen)",
"Belarusian",
"Belarusian (Belarus)",
"Bulgarian",
"Bulgarian (Bulgaria)",
"Catalan",
"Catalan (Spain)",
"Czech",
"Czech (Czech Republic)",
"Danish",
"Danish (Denmark)",
"German",
"German (Austria)",
"German (Switzerland)",
"German (Germany)",
"German (Luxembourg)",
"Greek",
"Greek (Cyprus)",
"Greek (Greece)",
"English",
"English (Australia)",
"English (Canada)",
"English (United Kingdom)",
"English (Ireland)",
"English (India)",
"English (Malta)",
"English (New Zealand)",
"English (Philippines)",
"English (Singapore)",
"English (United States)",
"English (South Africa)",
"Spanish",
"Spanish (Argentina)",
"Spanish (Bolivia)",
"Spanish (Chile)",
"Spanish (Colombia)",
"Spanish (Costa Rica)",
"Spanish (Dominican Republic)",
"Spanish (Ecuador)",
"Spanish (Spain)",
"Spanish (Guatemala)",
"Spanish (Honduras)",
"Spanish (Mexico)",
"Spanish (Nicaragua)",
"Spanish (Panama)",
"Spanish (Peru)",
"Spanish (Puerto Rico)",
"Spanish (Paraguay)",
"Spanish (El Salvador)",
"Spanish (United States)",
"Spanish (Uruguay)",
"Spanish (Venezuela)",
"Estonian",
"Estonian (Estonia)",
"Finnish",
"Finnish (Finland)",
"French",
"French (Belgium)",
"French (Canada)",
"French (Switzerland)",
"French (France)",
"French (Luxembourg)",
"Irish",
"Irish (Ireland)",
"Hindi (India)",
"Hindi (India)",
"Croatian",
"Croatian (Croatia)",
"Hungarian",
"Hungarian (Hungary)",
"Indonesian",
"Indonesian (Indonesia)",
"Icelandic",
"Icelandic (Iceland)",
"Italian",
"Italian (Switzerland)",
"Italian (Italy)",
"Hebrew",
"Hebrew (Israel)",
"Japanese",
"Japanese (Japan)",
"Japanese (Japan JP)",
"Korean",
"Korean (South Korea)",
"Lithuanian",
"Lithuanian (Lithuania)",
"Latvian",
"Latvian (Latvia)",
"Macedonian",
"Macedonian (Macedonia)",
"Malay",
"Malay (Malaysia)",
"Maltese",
"Maltese (Malta)",
"Dutch",
"Dutch (Belgium)",
"Dutch (Netherlands)",
"Norwegian",
"Norwegian (Norway)",
"Norwegian (Norway Nynorsk)",
"Polish",
"Polish (Poland)",
"Portuguese",
"Portuguese (Brazil)",
"Portuguese (Portugal)",
"Romanian",
"Romanian (Romania)",
"Russian",
"Russian (Russia)",
"Slovak",
"Slovak (Slovakia)",
"Slovenian",
"Slovenian (Slovenia)",
"Albanian",
"Albanian (Albania)",
"Serbian",
"Serbian (Bosnia and Herzegovina)",
"Serbian (Serbia and Montenegro)",
"Serbian (Montenegro)",
"Serbian (Serbia)",
"Swedish",
"Swedish (Sweden)",
"Thai",
"Thai (Thailand)",
"Thai (Thailand TH)",
"Turkish",
"Turkish (Turkey)",
"Ukrainian",
"Ukrainian (Ukraine)",
"Vietnamese",
"Vietnamese (Vietnam)",
"Chinese",
"Chinese (China)",
"Chinese (Hong Kong)",
"Chinese (Singapore)",
"Chinese (Taiwan)",
0
};


Vector<String> TranslationServer::get_all_locales() {

	Vector<String> locales;

	const char **ptr=locale_list;

	while (*ptr) {
		locales.push_back(*ptr);
		ptr++;
	}

	return locales;

}

Vector<String> TranslationServer::get_all_locale_names(){

	Vector<String> locales;

	const char **ptr=locale_names;

	while (*ptr) {
		locales.push_back(*ptr);
		ptr++;
	}

	return locales;

}


static bool is_valid_locale(const String& p_locale) {

	const char **ptr=locale_list;

	while (*ptr) {
		if (p_locale==*ptr)
			return true;
		ptr++;
	}

	return false;
}

DVector<String> Translation::_get_messages() const {

	DVector<String> msgs;
	msgs.resize(translation_map.size()*2);
	int idx=0;
	for (const Map<StringName, StringName>::Element *E=translation_map.front();E;E=E->next()) {

		msgs.set(idx+0,E->key());
		msgs.set(idx+1,E->get());
		idx+=2;
	}

	return msgs;
}

DVector<String> Translation::_get_message_list() const {

	DVector<String> msgs;
	msgs.resize(translation_map.size());
	int idx=0;
	for (const Map<StringName, StringName>::Element *E=translation_map.front();E;E=E->next()) {

		msgs.set(idx,E->key());
		idx+=1;
	}

	return msgs;

}

void Translation::_set_messages(const DVector<String>& p_messages){

	int msg_count=p_messages.size();
	ERR_FAIL_COND(msg_count%2);

	DVector<String>::Read r = p_messages.read();

	for(int i=0;i<msg_count;i+=2) {

		add_message( r[i+0], r[i+1] );
	}

}


void Translation::set_locale(const String& p_locale) {

	ERR_EXPLAIN("Invalid Locale: "+p_locale);
	ERR_FAIL_COND(!is_valid_locale(p_locale));
	locale=p_locale;
}

void Translation::add_message( const StringName& p_src_text, const StringName& p_xlated_text ) {

	translation_map[p_src_text]=p_xlated_text;

}
StringName Translation::get_message(const StringName& p_src_text) const {

	const Map<StringName, StringName>::Element *E=translation_map.find(p_src_text);
	if (!E)
		return StringName();

	return E->get();
}

void Translation::erase_message(const StringName& p_src_text) {

	translation_map.erase(p_src_text);
}

void Translation::get_message_list(List<StringName> *r_messages) const {

	for (const Map<StringName, StringName>::Element *E=translation_map.front();E;E=E->next()) {

		r_messages->push_back(E->key());
	}

}

int Translation::get_message_count() const {

	return translation_map.size();
};


void Translation::_bind_methods() {

	ObjectTypeDB::bind_method(_MD("set_locale","locale"),&Translation::set_locale);
	ObjectTypeDB::bind_method(_MD("get_locale"),&Translation::get_locale);
	ObjectTypeDB::bind_method(_MD("add_message","src_message","xlated_message"),&Translation::add_message);
	ObjectTypeDB::bind_method(_MD("get_message","src_message"),&Translation::get_message);
	ObjectTypeDB::bind_method(_MD("erase_message","src_message"),&Translation::erase_message);
	ObjectTypeDB::bind_method(_MD("get_message_list"),&Translation::_get_message_list);
	ObjectTypeDB::bind_method(_MD("get_message_count"),&Translation::get_message_count);
	ObjectTypeDB::bind_method(_MD("_set_messages"),&Translation::_set_messages);
	ObjectTypeDB::bind_method(_MD("_get_messages"),&Translation::_get_messages);

	ADD_PROPERTY( PropertyInfo(Variant::STRING_ARRAY,"messages",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR), _SCS("_set_messages"), _SCS("_get_messages") );
	ADD_PROPERTY( PropertyInfo(Variant::STRING,"locale"), _SCS("set_locale"), _SCS("get_locale") );
}

Translation::Translation() {

	locale="en";
}



///////////////////////////////////////////////


void TranslationServer::set_locale(const String& p_locale) {

	ERR_EXPLAIN("Invalid Locale: "+p_locale);
	ERR_FAIL_COND(!is_valid_locale(p_locale));
	locale=p_locale;
}

String TranslationServer::get_locale() const {

	return locale;

}

void TranslationServer::add_translation(const Ref<Translation> &p_translation) {

	translations.insert(p_translation);

}
void TranslationServer::remove_translation(const Ref<Translation> &p_translation) {

	translations.erase(p_translation);
}

void TranslationServer::clear() {

	translations.clear();
};

StringName TranslationServer::translate(const StringName& p_message) const {

	//translate using locale

	if (!enabled)
		return p_message;

	StringName res;
	bool near_match=false;
	const CharType *lptr=&locale[0];


	for (const Set< Ref<Translation> >::Element *E=translations.front();E;E=E->next()) {

		const Ref<Translation>& t = E->get();
		String l = t->get_locale();
		if (lptr[0]!=l[0] || lptr[1]!=l[1])
			continue; // locale not match

		//near match
		bool match = (l!=locale);

		if (near_match && !match)
			continue; //only near-match once

		StringName r=t->get_message(p_message);


		if (!r)
			continue;

		res=r;

		if (match)
			break;
		else
			near_match=true;

	}

	if (!res) {
		//try again with fallback
		if (fallback.length()>=2) {

			const CharType *fptr=&fallback[0];
			bool near_match=false;
			for (const Set< Ref<Translation> >::Element *E=translations.front();E;E=E->next()) {

				const Ref<Translation>& t = E->get();
				String l = t->get_locale();
				if (fptr[0]!=l[0] || fptr[1]!=l[1])
					continue; // locale not match

				//near match
				bool match = (l!=fallback);

				if (near_match && !match)
					continue; //only near-match once

				StringName r=t->get_message(p_message);

				if (!r)
					continue;

				res=r;

				if (match)
					break;
				else
					near_match=true;

			}
		}
	}


	if (!res)
		return p_message;

	return res;
}

TranslationServer *TranslationServer::singleton=NULL;

bool TranslationServer::_load_translations(const String& p_from) {

	if (Globals::get_singleton()->has(p_from)) {
		DVector<String> translations=Globals::get_singleton()->get(p_from);

		int tcount=translations.size();

		if (tcount) {
			DVector<String>::Read r = translations.read();

			for(int i=0;i<tcount;i++) {

				//print_line( "Loading translation from " + r[i] );
				Ref<Translation> tr = ResourceLoader::load(r[i]);
				if (tr.is_valid())
					add_translation(tr);
			}
		}
		return true;
	}

	return false;
}

void TranslationServer::setup() {

	String test = GLOBAL_DEF("locale/test","");
	test=test.strip_edges();
	if (test!="")
		set_locale( test );
	else
		set_locale( OS::get_singleton()->get_locale() );
	fallback = GLOBAL_DEF("locale/fallback","en");
#ifdef TOOLS_ENABLED

	{
		String options="";
		int idx=0;
		while(locale_list[idx]) {
			if (idx>0)
				options+=", ";
			options+=locale_list[idx];
			idx++;
		}
		Globals::get_singleton()->set_custom_property_info("locale/fallback",PropertyInfo(Variant::STRING,"locale/fallback",PROPERTY_HINT_ENUM,options));
	}
#endif
	//load translations

}

void TranslationServer::set_tool_translation(const Ref<Translation>& p_translation) {
	tool_translation=p_translation;
}

StringName TranslationServer::tool_translate(const StringName& p_message) const {

	if (tool_translation.is_valid()) {
		StringName r = tool_translation->tr(p_message);
		if (r)
			return r;
	}

	return p_message;
}


void TranslationServer::_bind_methods() {

	ObjectTypeDB::bind_method(_MD("set_locale","locale"),&TranslationServer::set_locale);
	ObjectTypeDB::bind_method(_MD("get_locale"),&TranslationServer::get_locale);

	ObjectTypeDB::bind_method(_MD("translate","message"),&TranslationServer::translate);

	ObjectTypeDB::bind_method(_MD("add_translation","translation:Translation"),&TranslationServer::add_translation);
	ObjectTypeDB::bind_method(_MD("remove_translation","translation:Translation"),&TranslationServer::remove_translation);

	ObjectTypeDB::bind_method(_MD("clear"),&TranslationServer::clear);

}

void TranslationServer::load_translations() {

	String locale = get_locale();
	bool found = _load_translations("locale/translations"); //all

	if (_load_translations("locale/translations_"+locale.substr(0,2)))
		found=true;
	if ( locale.substr(0,2) != locale ) {
		if (_load_translations("locale/translations_"+locale))
			found=true;
	}


}

TranslationServer::TranslationServer() {


	singleton=this;
	locale="en";
	enabled=true;

}