Skip to content

Commit

Permalink
Added interpolation support in translator
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschwendener committed Jan 23, 2025
1 parent 0dc6450 commit 1f92c95
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/main/Core/Translator/Contract/TFunction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type TFunction = (key: string, interpolation?: Record<string, string>) => string;
5 changes: 2 additions & 3 deletions src/main/Core/Translator/Contract/Translator.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Resources, Translations } from "@common/Core/Translator";
import type { TFunction } from "./TFunction";

/**
* Offers a method to create a translation function.
Expand All @@ -9,7 +10,5 @@ export interface Translator {
* @param resources The resources for the translations.
* @returns A translate function
*/
createT<T extends Translations>(
resources: Resources<T>,
): { t: (key: keyof T, interpolation?: Record<string, string>) => string };
createT<T extends Translations>(resources: Resources<T>): { t: TFunction };
}
1 change: 1 addition & 0 deletions src/main/Core/Translator/Contract/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./TFunction";
export * from "./Translator";
13 changes: 13 additions & 0 deletions src/main/Core/Translator/Translator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,17 @@ describe(Translator, () => {
expect(t("message")).toBe("Hello");
expect(t("otherMessage")).toBe("otherMessage");
});

it("should be able to interpolate", () => {
const getValueMock = vi.fn().mockReturnValue("en-US");
const settingsManager = <SettingsManager>{ getValue: (v, d) => getValueMock(v, d) };

const translator = new Translator(settingsManager);

const { t } = translator.createT({
"en-US": { greeting: "Hello {{name}}" },
});

expect(t("greeting", { name: "Darth Vader" })).toBe("Hello Darth Vader");
});
});
4 changes: 2 additions & 2 deletions src/main/Core/Translator/Translator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { SettingsManager } from "@Core/SettingsManager";
import type { Resources, Translations } from "@common/Core/Translator";
import { createInstance, type InitOptions } from "i18next";
import type { Translator as TranslatorInterface } from "./Contract";
import type { TFunction, Translator as TranslatorInterface } from "./Contract";

export class Translator implements TranslatorInterface {
public constructor(private readonly settingsManager: SettingsManager) {}

public createT<T extends Translations>(resources: Resources<T>): { t: (key: keyof T) => string } {
public createT<T extends Translations>(resources: Resources<T>): { t: TFunction } {
const instance = createInstance({
initImmediate: false, // Is needed for synchronous initialization
resources: this.createResources(resources),
Expand Down

0 comments on commit 1f92c95

Please sign in to comment.