addTranslator
protected
static
void
addTranslator
(
ident
,
lang
,
translator
)
- Parameters:
-
ident <String>
an identifier for the translator -
lang <String>
the BCP-47 language tag of the translator -
translator <Function>
the translator
applyTranslators
private
void
applyTranslators
(
)
fmt
static
void
fmt
(
str
,
attrs
)
For example, if {foobar}
appears in the format string, then it is replaced by the value of attrs.foobar
.
Whenever {foobar}
appears twice, only the first occurrence is expanded.
The method accepts as many attrs
parameters as you like.
They will apply in order, so that you can enforce priorities in the expansion process.
This is very useful when enforcing cascaded substitutions.
For example, fmt("oh-{foo}", { foo: "{bar}" }, { bar: "yeah!" })
will produce "oh-yeah!"
.
- Parameters:
-
str <String>
the format string -
attrs <Object<String>>
an optional object of attributes to substitute
getLang
protected
static
String
getLang
(
)
- Returns:
String
- the current global language
getRsc
protected
static
void
getRsc
(
ident
,
lang
)
- Parameters:
-
ident <String>
an identifier for the resource -
lang <String>
the optional BCP-47 language tag of the resource, defaulting to the global language set
lookupBestLang
private
static
void
lookupBestLang
(
preferredLanguages
,
availableLanguages
)
For example, with preferred languages being "fr-FR,en,de-DE"
the lokup will try in order:
fr-FR
, fr
, en
, de-DE
, and finally de
.
An empty string is returned if the match fails, i.e. if none of these values appear in available languages.
The set of preferred languages might be a string of comma separated values, or an array.
Languages appearing first are preferred over those appearing last. *
wildcard value is ignored.
When availableLanguages
is not specified, the set of all ever languages is used.
These are any languages that have some resources registered for any identifier.
- Parameters:
-
preferredLanguages <Array|String>
a set of preferred languages -
availableLanguages <Array>
an optional set of available languages
setLang
static
void
setLang
(
lang
)
- Parameters:
-
lang <String>
the new global language
setRsc
protected
static
void
setRsc
(
ident
,
lang
,
rsc
)
- Parameters:
-
ident <String>
an identifier for the resource -
lang <String>
the BCP-47 language tag of the resource content -
rsc <Object>
the resource to register