Skip to content

Commit

Permalink
More tests for interpolations.
Browse files Browse the repository at this point in the history
Code cleanup in instant.
  • Loading branch information
CodeAndWeb committed Sep 19, 2024
1 parent 4d1ab07 commit 594b05b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 22 deletions.
6 changes: 6 additions & 0 deletions projects/ngx-translate/src/lib/translate.parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ describe('Parser', () => {
expect(parser.interpolate("This is a {{ key1.key2.key3 }}", {key1: {key2: {key3: "value3"}}})).toEqual("This is a value3");
});

it('should interpolate strings with arrays', () => {
expect(parser.interpolate("This is a {{ key.0 }}", {key: ["A", "B", "C"]})).toEqual("This is a A");
expect(parser.interpolate("This is a {{ key.11 }}", {key: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"]})).toEqual("This is a L");
expect(parser.interpolate("This is a {{ key.1.x }}", {key: ["A", {x: "B"}]})).toEqual("This is a B");
});

it('should support interpolation functions', () => {
expect(parser.interpolate((v: string) => v.toUpperCase() + ' YOU!', 'bless')).toBe('BLESS YOU!');
});
Expand Down
66 changes: 61 additions & 5 deletions projects/ngx-translate/src/lib/translate.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,22 +171,22 @@ describe("TranslateService", () =>
expect(() =>
{
translate.get(undefined as any);
}).toThrowError("Parameter \"key\" required");
}).toThrowError("Parameter \"key\" is required and cannot be empty");

expect(() =>
{
translate.get("");
}).toThrowError("Parameter \"key\" required");
}).toThrowError("Parameter \"key\" is required and cannot be empty");

expect(() =>
{
translate.get(null as any);
}).toThrowError("Parameter \"key\" required");
}).toThrowError("Parameter \"key\" is required and cannot be empty");

expect(() =>
{
translate.instant(undefined as any);
}).toThrowError("Parameter \"key\" required");
}).toThrowError("Parameter \"key\" is required and cannot be empty");
});

it("should be able to get translations with nested keys", () =>
Expand Down Expand Up @@ -259,7 +259,7 @@ describe("TranslateService", () =>
expect(() =>
{
translate.getStreamOnTranslationChange("");
}).toThrowError("Parameter \"key\" required");
}).toThrowError("Parameter \"key\" is required and cannot be empty");
});


Expand Down Expand Up @@ -492,6 +492,62 @@ describe("TranslateService", () =>
expect(translate.instant("TEST2")).toEqual("TEST2");
});

xit("should be able to get instant translations of an array", () =>
{
const tr = {"TEST": "This is a test", "TEST2": "This is a test2"};
translate.setTranslation("en", tr);
translate.use("en");

expect(translate.instant(["TEST", "TEST2"])).toEqual(tr);
});

it("should be able interpolate parameters in array", () =>
{
const tr = {"TEST": "Hello {{value}} 1!", "TEST2": "Hello {{value}} 2!"};
translate.setTranslation("en", tr);
translate.use("en");

expect(translate.instant(["TEST", "TEST2"], {value: "world"})).toEqual({
"TEST": "Hello world 1!",
"TEST2": "Hello world 2!"
});
});

it("should be able to return sub-trees", () =>
{
const tr = {a: {b: {x: "X", y: "Y"}}};
translate.setTranslation("en", tr);
translate.use("en");

expect(translate.instant("a.b")).toEqual({x: "X", y: "Y"});
});

xit("should be able to interpolate in sub-trees", () =>
{
const tr = {a: {b: {x: "{{value}} 1", y: "{{value}} 2"}}};
translate.setTranslation("en", tr);
translate.use("en");

expect(translate.instant("a.b", {value: "world"})).toEqual({x: "world 1", y: "world 2"});
});

it("should be able to return arrays", () =>
{
const tr = {a: {b: ["X", "Y"]}};
translate.setTranslation("en", tr);
translate.use("en");

expect(translate.instant("a.b")).toEqual(['X', 'Y']);
});

xit("should be able to interpolate in arrays", () =>
{
const tr = {a: {b: ["{{value}} 1", "{{value}} 2"]}};
translate.setTranslation("en", tr);
translate.use("en");

expect(translate.instant("a.b", {value: "world"})).toEqual(["world 1", "world 2"]);
});

})

Expand Down
49 changes: 32 additions & 17 deletions projects/ngx-translate/src/lib/translate.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ export const USE_DEFAULT_LANG = new InjectionToken<string>('USE_DEFAULT_LANG');
export const DEFAULT_LANGUAGE = new InjectionToken<string>('DEFAULT_LANGUAGE');
export const USE_EXTEND = new InjectionToken<string>('USE_EXTEND');


type Translation =
string |
Record<string, Translation> |
Translation[]
;


export interface TranslationChangeEvent {
translations: any;
lang: string;
Expand All @@ -30,7 +38,12 @@ export interface DefaultLangChangeEvent {
}

declare interface Window {
navigator: any;
navigator: {
languages?: string[],
language?: string,
browserLanguage?: string,
userLanguage?: string,
};
}

declare const window: Window;
Expand Down Expand Up @@ -370,7 +383,7 @@ export class TranslateService {
*/
public get(key: string | string[], interpolateParams?: object): Observable<string | any> {
if (!isDefined(key) || !key.length) {
throw new Error(`Parameter "key" required`);
throw new Error(`Parameter "key" is required and cannot be empty`);
}
// check if we are loading a new translation to use
if (this.pending) {
Expand All @@ -393,7 +406,7 @@ export class TranslateService {
*/
public getStreamOnTranslationChange(key: string | string[], interpolateParams?: object): Observable<string | any> {
if (!isDefined(key) || !key.length) {
throw new Error(`Parameter "key" required`);
throw new Error(`Parameter "key" is required and cannot be empty`);
}

return concat(
Expand Down Expand Up @@ -433,26 +446,28 @@ export class TranslateService {

/**
* Returns a translation instantly from the internal state of loaded translation.
* All rules regarding the current language, the preferred language of even fallback languages will be used except any promise handling.
* All rules regarding the current language, the preferred language of even fallback languages
* will be used except any promise handling.
*/
public instant(key: string | string[], interpolateParams?: object): string | any {
if (!isDefined(key) || !key.length) {
throw new Error(`Parameter "key" required`);
public instant(key: string | string[], interpolateParams?: object): string | any
{
if (!isDefined(key) || key.length === 0) {
throw new Error('Parameter "key" is required and cannot be empty');
}

const res = this.getParsedResult(this.translations[this.currentLang], key, interpolateParams);
if (isObservable(res)) {
if (key instanceof Array) {
const obj: any = {};
key.forEach((value: string, index: number) => {
obj[key[index]] = key[index];
});
return obj;
const result = this.getParsedResult(this.translations[this.currentLang], key, interpolateParams);

if (isObservable(result)) {
if (Array.isArray(key)) {
return key.reduce((acc: Record<string, string>, currKey: string) => {
acc[currKey] = currKey;
return acc;
}, {});
}
return key;
} else {
return res;
}

return result;
}

/**
Expand Down

0 comments on commit 594b05b

Please sign in to comment.