Skip to main content

Hack The Library

The Loader

The loader provides you with the ability to override the default handling of translation file loading.

import { TranslocoLoader } from '@jsverse/transloco';

export class CustomLoader implements TranslocoLoader {
getTranslation(lang: string) {
if(langInLocalStorage) {
return of(langFromStorage);
}

return ...
}
}

The Interceptor

The interceptor provides you with the ability to manipulate the translation object before it is saved by the service.

import { TranslocoInterceptor } from '@jsverse/transloco';

export class CustomInterceptor implements TranslocoInterceptor {
preSaveTranslation(translation: Translation, lang: string): Translation {
return translation;
}

preSaveTranslationKey(key: string, value: string, lang: string): string {
return value;
}
}

Missing Handler

This handler is responsible for handling missing keys. The default handler calls console.warn() with the key when config.isProdMode is set to false, and returns an empty string to use as a replacement for the missing key's value.

import { TranslocoMissingHandler } from '@jsverse/transloco';

export class CustomHandler implements TranslocoMissingHandler {
handle(key: string, config: TranslocoConfig) {
return '...';
}
}

Fallback Strategy

The fallback strategy is responsible for loading the fallback translation file, when the selected active language has failed to load. The default behavior is to load the language set in the config.fallbackLang, and set it as the new active language.

When you need more control over this functionality, you can define your own strategy:

import { TranslocoFallbackStrategy } from '@jsverse/transloco';

export class CustomStrategy implements TranslocoFallbackStrategy {
getNextLangs(failedLang: string) {
return '...';
}
}

The getNextLangs method is called with the failed language, and should return an array containing the next languages to load, in order of preference.