Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
@davidbarratt/banana-i18n
Advanced tools
banana-i18n is a javascript internationalization library that uses "banana" format - A JSON based localization file format.
The 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.
Example:
App
|--src
|--doc
|--i18n
|--ar.json
|--de.json
|--en.json
|--he.json
|--hi.json
|--fr.json
|--qqq.json
A simple en.json file example is given below
{
"@metadata": {
"authors": [
"Alice",
"David",
"Santhosh"
],
"last-updated": "2012-09-21",
"locale": "en",
"message-documentation": "qqq",
"AnotherMetadata": "AnotherMedatadataValue"
},
"appname-title": "Example Application",
"appname-sub-title": "An example application with jquery.i18n",
"appname-header-introduction": "Introduction",
"appname-about": "About this application",
"appname-footer": "Footer text"
}
The 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.
Messages 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.
If you are curious to see some real jquery.i18n message file from other projects:
The localized message should be loaded before using .i18n() method. This can be done as follows:
const banana = new Banana('es',{
messages: {
'key-1': 'Localized message'
}
})
After the initialization,
const messages = {
'message-key-1': 'Localized message 1',
// Rest of the messages
};
banana.load(messages, 'es' );
If you think it is convinient to load all messages for all locales at once, you can do this as follows. Here the messages are keyed by locale.
const messages = {
'es': {
'message-key-1': 'Localized message 1 for es',
// Rest of the messages for es
},
'ru': {
'message-key-1': 'Localized message 1 for ru',
// Rest of the messages for ru
}
};
banana.load(messages); // Note that the locale parameter is missing here
Depeding on your application, the messages can be fetched from a server or a file system. Here is an example that fetches the localized messages json file.
fetch('i18n/es.json').then((response) => response.json()).then((messages) => {
banana.load(messages, 'es');
});
You may load the messages in parts too. That means, you can use the banana.load(message_set1, 'es')
and later banana.load(message_set2, 'es')
. Both of the messages will be merged to the locale. If message_2 has the same key of message_set1, the last message loaded wins.
The constructor for Banana class accepts the locale
const banana = new Banana('es')
Once the banana i18n is initialized you can change the lcoale using setLocale method
banana.setLocale('es'); // Change to new locale
All .i18n() calls will set the message for the new locale from there onwards.
If a particular message is not localized for locale, but localized for a fallback locale(defined in src/languages/fallbacks.json),
the .i18n() method will return that. By default English is the final fallback language. But this configurable using finalFallback
option. Example: new Banana('ru', {finalFallback:'es' })
Messages 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.
const message = "Welcome, $1";
banana.i18n(message, 'Alice'); // This gives "Welcome, Alice"
To 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|...}}
For example:
const message = "Found $1 {{PLURAL:$1|result|results}}";
banana.i18n(message, 1); // This gives "Found 1 result"
banana.i18n(message, 4); // This gives "Found 4 results"
Note that {{PLURAL:...}} is not case sensitive. It can be {{plural:...}} too.
In 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. You need to provide all those plural forms for a language.
For 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}}
.
You 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
{{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.
If there is an explicit plural form to be given for a specific number, it is possible with the following syntax
const message = 'Box has {{PLURAL:$1|one egg|$1 eggs|12=a dozen eggs}}.';
banana.i18n(message, 4 ); // Gives "Box has 4 eggs."
banana.i18n(message, 12 ); // Gives "Box has a dozen eggs."
Similar 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 show in example
const message = "$1 changed {{GENDER:$2|his|her}} profile picture";
banana.i18n(message, 'Alice', 'female' ); // This gives "Alice changed her profile picture"
banana.i18n(message, 'Bob', 'male' ); // This gives "Bob changed his profile picture"
Note that {{GENDER:...}} is not case sensitive. It can be {{gender:...}} too.
const banana=new Banana( { locale: 'fi' } );
const message = "{{grammar:genitive|$1}}";
banana.i18n(message, 'talo' ); // This gives "talon"
banana.locale = 'hy'; // Switch to locale Armenian
banana.i18n(message, 'Մաունա'); // This gives "Մաունայի"
To 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.//:
"Shalom, {{bidi:$1}}, hi!
"
The 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).
The 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 with same message key.
Example qqq.json:
{
"@metadata": {
"authors": [
"Developer Name"
]
},
"appname-title": "Application name. Transliteration is recommended",
"appname-sub-title": "Brief explanation of the application",
"appname-header-introduction": "Text for the introduction header",
"appname-about": "About this application text",
"appname-footer": "Footer text"
}
In 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
There are $1 cars
qqq
{sitename}
or [[link]]
To translate the banana-i18n based application, depending on the expertise of the translator, there are multiple ways.
FAQs
Banana Internationalization library
The npm package @davidbarratt/banana-i18n receives a total of 0 weekly downloads. As such, @davidbarratt/banana-i18n popularity was classified as not popular.
We found that @davidbarratt/banana-i18n demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.