Language API
A list of TranslocoService language-related API methods and their usages
getDefaultLang()
getDefaultLang()Returns the default language of the application.
app.component.ts
export class AppComponent {
constructor(private translocoService: TranslocoService) {
const defaultLang = translocoService.getDefaultLang();
console.log(defaultLang);
}
}setDefaultLang()
setDefaultLang()Sets the default language.
app.component.ts
export class AppComponent {
constructor(private translocoService: TranslocoService) {
translocoService.setDefaultLang('es');
}
}getActiveLang()
getActiveLang()Returns the current active language.
app.component.ts
export class AppComponent {
constructor(private translocoService: TranslocoService) {
const activeLang = translocoService.getActiveLang();
console.log(activeLang);
}
}setActiveLang()
setActiveLang()Sets the active language for the application.
app.component.ts
export class AppComponent {
constructor(private translocoService: TranslocoService) {
translocoService.setActiveLang('es');
}
}getAvailableLangs()
getAvailableLangs()Retrieves the list of available languages.
app.component.ts
export class AppComponent {
constructor(private translocoService: TranslocoService) {
const availableLangs = translocoService.getAvailableLangs();
console.log(availableLangs);
}
}setFallbackLangForMissingTranslation()
setFallbackLangForMissingTranslation()Defines a fallback language to be used when a translation key is missing for the active language.
app.component.ts
export class AppComponent {
constructor(private translocoService: TranslocoService) {
translocoService.setFallbackLangForMissingTranslation({
fallbackLang: 'he',
});
}
}setAvailableLangs()
setAvailableLangs()Sets the list of available languages.
app.component.ts
export class AppComponent {
constructor(private translocoService: TranslocoService) {
translocoService.setAvailableLangs(['en', 'es']);
// Alternatively, use an array of objects for additional metadata
translocoService.setAvailableLangs([{ id: 'en', label: 'English' }]);
}
}langChanges$
langChanges$An observable that emits whenever the active language changes.
app.component.ts
export class AppComponent {
constructor(private translocoService: TranslocoService) {
translocoService.langChanges$.subscribe(lang => {
console.log(`Language changed to: ${lang}`);
});
}
}load()
load()Loads the specified language and adds it to the service.
app.component.ts
export class AppComponent {
constructor(private translocoService: TranslocoService) {
translocoService.load('en').subscribe(() => {
console.log('Language loaded successfully');
});
}
}Was this helpful?

