Documente Academic
Documente Profesional
Documente Cultură
Table of Content
Features Of JQuery I18n
Usage
Translation
JQuery.I18n.Properties
Using Jquery.I18n.Properties
Features Of JQuery.I18n.Properties
Language Control
Example Using JQuery.I18n.Properties
Define .Properties Files
Loading Localized Strings From .Properties
Options
8
8
8
9
10
11
12
"@metadata": {
"author": "Colleen",
"last-updated": "2016-09-21",
"message-documentation": "qqq"
},
"greeting": "Hello",
"bye": "Goodbye"
10
An example JSON file with multiple languages and @metadata is provided below:
1
"@metadata": {
"author": "Fred",
"last-updated": "2016-09-21",
"message-documentation": "qqq",
"arrayGroups": {
"label": {
"en": "label_en",
10
"fr": "label_fr"
11
12
13
},
14
"en": {
15
"greeting": Hello,
16
"bye": "Goodbye"
17
18
"fr": {
19
"greeting": Bonjour,
20
21
22
}
}
Usage
Some of the ways that jquery.i18n can be used are shown in the table below.
Usage Area
Description
Switching
locale
The locale for the web page can be obtained using the locale
option:
$.i18n( {
locale: fr // Locale is French
} );
To switch to another locale after plugin is initialized:
$.i18n ().locale = ml;
Message
Loading
Data API
Message
Format
Placeholders
Message
Format
Plurals
In English, there are only two plural forms, but in many other
languages, there are more than two plural forms.
Message
Format
Gender
{{GENDER}} syntax
With the gender of placeholders, the syntax changes
dynamically.
Translation
There are several ways to translate a jquery.i18n application:
1. Editing the json files. This is suitable for small applications with a limited number of
languages.
2. Have a translation interface with the application. This option works for proprietary or
private applications with many translators.
JQuery.I18n.Properties
jQuery.i18n.properties is a jQuery plugin for internationalization. Similar to Java, jquery i18n
uses resource bundles (.properties files). Resource bundles are used to store locale-specific
information such as text messages. They allow an easy way of accessing locale-specific
information and adding locales easily by adding additional resource bundles. The .properties
files contain locale-specific key-value pairs and interprets these files based upon language and
country codes.
Using Jquery.I18n.Properties
The jquery.i18n.properties.js plugin can be downloaded from
https://github.com/jquery-i18n-properties/jquery-i18n-properties. The plug-in should be
included in the <head> section of your HTML page. The version of jquery that you use should be
specified in the source file as follows:
1
<HEAD>
</HEAD>
Features Of JQuery.I18n.Properties
Works like Java i18n. Uses resource bundles (.properties files) for translations. Uses
ISO-639 for language codes and ISO-3166 for country codes.
If no language is specified, uses the default browser language. The default language in
the resource bundles is always used first. The user-specified language will be loaded next.
The resource bundle strings allow placeholder substitution, and there is support for
namespaces in the keys.
Language Control
To make your code more efficient with less 404 errors, a languages.json file should be used. A
languages.json file defines the languages and the properties files that can be used. The
languages.json should be placed into the same directory as the language properties files. An
example of a languages.json file is as follows:
1
2
{
"languages": [
"en_GB",
"es_ES",
"pt_BR",
"sv_SE"
7
8
]
}
Next, the HTML code will be created. The HTML contains a dropdown that allows the user to
select a language. The messages below the dropdown are localized based upon the language
chosen.
1
<HTML>
2
3
<HEAD>
</HEAD>
7
8
<BODY>
10
<div id="langBox">
11
Language:
12
<select id="lang">
13
14
<option value="tr">Turkish</option>
15
<option value="fr">French</option>
16
</select>
17
</div><br>
18
19
20
21
</BODY>
22
23
</HTML>
Messages.properties
5
6
Messages_fr.properties
9
10
11
Messages_tr.properties
12
13
$(function() {
$.i18n.properties({
name: 'Messages',
path: 'bundle/',
mode: 'both',
language: lang,
callback: function() {
$("#lWelcome").text($.i18n.prop('lWelcome'));
$("#lSelLang").text($.i18n.prop('lSelLang', lang));
10
11
});
12
});
Options
Option
Description
Notes
name
Optional
String or
String[]
language
Optional
String
path
Optional
String
mode
Optional
String
cache
Optional
boolean
encoding
Optional
String
callback
Optional
function()
phraseapp.com
sales@phraseapp.com
+49-40-357-187-76
Groe Theaterstrae 39
Hamburg, Germany