Skip to content

Commit

Permalink
refactor(i18n): migrate to structured json format (#2747)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmenko authored Mar 19, 2024
1 parent 4d184f6 commit ad7c97e
Show file tree
Hide file tree
Showing 13 changed files with 2,083 additions and 545 deletions.
11 changes: 9 additions & 2 deletions docs/.storybook/configs/intl-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ const namifyLocale = (locale) => {

const IntlWrapper = (props) => {
const locale = props.locale;
const messages = getMessagesForLocale(locale);
const messagesForLocale = getMessagesForLocale(locale);
const normalizedMessages = Object.entries(messagesForLocale).reduce(
(messages, [messageKey, messageValue]) => ({
...messages,
[messageKey]: messageValue.string ?? messageValue,
}),
{}
);
return (
<IntlProvider locale={locale} messages={messages}>
<IntlProvider locale={locale} messages={normalizedMessages}>
{props.children}
</IntlProvider>
);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"build": "./scripts/build.sh",
"build:watch": "./scripts/build_watch.sh",
"start": "yarn --cwd docs start",
"extract-intl": "formatjs extract --format=./packages/i18n/transifex-transformer.js --out-file=./packages/i18n/data/core.json '{packages,src/components}/**/messages.ts'",
"extract-intl": "formatjs extract --format=transifex --out-file=./packages/i18n/data/core.json '{packages,src/components}/**/messages.ts'",
"compile-intl": "yarn workspace @commercetools-uikit/i18n compile-data",
"lint": "jest --projects jest.eslint.config.js",
"lint:js": "jest --config jest.eslint.config.js",
Expand Down
365 changes: 292 additions & 73 deletions packages/i18n/data/core.json

Large diffs are not rendered by default.

367 changes: 293 additions & 74 deletions packages/i18n/data/de.json

Large diffs are not rendered by default.

365 changes: 292 additions & 73 deletions packages/i18n/data/en.json

Large diffs are not rendered by default.

367 changes: 293 additions & 74 deletions packages/i18n/data/es.json

Large diffs are not rendered by default.

367 changes: 293 additions & 74 deletions packages/i18n/data/fr-FR.json

Large diffs are not rendered by default.

365 changes: 292 additions & 73 deletions packages/i18n/data/pt-BR.json

Large diffs are not rendered by default.

367 changes: 293 additions & 74 deletions packages/i18n/data/zh-CN.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"module": "dist/commercetools-uikit-i18n.esm.js",
"files": ["dist", "data", "compiled-data"],
"scripts": {
"compile-data": "formatjs compile-folder --format=./transifex-transformer.js --ast data compiled-data"
"compile-data": "formatjs compile-folder --format=./transifex-compiler.js --ast data compiled-data"
},
"dependencies": {
"@babel/runtime": "^7.20.13",
Expand Down
23 changes: 23 additions & 0 deletions packages/i18n/transifex-compiler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// NOTE: this should not be needed anymore once we fully migrated to STRUCTURED_JSON format.
exports.compile = function compile(msgs) {
const normalizedMessages = Object.entries(msgs).reduce(
(messages, [id, msg]) => {
// Key/Value JSON format
if (typeof msg === 'string') {
return {
...messages,
[id]: msg,
};
}

// Structured JSON format
return {
...messages,
[id]: msg.string,
};
},
{}
);

return normalizedMessages;
};
25 changes: 0 additions & 25 deletions packages/i18n/transifex-transformer.js

This file was deleted.

2 changes: 1 addition & 1 deletion transifex.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
git:
filters:
- filter_type: file
file_format: KEYVALUEJSON
file_format: STRUCTURED_JSON
source_language: en_US
source_file: packages/i18n/data/core.json
translation_files_expression: packages/i18n/data/<lang>.json
Expand Down

0 comments on commit ad7c97e

Please sign in to comment.