Translation API
Below is a list of the TranslocoService
API and their usages:
translate()
#
Translate the given key. Sometimes you may need to translate a key in a component or a service. To do so, you can inject the TranslocoService
and use its translate
method:
important
Note that in order to safely use this method, you are responsible for ensuring that the translation files have been successfully loaded by the time it's called.
If you aren't sure, you can use the selectTranslate()
method instead:
selectTranslate()
#
Returns an observable
that when subscribed loads the translation file and emits the translation of the given key:
selectTranslate
will emit each time the active language is changed.
You can also select a translation from scope
, simply inject the scope using the TRANSLOCO_SCOPE
token
and provide it to the selectTranslate
function:
translateObject()
#
The following code is based on the en.json
above โ
- Returns an
object
based on the given path:
- Given a
key: params
map (object
|Map
), returns anarray
of translated values:
important
Note that in order to safely use this method, you are responsible for ensuring that the translation files have been successfully loaded by the time it's called.
If you aren't sure, you can use the selectTranslateObject()
method instead:
selectTranslateObject()
#
Load the translation file (if not loaded yet) and behaves the same as translateObject
.
The only difference is that this method returns an observable to which you can subscribe.
The following code is based on the en.json
in translateObject
โ
getTranslation()
#
Returns the selected language translation or a scope translation, if a language isn't passed, the method will return the entire translation map:
selectTranslation()
#
Returns an observable
that when subscribed loads and emits the given language translation object, if no language is passed the active language will be used.
This method can also be used to retrieve a scope translation object:
setTranslation()
#
Manually sets a translations object to be used for a given language, set merge to true if you want to append the translations instead of replacing them:
setTranslationKey()
#
Sets the translated value of a key. If a language isn't specified in the third parameter, it sets the key value for the current active language:
events$
#
Subscribe to the translation loading events:
important
Note that these events will only fire when the translation is loaded from the server, meaning that when switching from a given lang and back won't trigger the events again since the language is loaded from the cache.