Skip to content

Commit

Permalink
refactor: split localizations in namespaces (#116)
Browse files Browse the repository at this point in the history
* refactor: split localizations in namespaces

* fix: tests

* chore: update crowdin configs

* fix: test
  • Loading branch information
imnaiyar authored Nov 5, 2024
1 parent 37566d4 commit 146148a
Show file tree
Hide file tree
Showing 95 changed files with 2,310 additions and 19,599 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/crowdin-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
uses: crowdin/github-action@v2
with:
upload_sources: true
upload_translations: false
upload_translations: true
download_translations: true
localization_branch_name: crowdin_translate
create_pull_request: true
Expand Down
10 changes: 5 additions & 5 deletions __tests__/getTranslator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,29 @@ import { getTranslator } from "../src/bot/i18n.js";
describe("getTranslator", () => {
it("should return the translated string for a given key and language", () => {
const translator = getTranslator("en-US");
const translation = translator("common.bot.intro");
const translation = translator("common:bot.intro");

expect(translation).toMatch("That's me...");
});

it("should throw an error if translation key is not found", () => {
const translator = getTranslator("en-US");
// @ts-expect-error
expect(() => translator("common.nonexistentKey")).toThrow("Translation key invalid: common.nonexistentKey");
expect(() => translator("common:nonexistentKey")).toThrow("Translation key invalid: nonexistentKey");
});

it("should return the translated string with interpolation values", () => {
const translator = getTranslator("en-US");
const translatedString = translator("common.errors.ERROR_ID", { ID: "123456" });
const translatedString = translator("errors:ERROR_ID", { ID: "123456" });

expect(translatedString).toBe("Error ID: `123456`");
});

it("should return the translated string for a different language", () => {
const translatorHi = getTranslator("hi");
const translatorRu = getTranslator("ru");
const translatedStringHi = translatorHi("common.bot.intro");
const translatedStringRu = translatorRu("common.bot.intro");
const translatedStringHi = translatorHi("common:bot.intro");
const translatedStringRu = translatorRu("common:bot.intro");

expect(translatedStringHi).toMatch("यह में हूं...");
expect(translatedStringRu).toMatch("Это я...");
Expand Down
6 changes: 3 additions & 3 deletions __tests__/useTranslation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ type Localizations = Partial<Record<(typeof l)[number], string>>;

describe("useTranslations", () => {
it("should return the correct translation for a given key", () => {
const translations = useTranslations("common.bot.intro");
const translations = useTranslations("common:bot.intro");

const translationsTyped: Localizations = translations;
expect(translationsTyped).toBeDefined();
Expand All @@ -19,15 +19,15 @@ describe("useTranslations", () => {

it("should return an empty object if the translation key is not found", () => {
// @ts-expect-error
const translations = useTranslations("common.nonexistentKey");
const translations = useTranslations("common:nonexistentKey");

expect(translations).toEqual({});
expect(Object.keys(translations).length).toBe(0);
});

it("should throw an error if the translation value is not a string", () => {
expect(() => {
useTranslations("common.bot");
useTranslations("common:bot");
}).toThrow(TypeError);
});
});
4 changes: 2 additions & 2 deletions crowdin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ pull_request_title: "chore: new translations [crowdin]"
commit_message: "chore: translate to %language%"
append_commit_message: false
files:
- source: /locales/en-US.json
translation: /locales/%two_letters_code%.json
- source: /locales/en-US/*.json
translation: /locales/%two_letters_code%/%original_file_name%
preserve_translations: true

pre_translate:
Expand Down
Loading

0 comments on commit 146148a

Please sign in to comment.