Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FbtTranslations: add support for modifying registered translations #208

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ List of changes for each released npm package version.
<summary>
Unreleased changes that have landed in master. Click to see more.
</summary>

- [feat] Add a new `FbtTranslations.getRegisteredTranslations` function
- [feat] Add a new `FbtTranslations.mergeTranslations` function
</details>

- 0.16.5:
Expand Down
17 changes: 15 additions & 2 deletions runtime/nonfb/FbtTranslations.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {FbtRuntimeCallInput, FbtTranslatedInput} from 'FbtHooks';

const FbtHooks = require('FbtHooks');

let translatedFbts = null;
let translatedFbts: TranslationDict = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


type TranslationStr = string;

Expand All @@ -30,7 +30,7 @@ const FbtTranslations = {
const {args, options} = input;
const hashKey = options?.hk;
const {locale} = FbtHooks.getViewerContext();
const table = translatedFbts?.[locale];
const table = translatedFbts[locale];
if (__DEV__) {
if (!table && locale !== DEFAULT_SRC_LOCALE) {
console.warn('Translations have not been provided');
Expand All @@ -49,6 +49,19 @@ const FbtTranslations = {
registerTranslations(translations: TranslationDict): void {
translatedFbts = translations;
},

getRegisteredTranslations(): TranslationDict {
return translatedFbts;
},

mergeTranslations(newTranslations: TranslationDict): void {
Object.keys(newTranslations).forEach(locale => {
translatedFbts[locale] = Object.assign(
translatedFbts[locale] ?? {},
newTranslations[locale],
);
});
},
};

module.exports = FbtTranslations;
61 changes: 61 additions & 0 deletions runtime/nonfb/__tests__/FbtTranslations-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// @flow

import FbtTranslations from '../FbtTranslations'

it('can register and get back translations', () => {
FbtTranslations.registerTranslations({ 'en_US': { c1: "aaa" } });
expect(FbtTranslations.getRegisteredTranslations()).toMatchInlineSnapshot(`
Object {
"en_US": Object {
"c1": "aaa",
},
}
`);
})

it('merges translations with the same locale as expected', () => {
FbtTranslations.registerTranslations({ 'en_US': { c1: "aaa" } });
FbtTranslations.mergeTranslations({ 'en_US': { c2: "bbb" } });
expect(FbtTranslations.getRegisteredTranslations()).toMatchInlineSnapshot(`
Object {
"en_US": Object {
"c1": "aaa",
"c2": "bbb",
},
}
`);
})

it('merges translations with different locales as expected', () => {
FbtTranslations.registerTranslations({ 'en_US': { c1: "aaa" } });
FbtTranslations.mergeTranslations({
'es_MX': { c1: "bbb" },
'cs_CZ': { c1: "ccc" },
});
expect(FbtTranslations.getRegisteredTranslations()).toMatchInlineSnapshot(`
Object {
"cs_CZ": Object {
"c1": "ccc",
},
"en_US": Object {
"c1": "aaa",
},
"es_MX": Object {
"c1": "bbb",
},
}
`);
})

it('merges translations with the same hash as expected', () => {
FbtTranslations.registerTranslations({ 'en_US': { c1: "aaa", c2: "bbb" } });
FbtTranslations.mergeTranslations({ 'en_US': { c1: "ccc" } });
expect(FbtTranslations.getRegisteredTranslations()).toMatchInlineSnapshot(`
Object {
"en_US": Object {
"c1": "ccc",
"c2": "bbb",
},
}
`);
})