-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add in loadTranslations/clearTranslations to i18n, use stored message…
…s if they exist. (#2277) ## Summary: This is adding in functionality that's similar to LinguiJS, to the wonder-blocks-i18n package, so that we can have a store of translated strings and use them for our translations instead of assuming that the strings will already be translated for us. Issue: FEI-5682 ## Test plan: Tests should pass! I'll be wiring this up in Webapp in a separate PR, once this lands. Author: jeresig Reviewers: jeresig, jandrade Required Reviewers: Approved By: jandrade Checks: ✅ codecov/project, ✅ Chromatic - Get results on regular PRs (ubuntu-latest, 20.x), ✅ Test (ubuntu-latest, 20.x, 2/2), ✅ Check build sizes (ubuntu-latest, 20.x), ✅ Test (ubuntu-latest, 20.x, 1/2), ✅ Lint (ubuntu-latest, 20.x), ✅ Chromatic - Build on regular PRs / chromatic (ubuntu-latest, 20.x), ✅ Publish npm snapshot (ubuntu-latest, 20.x), ⏭️ Chromatic - Skip on Release PR (changesets), ✅ Prime node_modules cache for primary configuration (ubuntu-latest, 20.x), ✅ Check for .changeset entries for all changed files (ubuntu-latest, 20.x), ✅ gerald, ⏭️ dependabot Pull Request URL: #2277
- Loading branch information
Showing
10 changed files
with
417 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@khanacademy/wonder-blocks-i18n": minor | ||
--- | ||
|
||
Add loadTranslations/clearTranslations methods to wonder-blocks-i18n. |
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
147 changes: 147 additions & 0 deletions
147
packages/wonder-blocks-i18n/src/functions/__tests__/i18n-store.test.ts
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,147 @@ | ||
import { | ||
clearTranslations, | ||
getPluralTranslation, | ||
getSingularTranslation, | ||
loadTranslations, | ||
} from "../i18n-store"; | ||
import {setLocale} from "../locale"; | ||
|
||
describe("getSingularTranslation", () => { | ||
const TEST_LOCALE = "en-pt"; | ||
|
||
afterEach(() => { | ||
clearTranslations(TEST_LOCALE); | ||
}); | ||
|
||
it("should return the translated string", () => { | ||
// Arrange | ||
loadTranslations(TEST_LOCALE, { | ||
test: "arrrr matey", | ||
}); | ||
setLocale(TEST_LOCALE); | ||
|
||
// Act | ||
const result = getSingularTranslation("test"); | ||
|
||
// Assert | ||
expect(result).toMatchInlineSnapshot(`"arrrr matey"`); | ||
}); | ||
|
||
it("should return the original string if no translation found", () => { | ||
// Arrange | ||
loadTranslations(TEST_LOCALE, { | ||
test2: "arrrr matey", | ||
}); | ||
setLocale(TEST_LOCALE); | ||
|
||
// Act | ||
const result = getSingularTranslation("test"); | ||
|
||
// Assert | ||
expect(result).toMatchInlineSnapshot(`"test"`); | ||
}); | ||
|
||
it("should return the translated string even if it's plural", () => { | ||
// Arrange | ||
loadTranslations(TEST_LOCALE, { | ||
test: ["arrrr matey", "arrrr mateys"], | ||
}); | ||
setLocale(TEST_LOCALE); | ||
|
||
// Act | ||
const result = getSingularTranslation("test"); | ||
|
||
// Assert | ||
expect(result).toMatchInlineSnapshot(`"arrrr matey"`); | ||
}); | ||
|
||
it("should return a fake translation", () => { | ||
// Arrange | ||
setLocale("boxes"); | ||
|
||
// Act | ||
const result = getSingularTranslation("test"); | ||
|
||
// Assert | ||
expect(result).toMatchInlineSnapshot(`"□□□□"`); | ||
}); | ||
}); | ||
|
||
describe("getPluralTranslation", () => { | ||
const TEST_LOCALE = "en-pt"; | ||
|
||
afterEach(() => { | ||
clearTranslations(TEST_LOCALE); | ||
}); | ||
|
||
it("should return the translated plural singular string", () => { | ||
// Arrange | ||
loadTranslations(TEST_LOCALE, { | ||
test: "arrrr matey", | ||
}); | ||
setLocale(TEST_LOCALE); | ||
|
||
// Act | ||
const result = getPluralTranslation( | ||
{ | ||
lang: TEST_LOCALE, | ||
messages: ["test singular", "test plural"], | ||
}, | ||
0, | ||
); | ||
|
||
// Assert | ||
expect(result).toMatchInlineSnapshot(`"test singular"`); | ||
}); | ||
|
||
it("should return the translated plural string", () => { | ||
// Arrange | ||
loadTranslations(TEST_LOCALE, { | ||
test: "arrrr matey", | ||
}); | ||
setLocale(TEST_LOCALE); | ||
|
||
// Act | ||
const result = getPluralTranslation( | ||
{ | ||
lang: TEST_LOCALE, | ||
messages: ["test singular", "test plural"], | ||
}, | ||
1, | ||
); | ||
|
||
// Assert | ||
expect(result).toMatchInlineSnapshot(`"test plural"`); | ||
}); | ||
|
||
it("should return the original string if no translation found", () => { | ||
// Arrange | ||
loadTranslations(TEST_LOCALE, { | ||
test: "arrrr matey", | ||
}); | ||
setLocale(TEST_LOCALE); | ||
|
||
// Act | ||
const result = getSingularTranslation("test2"); | ||
|
||
// Assert | ||
expect(result).toMatchInlineSnapshot(`"test2"`); | ||
}); | ||
|
||
it("should return a fake translation", () => { | ||
// Arrange | ||
setLocale("boxes"); | ||
|
||
// Act | ||
const result = getPluralTranslation( | ||
{ | ||
lang: TEST_LOCALE, | ||
messages: ["test singular", "test plural"], | ||
}, | ||
0, | ||
); | ||
|
||
// Assert | ||
expect(result).toMatchInlineSnapshot(`"□□□□ □□□□□□□□"`); | ||
}); | ||
}); |
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.