From 91478f21fee5373ea80b6720db657ad1b57e03ea Mon Sep 17 00:00:00 2001 From: Jose Felix Date: Mon, 8 Jan 2024 23:37:45 -0400 Subject: [PATCH] feat: add script to add translations inline --- README.md | 0 cms/osmosis-update.json | 3 +- cms/swap-rotating-banner.json | 3 +- cms/top-announcement-banner.json | 74 ++++++++++++++++++++++++++++++++ localizations/en.json | 2 +- localizations/es.json | 2 +- localizations/fa.json | 2 +- localizations/fr.json | 2 +- localizations/ko.json | 2 +- localizations/pl.json | 2 +- localizations/pt-br.json | 2 +- localizations/ro.json | 2 +- localizations/tr.json | 2 +- localizations/zh-cn.json | 2 +- localizations/zh-hk.json | 2 +- localizations/zh-tw.json | 2 +- package.json | 9 ++-- scripts/translate-cms.js | 46 ++++++++++++++++++++ yarn.lock | 29 ++++++++++++- 19 files changed, 170 insertions(+), 18 deletions(-) create mode 100644 README.md create mode 100644 scripts/translate-cms.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/cms/osmosis-update.json b/cms/osmosis-update.json index 377d60b..2dc12c9 100644 --- a/cms/osmosis-update.json +++ b/cms/osmosis-update.json @@ -1,4 +1,5 @@ { "$schema": "../schemas/osmosis-update.schema.json", - "iframeUrl": null + "iframeUrl": null, + "localization": {} } diff --git a/cms/swap-rotating-banner.json b/cms/swap-rotating-banner.json index 74517ca..f37fe6f 100644 --- a/cms/swap-rotating-banner.json +++ b/cms/swap-rotating-banner.json @@ -57,5 +57,6 @@ "arrow_color": "#CBFD50", "featured": true } - ] + ], + "localization": {} } diff --git a/cms/top-announcement-banner.json b/cms/top-announcement-banner.json index ccf9d5f..98c9461 100644 --- a/cms/top-announcement-banner.json +++ b/cms/top-announcement-banner.json @@ -11,5 +11,79 @@ "localStorageKey": "top-announcement-banner-test", "startDate": "2024-01-08T00:00:00.000Z", "endDate": "2024-02-01T00:00:00.000Z" + }, + "localization": { + "en": { + "top-announcement-banner": { + "title": "Testing!", + "linkLearnMore": "Click here to learn more." + } + }, + "es": { + "top-announcement-banner": { + "title": "¡Pruebas!", + "linkLearnMore": "Clic aquí para saber más." + } + }, + "fa": { + "top-announcement-banner": { + "title": "آزمایش کردن!", + "linkLearnMore": "برای اطلاعات بیشتر اینجا کلیک کنید." + } + }, + "fr": { + "top-announcement-banner": { + "title": "Essai!", + "linkLearnMore": "Cliquez ici pour en savoir plus." + } + }, + "ko": { + "top-announcement-banner": { + "title": "테스트!", + "linkLearnMore": "자세한 내용을 보려면 여기를 클릭하세요." + } + }, + "pl": { + "top-announcement-banner": { + "title": "Testowanie!", + "linkLearnMore": "Kliknij tutaj, aby dowiedzieć się więcej." + } + }, + "pt-br": { + "top-announcement-banner": { + "title": "Testando!", + "linkLearnMore": "Clique aqui para saber mais." + } + }, + "ro": { + "top-announcement-banner": { + "title": "Testare!", + "linkLearnMore": "Apasa aici pentru a afla mai multe." + } + }, + "tr": { + "top-announcement-banner": { + "title": "Test yapmak!", + "linkLearnMore": "Daha fazlasını öğrenmek için buraya tıklayın." + } + }, + "zh-cn": { + "top-announcement-banner": { + "title": "测试!", + "linkLearnMore": "点击这里了解更多。" + } + }, + "zh-hk": { + "top-announcement-banner": { + "title": "測試!", + "linkLearnMore": "點這裡了解更多。" + } + }, + "zh-tw": { + "top-announcement-banner": { + "title": "測試!", + "linkLearnMore": "點這裡了解更多。" + } + } } } diff --git a/localizations/en.json b/localizations/en.json index dbf7e5f..fd8859c 100644 --- a/localizations/en.json +++ b/localizations/en.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "Testing!", "linkLearnMore": "Click here to learn more." } diff --git a/localizations/es.json b/localizations/es.json index 605b515..34e44c8 100644 --- a/localizations/es.json +++ b/localizations/es.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "¡Pruebas!", "linkLearnMore": "Clic aquí para saber más." } diff --git a/localizations/fa.json b/localizations/fa.json index 1268b54..cc9a075 100644 --- a/localizations/fa.json +++ b/localizations/fa.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "آزمایش کردن!", "linkLearnMore": "برای اطلاعات بیشتر اینجا کلیک کنید." } diff --git a/localizations/fr.json b/localizations/fr.json index 6e53e4a..576db66 100644 --- a/localizations/fr.json +++ b/localizations/fr.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "Essai!", "linkLearnMore": "Cliquez ici pour en savoir plus." } diff --git a/localizations/ko.json b/localizations/ko.json index f8f49f0..e6b49ba 100644 --- a/localizations/ko.json +++ b/localizations/ko.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "테스트!", "linkLearnMore": "자세한 내용을 보려면 여기를 클릭하세요." } diff --git a/localizations/pl.json b/localizations/pl.json index 6ea30da..cc76fc0 100644 --- a/localizations/pl.json +++ b/localizations/pl.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "Testowanie!", "linkLearnMore": "Kliknij tutaj, aby dowiedzieć się więcej." } diff --git a/localizations/pt-br.json b/localizations/pt-br.json index 6885eef..9eeb100 100644 --- a/localizations/pt-br.json +++ b/localizations/pt-br.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "Testando!", "linkLearnMore": "Clique aqui para saber mais." } diff --git a/localizations/ro.json b/localizations/ro.json index 577e461..af4c880 100644 --- a/localizations/ro.json +++ b/localizations/ro.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "Testare!", "linkLearnMore": "Apasa aici pentru a afla mai multe." } diff --git a/localizations/tr.json b/localizations/tr.json index a2bc7ad..1905c3a 100644 --- a/localizations/tr.json +++ b/localizations/tr.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "Test yapmak!", "linkLearnMore": "Daha fazlasını öğrenmek için buraya tıklayın." } diff --git a/localizations/zh-cn.json b/localizations/zh-cn.json index 2c671c5..142a1e3 100644 --- a/localizations/zh-cn.json +++ b/localizations/zh-cn.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "测试!", "linkLearnMore": "点击这里了解更多。" } diff --git a/localizations/zh-hk.json b/localizations/zh-hk.json index c087cec..a92fb95 100644 --- a/localizations/zh-hk.json +++ b/localizations/zh-hk.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "測試!", "linkLearnMore": "點這裡了解更多。" } diff --git a/localizations/zh-tw.json b/localizations/zh-tw.json index c087cec..a92fb95 100644 --- a/localizations/zh-tw.json +++ b/localizations/zh-tw.json @@ -1,5 +1,5 @@ { - "topAnnouncementBanner": { + "top-announcement-banner": { "title": "測試!", "linkLearnMore": "點這裡了解更多。" } diff --git a/package.json b/package.json index eaa75ad..d0d93f1 100644 --- a/package.json +++ b/package.json @@ -5,16 +5,19 @@ "main": "index.js", "scripts": { "test": "vitest", - "machine-translate": "inlang machine translate --project ./project.inlang" + "machine-translate": "inlang machine translate --project ./project.inlang", + "inline-translations": "tsx ./scripts/translate-cms.js" }, "keywords": [], "author": "", "license": "Apache-2.0", "devDependencies": { + "@inlang/cli": "^2.0.0", "ajv": "^8.12.0", "ajv-cli": "^5.0.0", "ajv-formats": "^2.1.1", - "vitest": "^1.1.3", - "@inlang/cli": "^2.0.0" + "prettier": "^3.1.1", + "tsx": "^4.7.0", + "vitest": "^1.1.3" } } diff --git a/scripts/translate-cms.js b/scripts/translate-cms.js new file mode 100644 index 0000000..655772c --- /dev/null +++ b/scripts/translate-cms.js @@ -0,0 +1,46 @@ +import fs from "fs"; +import path from "path"; +import prettier from "prettier"; + +const cmsDirectoryPath = "./cms"; +const localizationDirectoryPath = "./localizations"; +const cmsFiles = fs.readdirSync(cmsDirectoryPath); + +const inlangSettings = JSON.parse( + fs.readFileSync("project.inlang/settings.json", "utf8") +); + +cmsFiles.forEach(async (filePath) => { + const content = JSON.parse( + fs.readFileSync(path.resolve(cmsDirectoryPath, filePath), "utf8") + ); + + content.localization = inlangSettings.languageTags.reduce( + (acc, languageTag) => { + const localizationFile = JSON.parse( + fs.readFileSync( + path.join(localizationDirectoryPath, `${languageTag}.json`), + "utf8" + ) + ); + + const namespace = filePath.split(".")[0]; + const namespaceContent = localizationFile[namespace]; + + if (!namespaceContent) { + return acc; + } + + acc[languageTag] = { [namespace]: namespaceContent }; + return acc; + }, + {} + ); + + const formatted = await prettier.format(JSON.stringify(content), { + parser: "json", + }); + fs.writeFileSync(path.resolve(cmsDirectoryPath, filePath), formatted, { + encoding: "utf8", + }); +}); diff --git a/yarn.lock b/yarn.lock index 1af92ec..51481c2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -389,7 +389,7 @@ esbuild-wasm@^0.19.2: resolved "https://registry.yarnpkg.com/esbuild-wasm/-/esbuild-wasm-0.19.11.tgz#4ed96cdd1a289bc08432a25fd38b7331d3eac98d" integrity sha512-MIhnpc1TxERUHomteO/ZZHp+kUawGEc03D/8vMHGzffLvbFLeDe6mwxqEZwlqBNY7SLWbyp6bBQAcCen8+wpjQ== -esbuild@^0.19.3: +esbuild@^0.19.3, esbuild@~0.19.10: version "0.19.11" resolved "https://registry.yarnpkg.com/esbuild/-/esbuild-0.19.11.tgz#4a02dca031e768b5556606e1b468fe72e3325d96" integrity sha512-HJ96Hev2hX/6i5cDVwcqiJBBtuo9+FeIJOtZ9W1kA5M6AMJRHUZlpYZ1/SbEwtO0ioNAW8rUooVpC/WehY2SfA== @@ -491,6 +491,13 @@ get-stream@^8.0.1: resolved "https://registry.yarnpkg.com/get-stream/-/get-stream-8.0.1.tgz#def9dfd71742cd7754a7761ed43749a27d02eca2" integrity sha512-VaUJspBffn/LMCJVoMvSAdmscJyS1auj5Zulnn5UoYcY531UWmdwhRWkcGKnGU93m5HSXP9LP2usOryrBtQowA== +get-tsconfig@^4.7.2: + version "4.7.2" + resolved "https://registry.yarnpkg.com/get-tsconfig/-/get-tsconfig-4.7.2.tgz#0dcd6fb330391d46332f4c6c1bf89a6514c2ddce" + integrity sha512-wuMsz4leaj5hbGgg4IvDU0bqJagpftG5l5cXIAvo8uZrqn0NJqwtfupTN00VnkQJPcIRrxYrm1Ue24btpCha2A== + dependencies: + resolve-pkg-maps "^1.0.0" + glob@^7.1.0: version "7.2.3" resolved "https://registry.yarnpkg.com/glob/-/glob-7.2.3.tgz#b8df0fb802bbfa8e89bd1d938b4e16578ed44f2b" @@ -715,6 +722,11 @@ postcss@^8.4.32: picocolors "^1.0.0" source-map-js "^1.0.2" +prettier@^3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.1.1.tgz#6ba9f23165d690b6cbdaa88cb0807278f7019848" + integrity sha512-22UbSzg8luF4UuZtzgiUOfcGM8s4tjBv6dJRT7j275NXsy2jb4aJa4NNveul5x4eqlF1wuhuR2RElK71RvmVaw== + pretty-format@^29.7.0: version "29.7.0" resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-29.7.0.tgz#ca42c758310f365bfa71a0bda0a807160b776812" @@ -739,6 +751,11 @@ require-from-string@^2.0.2: resolved "https://registry.yarnpkg.com/require-from-string/-/require-from-string-2.0.2.tgz#89a7fdd938261267318eafe14f9c32e598c36909" integrity sha512-Xf0nWe6RseziFMu+Ap9biiUbmplq6S9/p+7w7YXP/JBHhrUDDUhwa+vANyubuqfZWTveU//DYVGsDG7RKL/vEw== +resolve-pkg-maps@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/resolve-pkg-maps/-/resolve-pkg-maps-1.0.0.tgz#616b3dc2c57056b5588c31cdf4b3d64db133720f" + integrity sha512-seS2Tj26TBVOC2NIc2rOe2y2ZO7efxITtLZcGSOnHHNOQ7CkiUBfw0Iw2ck6xkIhPwLhKNLS8BO+hEpngQlqzw== + rollup@^4.2.0: version "4.9.3" resolved "https://registry.yarnpkg.com/rollup/-/rollup-4.9.3.tgz#12f54775f359409641b5a71a86fe226a7c1d6b67" @@ -830,6 +847,16 @@ tinyspy@^2.2.0: resolved "https://registry.yarnpkg.com/tinyspy/-/tinyspy-2.2.0.tgz#9dc04b072746520b432f77ea2c2d17933de5d6ce" integrity sha512-d2eda04AN/cPOR89F7Xv5bK/jrQEhmcLFe6HFldoeO9AJtps+fqEnh486vnT/8y4bw38pSyxDcTCAq+Ks2aJTg== +tsx@^4.7.0: + version "4.7.0" + resolved "https://registry.yarnpkg.com/tsx/-/tsx-4.7.0.tgz#1689cfe7dda495ca1f9a66d4cad79cb57b9f6f4a" + integrity sha512-I+t79RYPlEYlHn9a+KzwrvEwhJg35h/1zHsLC2JXvhC2mdynMv6Zxzvhv5EMV6VF5qJlLlkSnMVvdZV3PSIGcg== + dependencies: + esbuild "~0.19.10" + get-tsconfig "^4.7.2" + optionalDependencies: + fsevents "~2.3.3" + type-detect@^4.0.0, type-detect@^4.0.8: version "4.0.8" resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.8.tgz#7646fb5f18871cfbb7749e69bd39a6388eb7450c"