ngx-translate-testing
Version:
A library of utilities for testing with the ngx-translate i18n Angular library
166 lines (160 loc) • 6.71 kB
JavaScript
import { TranslateLoader, TranslateService, TranslateFakeCompiler, TranslateDefaultParser, FakeMissingTranslationHandler, TranslateModule } from '@ngx-translate/core';
import { of } from 'rxjs';
import * as i0 from '@angular/core';
import { NgModule } from '@angular/core';
/**
* The TestTranslateLoader class provides a simple loader that loads translations
* from a {Translations} object, which maps language codes to translations.
*
* @example
* const loader = new TestTranslateLoader({
* en: {
* greeting: 'Hello'
* },
* es: {
* greeting: 'Hola'
* }
* } as Translations);
*/
class TestTranslateLoader extends TranslateLoader {
constructor(_translations = {}) {
super();
this._translations = _translations;
}
/**
* Returns an {Observable} of translations for the specified language. If the
* language is not recognized, an empty translations object will be returned.
*
* @param language the language for which the translations should be retrieved.
* @returns the translations for the specified
* language or an empty set of translations if the language is not recognized.
*/
getTranslation(language) {
return of(this._translations[language] || {});
}
}
/**
* The TranslateTestingModule provides the {TranslateModule} as well as a
* {TranslateService} configured to return translations specific for the
* test environment.
*
* @export
*/
class TranslateTestingModule {
constructor() {
this._translations = {};
}
static withTranslations(languageOrTranslations, translations) {
const translateTestingModule = new TranslateTestingModule();
if (typeof languageOrTranslations === 'string') {
return translateTestingModule.withTranslations(languageOrTranslations, translations);
}
return translateTestingModule.withTranslations(languageOrTranslations);
}
get ngModule() {
return TranslateTestingModule;
}
get providers() {
const translateService = new TranslateService(null, new TestTranslateLoader(this._translations), this._compiler || new TranslateFakeCompiler(), this._parser || new TranslateDefaultParser(), new FakeMissingTranslationHandler(), true, true, false, this._defaultLanguage);
return [
{
provide: TranslateService,
useValue: translateService
}
];
}
withTranslations(languageOrTranslations, translations) {
if (typeof languageOrTranslations === 'string' && translations) {
this.addTranslations(languageOrTranslations, translations);
this._defaultLanguage = languageOrTranslations;
}
else if (languageOrTranslations) {
Object.keys(languageOrTranslations).forEach(language => this.addTranslations(language, languageOrTranslations[language]));
}
return this;
}
/**
* Updates the {TranslationTestingModule} to provide a {TranslateService} that will
* use the provided {TranslateCompiler} to translate the test translations.
*
* @example
*
* TranslateTestingModule.withTranslations('en', {people: '{gender, select, male{He is} female{She is} other{They are}} {how})'})
* .withCompiler(new TranslateMessageFormatCompiler());
*
* @param compiler the compiler to use to compile the test translations.
* @returns the instance that can be used to chain additional configuration.
* @memberof TranslateTestingModule
*/
withCompiler(compiler) {
this._compiler = compiler;
return this;
}
/**
* Updates the {TranslateTestingModule} to provide a {TranslateService} that will
* use the provided {TranslateParser} to parse the test translations.
*
* @example
*
* TranslateTestingModule.withTranslations('en', {key: 'content'})
* .withParser(new CustomTranslateDefaultParser());
*
* @param parser the parser to use to parse the test translations.
* @returns the instance that can be used to chain additional configuration.
* @memberof TranslateTestingModule
*/
withParser(parser) {
this._parser = parser;
return this;
}
/**
* Updates the {TranslateTestingModule} to use the provided language as the default language.
* By default, the default language will be set to the first language provided.
*
* @example
*
* TranslateTestingModule.withTranslations('es', SPANISH_TRANSLATIONS)
* .withTranslations('en', ENGLISH_TRANSLATIONS)
* .withDefaultLanguage('en');
*
* @param language the new default language for translations.
* @returns the instance that can be used to chain additional configuration.
* @memberof TranslateTestingModule
*/
withDefaultLanguage(language) {
this._defaultLanguage = language || this._defaultLanguage;
return this;
}
addTranslations(language, translations) {
if (!this._defaultLanguage) {
this._defaultLanguage = language;
}
if (this._translations[language]) {
this._translations[language] = {
...this._translations[language],
...translations
};
}
else {
this._translations[language] = translations;
}
}
/** @nocollapse */ static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.0.5", ngImport: i0, type: TranslateTestingModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); }
/** @nocollapse */ static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "16.0.5", ngImport: i0, type: TranslateTestingModule, imports: [TranslateModule], exports: [TranslateModule] }); }
/** @nocollapse */ static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "16.0.5", ngImport: i0, type: TranslateTestingModule, imports: [TranslateModule, TranslateModule] }); }
}
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.0.5", ngImport: i0, type: TranslateTestingModule, decorators: [{
type: NgModule,
args: [{
imports: [TranslateModule],
exports: [TranslateModule]
}]
}] });
/*
* Public API Surface of testing
*/
/**
* Generated bundle index. Do not edit.
*/
export { TestTranslateLoader, TranslateTestingModule };
//# sourceMappingURL=ngx-translate-testing.mjs.map