Protected
Optional
localeThe preferred locale. This corresponds to the config prefix.
en_US means "lang:en_US" config scope
es_MX means "lang:es_MX" config scope
Private
appThe global application instance.
Protected
configProtected
appGet the global Application.
Protected
containerResolve the given phrase to the locale of this service. This is where all the magic happens.
This method tries to load the given phrase from the config using load()
then localizes it
using the specified parameters.
The pluralization can be specified using the plural
parameter. If an explicit pluralization
is specified in the phrase config, that will be used. Otherwise, the pluralize
library is
used to generate one automatically.
// Example phrase config:
{
apple: 'Apple',
bunch_of_bananas: {
one: 'Bunch of Bananas',
many: 'Bunches of Bananas',
}
}
// Example usage:
locale.get('apple', { plural: 3 }) // => 'Apples'
locale.get('apple') // => 'Apple'
locale.get('bunch_of_bananas', { plural: 2 }) // => 'Bunches of Bananas'
locale.get('bunch_of_bananas', { plural: 1}) // => 'Bunch of Bananas'
If a translation cannot be found, and a fallback is specified, the fallback will be returned.
Otherwise, the value of phrase
is returned instead.
locale.get('nonexistent_phrase', { fallback: 'Hello, world!' }) // => 'Hello, world!'
Values can be interpolated into phrases using the interp
parameter. For example, if there is
a phrase my_phrase: 'Hello, :name:!
, the value of :name:
can be replaced like so:
locale.get('my_phrase', {interp: {name: 'John Doe'}}) // => 'Hello, John Doe!'
If a phrase cannot be found in the specific locale config, the service will try to load it from
the equivalent common
locale config. For example, if this.locale
is es_MX
:
// Example "lang:common:dashboard" config:
{
title: "MyDash2.0",
header: "Welcome to the dashboard!",
}
// Example "lang:es_MX:dashboard" config:
{
header: "¡Bienvenido al panel de control!",
}
// Example usage:
locale.get('dashboard.title') // => 'MyDash2.0'
locale.get('dashboard.header') // => '¡Bienvenido al panel de control!'
Optional
fallback?: stringOptional
interp?: { Optional
plural?: numberGet the method with the given name from this class, bound to this class.
function
Get the method with the given name from this class, bound to this class.
function
Rest
...args: any[]Get a wrapped helper function that can be passed around instead of this service.
Protected
loadProtected
make
Request-level service that provides localization of phrases based on config files.