TranslateService API
This content is for v15. Switch to the latest version for up-to-date documentation.
Methods
setDefaultLang
Sets the default language to use as a fallback. A translation is used from the default language, if no translation is found in the current language.
Calling setDefaultLang()
uses the loader to retrieve the language and
updates the list of available languages which can be retrieved using getLangs()
.
getDefaultLang
Returns the default language.
use
Changes the currently active language. This method triggers the loader to retrieve the translations if the language has not been loaded yet.
Calling use()
fetches the translation for the given language and updates the
list of available languages, which can be accessed using
getLangs()
.
getTranslation
Gets an object of translations for a given language with the current loader.
setTranslation
Manually sets an object of translations for a given language,
set shouldMerge
to true if you want to append the translations instead of replacing them.
Using setTranslation
updates the list of available languages which can be retrieved using getLangs
.
addLangs
Add new languages to the list. This does not invoke the loader to retrieve the languages.
getLangs
Returns an array of currently available languages. The list can be extended
calling setDefaultLang
, use
, setTranslation
oraddLangs
.
get
Retrieves the translated value for a key (or an array of keys). If the translation
is not found, the key itself is returned. The Observable
will emit once the
language file has finished loading.
If you also want to receive updates when the language changes, consider using
stream()
instead.
getStreamOnTranslationChange
Returns a stream of translated values for a key (or an array of keys). If the
translation is not found, the key itself is returned. Without any
onTranslationChange()
events, this behaves
similarly to get()
. However, it will also emit new values whenever the
translation changes.
stream
Returns a stream of translated values for a key (or an array of keys). If the
translation is not found, the key itself is returned. Without any
onLangChange()
events, this behaves the same as
get()
, but it will also emit new values whenever the active language
changes.
instant
Retrieves the translated value for a key (or an array of keys) instantly.
set
Assigns a translated value to a specific key. Optionally, you can specify a language to set the translation for. If no language is provided, it defaults to the currently active language.
reloadLang
This method calls resetLang()
and re-fetches the translations
object using the current loader for the specified language.
resetLang
Removes the current translations for the specified language.
getBrowserLang
Returns the browser’s current language setting if available, or undefined
if
it cannot be determined.
getBrowserCultureLang
Returns the browser’s full culture language code (e.g., "en-US"
) if available,
or undefined
if it cannot be determined.
Properties
The TranslateService exposes the following properties:
Properties
The TranslateService
exposes the following properties:
Property | Description |
---|---|
langs | List of languages. string[] |
translations | Object containing translations per language any |
currentLang | The language currently in use. string |
defaultLang | The default (fallback) language. string |
currentLoader | The current instance of the loader (static loader by default). TranslateLoader |
onLangChange | Event emitter that fires when the language changes. EventEmitter<LangChangeEvent> |
onTranslationChange | Event emitter that fires when the translations change. EventEmitter<TranslationChangeEvent> |
onDefaultLangChange | Event emitter that fires when the default language changes. EventEmitter<DefaultLangChangeEvent> |
Event Emitters
onLangChange (Event Emitter)
An EventEmitter
that listens for language change events. A LangChangeEvent
object is emitted, containing the following properties:
Name | Type | Description |
---|---|---|
lang | string | The code of the newly activated language. |
translations | any | An object containing the updated translations. |
Example:
onTranslationChange (Event Emitter)
An EventEmitter
that listens for translation change events.
A TranslationChangeEvent
object is emitted, containing the following properties:
Name | Type | Description |
---|---|---|
lang | string | The code of the currently active language. |
translations | any | An object containing the updated translations. |
Example:
onDefaultLangChange (Event Emitter)
An EventEmitter
that listens for default language change events.
A DefaultLangChangeEvent
object is emitted, containing the following properties:
Name | Type | Description |
---|---|---|
lang | string | The code of the newly set default language. |
translations | any | An object containing the default translations. |
Example: