• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/languages/H03-May-2022-

CODE_OF_CONDUCT.mdH A D13-Nov-2021135 21

CREDITSH A D13-Nov-2021647 2419

GPL-LICENSEH A D04-Nov-201917.7 KiB340281

MIT-LICENSEH A D04-Nov-20191.1 KiB2218

README.mdH A D13-Nov-202117.1 KiB457347

README.md

1jQuery.i18n
2===========
3
4[![npm][npm]][npm-url]
5
6> NOTE: For jquery independent version of this library, see https://github.com/wikimedia/banana-i18n
7
8jQuery.i18n is a jQuery based Javascript internationalization library. It helps you to internationalize your web applications easily.
9
10This is a project by Wikimedia foundation's [Language Engineering team](https://www.mediawiki.org/wiki/Wikimedia_Language_engineering) and used in some of the Wikimedia Foundation projects like Universal Language Selector.
11
12The jquery.i18n library uses a json based localization file format, "banana", which is used as the localization file format for  MediaWiki and other projects.
13
14
15Features
16========
17* Simple file format - JSON. Easily readable for humans and machines.
18* Author and metadata information is not lost anywhere. There are other file formats using comments to store this.
19* Uses MediaWiki convention for placeholders. Easily readable and proven convention. Example: ```There are $1 cars```
20* Supports plural conversion without using extra messages for all plural forms. Plural rule handling is done using CLDR. Covers a wide range of languages
21* Supports gender. By passing the gender value, you get correct sentences according to gender.
22* Supports grammar forms. jquery.i18n has a basic but extensible grammar conversion support
23* Fallback chains for all languages.
24* Data api- the message key. Example: ```<li data-i18n="message-key"></li>```.
25* Dynamic change of interface language without refreshing a webpage.
26* Nestable grammar, plural, gender support. These constructs can be nested to any arbitrary level for supporting sophisticated message localization
27* Message documentation through special language code ```qqq```
28* Extensible message parser to add or customize magic words in the messages. Example: ```{sitename}``` or ```[[link]]```
29
30
31Quick start
32-----------
33
34```bash
35git clone https://github.com/wikimedia/jquery.i18n.git
36cd jquery.i18n
37git submodule update --init
38```
39
40Testing
41-------
42
43```shell
44npm install
45```
46
47To run tests locally, run `npm test`, and this will run the tests.
48
49Message File Format
50===================
51
52The message files are json formatted. As a convention, you can have a folder named i18n inside your source code. For each language or locale, have a file named like languagecode.json.
53
54Example:
55```
56App
57	|--src
58	|--doc
59	|--i18n
60		|--ar.json
61		|--de.json
62		|--en.json
63		|--he.json
64		|--hi.json
65		|--fr.json
66		|--qqq.json
67```
68
69A simple en.json file example is given below
70
71```json
72{
73	"@metadata": {
74		"authors": [
75			"Alice",
76			"David",
77			"Santhosh"
78		],
79		"last-updated": "2012-09-21",
80		"locale": "en",
81		"message-documentation": "qqq",
82		"AnotherMetadata": "AnotherMedatadataValue"
83	},
84	"appname-title": "Example Application",
85	"appname-sub-title": "An example application with jquery.i18n",
86	"appname-header-introduction": "Introduction",
87	"appname-about": "About this application",
88	"appname-footer": "Footer text"
89}
90```
91
92The json file should be a valid json. The ```@metadata``` holds all kind of data that are not messages. You can store author information, copyright, updated date or anything there.
93
94Messages are key-value pairs. It is a good convention to prefix your appname to message keys to make the messages unique. It acts as the namespace for the message keys. It is also a good convention to have the message keys with ```-``` separated words, all in lower case.
95
96If you are curious to see some real jquery.i18n message file from other projects:
97
98- message files of MediaWiki https://github.com/wikimedia/mediawiki-core/tree/master/languages/i18n
99- message files from jquery.uls project https://github.com/wikimedia/jquery.uls/blob/master/i18n
100
101Single message file for all languages
102-------------------------------------
103There are some alternate message file formats supported for different use cases. If your application is not big, and want all the translation in a single file, you can have it as shown in the below example:
104
105```json
106{
107	"@metadata": {
108		"authors": [
109			"Alice",
110			"David",
111			"Santhosh"
112		],
113		"last-updated": "2012-09-21",
114		"locale": "en",
115		"message-documentation": "qqq",
116		"AnotherMetadata": "AnotherMedatadataValue"
117	},
118	"en": {
119		"appname-title": "Example Application",
120		"appname-sub-title": "An example application with jquery.i18n",
121		"appname-header-introduction": "Introduction",
122		"appname-about": "About this application",
123		"appname-footer": "Footer text"
124		},
125	"ml": {
126		"appname-title": "അപ്ലിക്കേഷന്‍ ഉദാഹരണം",
127		"appname-sub-title": "jquery.i18n ഉപയോഗിച്ചുള്ള അപ്ലിക്കേഷന്‍ ഉദാഹരണം",
128		"appname-header-introduction": "ആമുഖം",
129		"appname-about": "ഈ അപ്ലിക്കേഷനെപ്പറ്റി",
130		"appname-footer": "അടിക്കുറിപ്പു്"
131	}
132}
133```
134
135Here the json file contains language code as key-value and messagekey-message pairs as the value for all language pairs. You can choose this format or per-language file formats depending on your use case. Per-language files are more convenient for collaboration, version controlling, scalability, etc.
136
137In this approach, it is also possible to give a file name as the value of language code.
138
139```json
140{
141	"@metadata": {
142		"authors": [
143			"Alice",
144			"David",
145			"Santhosh"
146		],
147		"last-updated": "2012-09-21",
148		"locale": "en",
149		"message-documentation": "qqq",
150		"AnotherMetadata": "AnotherMedatadataValue"
151	},
152	"en": {
153		"appname-title": "Example Application",
154		"appname-sub-title": "An example application with jquery.i18n",
155		"appname-header-introduction": "Introduction",
156		"appname-about": "About this application",
157		"appname-footer": "Footer text"
158		},
159	"ml": "path/to/ml.json"
160}
161```
162
163Translation
164===========
165To translate the jquery.i18n application, depending on the expertise of the translator, there are multiple ways.
166
167* Editing the json files directly - Suitable for translators with technical background. Also suitable if your application is small and you want to work with only a small number of languages
168* Providing a translation interface along with your application: Suitable for proprietary or private applications with significant amount of translators
169* Using open source translation platforms like translatewiki.net. The MediaWiki and jquery.uls from previous examples use translatewiki.net for crowdsourced message translation. Translatewiki.net can update your code repo at regular intervals with updated translations. Highly recommended if your application is opensource and want it to be localized to as many as languages possible with maximum number of translators.
170
171Usage
172=====
173
174## Switching locale
175
176While initializing the `jquery.i18n`, the locale for the page can be given using the `locale` option. For example
177
178```javascript
179$.i18n( {
180    locale: 'he' // Locale is Hebrew
181} );
182```
183
184In case locale option is not given, `jquery.i18n` plugin will use the language attribute given for the html tag. For example
185
186```html
187<html lang="he" dir="rtl">
188```
189
190In this case, the locale will be he(Hebrew). If that `lang` attribute is also missing, it will try to use the locale specified by the browser.
191
192It is possible to switch to another locale after plugin is initialized. See below example:
193
194```javascript
195$.i18n({
196    locale: 'he' // Locale is Hebrew
197});
198$.i18n( 'message-hello' ); // This will give the Hebrew translation of message key `message-hello`.
199$.i18n().locale = 'ml'; // Now onwards locale is 'Malayalam'
200$.i18n( 'message-hello' ); // This will give the Malayalam translation of message key `message-hello`.
201```
202
203## Message Loading
204
205JSON formatted messages can be loaded to the plugin using multiple ways.
206
207### Dynamic loading using `load` method.
208
209Following example shows loading messages for two locales- localex, and localey. Here localex and localey are just examples. They should be valid IS0 639 language codes(eg: en, ml, hi, fr, ta etc)
210
211```javascript
212$.i18n().load( {
213	'localex' : {
214		'message-key1' : 'message1' // Message for localex.
215	},
216	'localey' : {
217		'message-key1' : 'message1'
218	}
219} );
220```
221
222If we want to load the messages for a specific locale, it can be done like this:
223
224```javascript
225$.i18n().load({
226    'message-hello': 'Hello World',
227    'message-welcome': 'Welcome'
228}, 'en');
229```
230
231Note the second argument for the `load` method. It should be a valid language code.
232
233It is also possible to refer messages from an external URL. See below example
234
235```javascript
236$.i18n().load( {
237	en: {
238		'message-hello': 'Hello World',
239		'message-welcome': 'Welcome'
240	},
241	hi: 'i18n/messages-hi.json', // Messages for Hindi
242	de: 'i18n/messages-de.json'
243} );
244```
245
246Messages for a locale can be also loaded in parts. Example
247
248```javascript
249$.i18n().load( {
250	en: {
251		'message-hello': 'Hello World',
252		'message-welcome': 'Welcome'
253	}
254} );
255
256$.i18n().load( {
257    	// This does not remove the previous messages.
258	en: {
259		'message-header' : 'Header',
260		'message-footer' : 'Footer',
261		// This will overwrite message-welcome message
262		'message-welcome' : 'Welcome back'
263	}
264} );
265```
266
267Since it is desirable to render interface messages instantly and not after a delay of loading the message files from a server, make sure that the messages are present at client side before using jQuery.i18n.
268
269The library should expose an API to load an object containing key-value pair of messages. Example: ```$.i18n.load(data)```. This will return a ```jQuery.Promise```.
270
271jquery.i18n plugin
272=========================
273
274The jQuery plugin defines ```$.i18n()``` and ```$.fn.i18n()```
275
276```javascript
277$.i18n( 'message-key-sample1' );
278$.i18n( 'message-key-sample1' );
279$.i18n( 'Found $1 {{plural:$1|result|results}}', 10 ); // Message key itself is message text
280$.i18n( 'Showing $1 out of $2 {{plural:$2|result|results}}', 5,100 );
281$.i18n( 'User X updated {{gender|his|her}} profile', 'male' );
282
283$( '#foo' ).i18n(); // to translate the element matching jquery selector based on data-i18n key
284```
285
286Data API
287--------
288
289It is possible to display localized messages without any custom JavaScript. For the HTML tags, add an attribute data-i18n with value as the message key. Example:
290```html
291<li data-i18n="message-key"></li>.
292```
293
294It is also possible to have the above li node with fallback text already in place.
295```html
296<li data-i18n="message-key">Fallback text</li>
297```
298
299The framework will place the localized message corresponding to message-key as the text value of the node. Similar to $('selector').i18n( ... ).
300This will not work for dynamically created elements.
301
302Note that if data-i18n contains html markup, that html will not be used as the element content, instead, the text version will be used. But if the message key is prefixed with `[html]`, the element's html will be changed. For example ```<li data-i18n="[html]message-key">Fallback html</li>```, in this if the message-key has a value containing HTML markup, the `<li>` tags html will be replaced by that html.
303
304
305If you want to change the html of the element, you can also use: ```$(selector).html($.i18n(messagekey))```
306
307Examples
308========
309
310See https://thottingal.in/projects/js/jquery.i18n/demo/
311
312Message format
313==============
314
315## Placeholders
316
317Messages take parameters. They are represented by $1, $2, $3, … in the message texts, and replaced at run time. Typical parameter values are numbers (Example: "Delete 3 versions?"), or user names (Example: "Page last edited by $1"), page names, links, and so on, or sometimes other messages.
318
319```javascript
320var message = "Welcome, $1";
321$.i18n(message, 'Alice'); // This gives "Welcome, Alice"
322```
323
324
325## Plurals
326
327To make the syntax of sentence correct, plural forms are required. jquery.i18n support plural forms in the message using the syntax `{{PLURAL:$1|pluralform1|pluralform2|...}}`
328
329For example:
330
331```javascript
332var message = "Found $1 {{PLURAL:$1|result|results}}";
333$.i18n(message, 1); // This gives "Found 1 result"
334$.i18n(message, 4); // This gives "Found 4 results"
335```
336Note that {{PLURAL:...}} is not case sensitive. It can be {{plural:...}} too.
337
338In case of English, there are only 2 plural forms, but many languages use more than 2 plural forms. All the plural forms can be given in the above syntax, separated by pipe(|). The number of plural forms for each language is defined in [CLDR](https://www.unicode.org/cldr/charts/latest/supplemental/language_plural_rules.html). You need to provide all those plural forms for a language. Please note that many languages will require the inclusion of `CLDRPluralRuleParser.js` ([from here](https://github.com/santhoshtr/CLDRPluralRuleParser/tree/8baf9aedc428924fe6ee508b3d952cb5564efb3a/src)) as well as this project's own files to work properly.
339
340For example, English has 2 plural forms and the message format will look like `{{PLURAL:$1|one|other}}`. for Arabic there are 6 plural forms and format will look like `{{PLURAL:$1|zero|one|two|few|many|other}}`.
341
342You cannot skip a plural form from the middle or beginning. However, you can skip from end. For example, in Arabic, if the message is like
343`{{PLURAL:$1|A|B}}`, for 0, A will be used, for numbers that fall under one, two, few, many, other categories B will be used.
344
345If there is an explicit plural form to be given for a specific number, it is possible with the following syntax
346
347```
348var message = 'Box has {{PLURAL:$1|one egg|$1 eggs|12=a dozen eggs}}.';
349$.i18n(message, 4 ); // Gives "Box has 4 eggs."
350$.i18n(message, 12 ); // Gives "Box has a dozen eggs."
351```
352
353## Gender
354Similar to plural, depending on gender of placeholders, mostly user names, the syntax changes dynamically. An example in English is "Alice changed her profile picture" and "Bob changed his profile picture". To support this {{GENDER...}} syntax can be used as shown in example
355
356```javascript
357var message = "$1 changed {{GENDER:$2|his|her}} profile picture";
358$.i18n(message, 'Alice', 'female' ); // This gives "Alice changed her profile picture"
359$.i18n(message, 'Bob', 'male' ); // This gives "Bob changed his profile picture"
360```
361
362Note that {{GENDER:...}} is not case sensitive. It can be {{gender:...}} too.
363
364## Grammar
365
366
367```javascript
368$.i18n( { locale: 'fi' } );
369
370var message = "{{grammar:genitive|$1}}";
371
372$.i18n(message, 'talo' ); // This gives "talon"
373
374$.i18n().locale = 'hy'; // Switch to locale Armenian
375$.i18n(message, 'Մաունա'); // This gives "Մաունայի"
376```
377
378## Directionality-safe isolation
379
380To avoid BIDI corruption that looks like "(Foo_(Bar", which happens when a string is inserted into a context with the reverse directionality, you can use `{{bidi:…}}`. Directionality-neutral characters at the edge of the string can get wrongly interpreted by the BIDI algorithm. This would let you embed your substituted string into a new BIDI context, //e.g.//:
381
382   "`Shalom, {{bidi:$1}}, hi!`"
383
384The embedded context's directionality is determined by looking at the argument for `$1`, and then explicitly inserted into the Unicode text, ensuring correct rendering (because then the bidi algorithm "knows" the argument text is a separate context).
385
386
387Fallback
388========
389
390The plugin takes an option 'fallback' with the default value 'en'. The library reuses the fallback data available in MediaWiki for calculating the language fallbacks. Fallbacks are used when a message key is not found in a locale. Example fallbacks: sa->hi->en or tt->tt-cyrl->ru.
391
392See jquery.i18n.fallbacks.js in the source.
393
394Magic word support
395===================
396* For plural, gender and grammar support, MediaWiki template-like syntax - {{...}} will be used.
397* There will be a default implementation for all these in $.i18n.language['default']
398* The plural, gender and grammar methods in ```$.i18n.language[ 'default' ]``` can be overridden or extended in ```$.i18n.language['languageCode']```.
399* Language-specific rules about Gender and Grammar can be written in languages/langXYZ.js files
400* Plural forms will be dynamically calculated using the CLDR plural parser.
401
402Extending the parser
403--------------------
404Following example illustrates extending the parser to support more magic words
405
406```javascript
407$.extend( $.i18n.parser.emitter, {
408	// Handle SITENAME keywords
409	sitename: function () {
410		return 'Wikipedia';
411	},
412	// Handle LINK keywords
413	link: function ( nodes ) {
414		return '<a href="' + nodes[1] + '">' + nodes[0] + '</a>';
415	}
416} );
417```
418
419This will parse the message
420```javascript
421$.i18n( '{{link:{{SITENAME}}|https://en.wikipedia.org}}' );
422```
423
424to
425
426```html
427<a href="https://en.wikipedia.org">Wikipedia</a>
428```
429
430Message documentation
431=====================
432
433The message keys and messages won't give a enough context about the message being translated to the translator. Whenever a developer adds a new message, it is a usual practice to document the message to a file named qqq.json
434with same message key.
435
436Example qqq.json:
437```json
438{
439	"@metadata": {
440		"authors": [
441			"Developer Name"
442		]
443	},
444	"appname-title": "Application name. Transliteration is recommended",
445	"appname-sub-title": "Brief explanation of the application",
446	"appname-header-introduction": "Text for the introduction header",
447	"appname-about": "About this application text",
448	"appname-footer": "Footer text"
449}
450
451```
452
453In MediaWiki and its hundreds of extensions, message documentation is a strictly followed practice. There is a grunt task to check whether all messages are documented or not. See https://www.npmjs.org/package/grunt-banana-checker
454
455[npm]: https://img.shields.io/npm/v/@wikimedia/jquery.i18n.svg
456[npm-url]: https://npmjs.com/package/@wikimedia/jquery.i18n
457