Skip to content

Commit

Permalink
FbtTranslations: add support for modifying registered translations (#208
Browse files Browse the repository at this point in the history
)

Summary:
This change adds a new function `getRegisteredTranslations` into `FbtTranslations` which allows us to retrieve registered translations. For convenience, it also adds a new `mergeTranslations` function that allows us to append the translations.

Use-case: I have an application with FBT and a design system with its own FBT as well. Unfortunately, registering translations overwrites any translations previously registered so the design system (or the application) cannot register their own translations without conflicting with the other part. One solution could be to get already registered translations and re-register them modified. Another solution is to merge these two translations together.

There are many other possible solutions so I am open to suggestions how to make it better.

Pull Request resolved: #208

Test Plan:
```
$ yarn jest Translations
Using Yarn from /data/users/jwatson/www/scripts/third-party/yarn/yarn
yarn run v1.9.0-20180706.1003
$ /data/users/jwatson/www/scripts/intl/js/oss-fbt/__github__/node_modules/.bin/jest Translations
 PASS   fbt-runtime  packages/fbt/lib/__tests__/FbtTranslations-test.js
  ✓ can register and get back translations (8ms)
  ✓ merges translations with the same locale as expected (3ms)
  ✓ merges translations with different locales as expected (1ms)
  ✓ merges translations with the same hash as expected (1ms)

Test Suites: 1 passed, 1 total
Tests:       4 passed, 4 total
Snapshots:   4 passed, 4 total
Time:        1.383s
Ran all test suites matching /Translations/i.
Done in 2.38s.
```

```
$ yarn 'flow:check'
Using Yarn from /data/users/jwatson/www/scripts/third-party/yarn/yarn
yarn run v1.9.0-20180706.1003
$ flow check --show-all-errors
Found 0 errors
Done in 10.50s.
```

Reviewed By: kayhadrin

Differential Revision: D27741516

Pulled By: jrwats

fbshipit-source-id: 5b89a48157965cf69611e8a004666d6c0daac359
  • Loading branch information
mrtnzlml authored and facebook-github-bot committed Apr 15, 2021
1 parent 05eab46 commit d05d44f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 2 deletions.
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 = {};

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;
68 changes: 68 additions & 0 deletions runtime/nonfb/__tests__/FbtTranslations-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* (c) Facebook, Inc. and its affiliates. Confidential and proprietary.
*
* @flow
* @emails oncall+i18n_fbt_oss
*/

import FbtTranslations from '../FbtTranslations';

describe('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",
},
}
`);
});
});

0 comments on commit d05d44f

Please sign in to comment.