Skip to content

Internationalization

kanasimi edited this page Sep 16, 2017 · 8 revisions

i18n (Internationalization) / l10n (Localization)

usage

//	define gettext() user domain resource location.
//	gettext() will auto load (CeL.env.domain_location + language + '.js').
//	e.g., resource/cmn-Hant-TW.js, resource/ja-JP.js
CeL.env.domain_location = 'resource/';
//	declaration for gettext()
var _;

including

CeL.run('application.locale', function() {
	// alias for CeL.gettext, then we can use _('message').
	_ = CeL.gettext;
});

System message test

CeL.gettext.use_domain('TW', function() {
	CeL.assert([ '載入中…', CeL.gettext('Loading..') ]);
	CeL.assert([ '已載入 20%…', CeL.gettext('Loading %1%..', 20) ]);
	CeL.log('System message test OK');
}, true);

basic test

//	設定欲轉換的文字格式。
CeL.gettext.set_text({
	'%n1 smart ways to spend %c2' : '%Chinese/n1個花%c2的聰明方法'
}, 'Traditional Chinese');

CeL.assert([ '十個花新臺幣柒萬圓整的聰明方法',
		CeL.gettext('%n1 smart ways to spend %c2', 10, 70000) ],
		'test it with 貨幣/currency###1');

CeL.assert([ '二十五個花新臺幣捌拾億捌萬圓整的聰明方法',
		CeL.gettext('%n1 smart ways to spend %c2', 25, 8000080000) ],
		'test it with 貨幣/currency###2');

CeL.assert([ '四萬〇三十五個花新臺幣伍佰玖拾捌萬陸仟玖佰貳拾捌圓整的聰明方法',
		CeL.gettext('%n1 smart ways to spend %c2', 40035, 5986928) ],
		'test it with 貨幣/currency###3');

test with 貨幣

CeL.gettext.conversion['smart way'] = [ 'no %n', '1 %n', '%d %ns' ];
// You can also use this:
CeL.gettext.conversion['smart way'] = function(count) {
	var pattern = [ 'no %n', '1 %n', '%d %ns' ];
	return pattern[count < pattern.length ? count : pattern.length - 1]
			.replace(/%n/, 'smart way').replace(/%d/, count);
};

//	then we can use:
CeL.gettext.set_text({
	'%smart way@1 to spend %c2' : '%Chinese/n1個花%c2的聰明方法'
}, 'TW');

CeL.gettext.use_domain('繁體中文');
CeL.assert([ '十個花新臺幣柒萬圓整的聰明方法',
		CeL.gettext('%smart way@1 to spend %c2', 10, 70000) ]);
CeL.assert([ '二十五個花新臺幣捌拾億捌萬圓整的聰明方法',
		CeL.gettext('%smart way@1 to spend %c2', 25, 8000080000) ]);
CeL.assert([ '四萬〇三十五個花新臺幣伍佰玖拾捌萬陸仟玖佰貳拾捌圓整的聰明方法',
		CeL.gettext('%smart way@1 to spend %c2', 40035, 5986928) ]);

CeL.gettext.use_domain('en-US', true);
CeL.assert([ '10 smart ways to spend US$70,000',
		CeL.gettext('%smart way@1 to spend %c2', 10, 70000) ]);

runtime translate all nodes to show in specified language

//	including: interact.DOM will auto load application.locale.
CeL.run('interact.DOM', function() {
	//	setup domain (language)
	CeL.gettext.use_domain(language);

	//	simple way to create a text node with language tag.
	CeL.new_node({ T : message }, node);

	//	translate all nodes to show in specified language.
	CeL.gettext.translate_nodes();
});
Clone this wiki locally