-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
513 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { expect, test } from "vitest"; | ||
import { Translator } from "./translator"; | ||
|
||
test("Translator - allows to set and retrieve current lang", () => { | ||
type Translation = { | ||
hello: string; | ||
}; | ||
const en: Translation = { | ||
hello: "Hello!", | ||
}; | ||
const ru: Translation = { | ||
hello: "Привет", | ||
}; | ||
|
||
const translations = { en, ru }; | ||
type Language = keyof typeof translations; | ||
|
||
const ts = new Translator<Language, Translation>(translations, "en"); | ||
|
||
expect(ts.getLang()).toEqual("en"); | ||
expect(ts.translate("hello")).toBe("Hello!"); | ||
|
||
ts.setLang("ru"); | ||
|
||
expect(ts.getLang()).toEqual("ru"); | ||
expect(ts.translate("hello")).toBe("Привет"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
type Storage<Language extends string, Resource> = { | ||
[key in Language]: Resource; | ||
}; | ||
|
||
type DefaultResource = { [key in string]: string }; | ||
|
||
export class Translator< | ||
Language extends string, | ||
Translation extends DefaultResource, | ||
> { | ||
constructor( | ||
private storage: Storage<Language, Translation>, | ||
private lang: Language, | ||
) {} | ||
|
||
setLang(lang: Language) { | ||
this.lang = lang; | ||
} | ||
|
||
getLang() { | ||
return this.lang; | ||
} | ||
|
||
translate(key: keyof Translation, defaultValue?: string): string { | ||
return this.storage[this.lang][key] ?? defaultValue; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.