new Strargs(str, args) → {String}
This is a utility method to provide some way to support positional parameters within a string, as javascript lacks a printf() method.
The format is similar to printf(), but greatly simplified (ie. fewer features).
Any percent signs followed by numbers are replaced with the corresponding item from the argument’s array.
Any percent signs followed by numbers are replaced with the corresponding item from the argument’s array.
Parameters:
Name | Type | Description |
---|---|---|
str |
String | a string that potentially contains formatting characters |
args |
Array | an array of positional replacement values |
- Source:
Returns:
The formatted text.
- Type
- String
Example
iJS.i18n.setlocale("fr_FR.UTF8") ;
iJS.i18n.bindtextdomain("fr_FR.UTF8") ;
iJS.i18n.try_load_lang() ;
//One common mistake is to interpolate a variable into the string like this:
var translated = iJS._("Hello " + full_name); //`iJS._()` can be replace by `iJS.i18n.gettext()`
//The interpolation will happen before it's passed to gettext, and it's
//unlikely you'll have a translation for every "Hello Tom" and "Hello Dick"
//and "Hellow Harry" that may arise.
//Use `strargs()` (see below) to solve this problem:
var translated = iJS.Gettext.strargs( iJS._("Hello %1"), [full_name] );
/* This is espeically useful when multiple replacements are needed, as they
may not appear in the same order within the translation. As an English to
French example:
Expected result: "This is the red ball"
English: "This is the %1 %2"
French: "C'est le %2 %1"
Code: iJS.Gettext.strargs( iJS._("This is the %1 %2"), ["red", "ball"] );
(The example show thing that not have to be done because neither color nor text
will get translated here ...).