Last active
May 10, 2022 07:32
-
-
Save BANG88/dd4fcaf46d43fb4944cf1f624310ca4a to your computer and use it in GitHub Desktop.
Sync missing key to other locales after formatjs's extraction have done. these locales are come from next.config.js.
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
# generated by patch-package 6.4.10 | |
# | |
# command: | |
# npx patch-package @formatjs/cli | |
# | |
# declared package: | |
# @formatjs/cli: ^4.8.4 | |
# | |
diff --git a/node_modules/@formatjs/cli/src/extract.js b/node_modules/@formatjs/cli/src/extract.js | |
index 5f9095e..73dd043 100755 | |
--- a/node_modules/@formatjs/cli/src/extract.js | |
+++ b/node_modules/@formatjs/cli/src/extract.js | |
@@ -166,6 +166,8 @@ function extract(files, extractOpts) { | |
} | |
results[id] = msg; | |
} | |
+ const runner = require(process.cwd() + '/scripts/extract.js') | |
+ runner(results) | |
return [2 /*return*/, (0, json_stable_stringify_1.default)(formatter.format(results), { | |
space: 2, | |
cmp: formatter.compareMessages || undefined, |
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
{ | |
"presets": ["next/babel"], | |
"plugins": [ | |
[ | |
"formatjs", | |
{ | |
"idInterpolationPattern": "[sha512:contenthash:base64:6]", | |
"ast": true | |
} | |
] | |
] | |
} |
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
const fs = require("fs-extra"); | |
const nextConfig = require("../next.config.js"); | |
const Opencc = require("opencc"); | |
const converter = new Opencc("s2twp.json"); | |
const debug = require("debug")("extract"); | |
/** | |
* Extracts the content of the given file to the given directory. | |
* @param {{[key: string]:{ defaultMessage: 'string'}}} results | |
*/ | |
module.exports = function (results) { | |
const { | |
i18n: { locales }, | |
} = nextConfig; | |
const defaultLocale = "zh-CN"; | |
debug("extracting results %o ", results); | |
//content/locales/en.json | |
locales | |
.filter((x) => x !== defaultLocale) | |
.forEach((locale) => { | |
debug("\r\nhandle locale: %s", locale); | |
const localePath = `content/locales/${locale}.json`; | |
let localeContent = {}; | |
const isTW = locale === "zh-TW"; | |
if (!isTW && fs.existsSync(localePath)) { | |
localeContent = fs.readJsonSync(localePath); | |
debug("has content %o", localeContent); | |
} | |
debug("is zh-TW: %s", isTW); | |
for (const key in results) { | |
if (!localeContent[key]) { | |
const text = results[key].defaultMessage; | |
localeContent[key] = isTW ? converter.convertSync(text) : text; | |
} | |
} | |
debug("new content %o", localeContent); | |
fs.writeJsonSync(localePath, localeContent, { | |
spaces: 2, | |
replacer: null, | |
}); | |
}); | |
}; |
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
/** @type {import('next').NextConfig} */ | |
const nextConfig = { | |
reactStrictMode: true, | |
i18n: { | |
locales: ["en", "zh-CN", "zh-TW"], | |
defaultLocale: "en", | |
}, | |
}; | |
module.exports = nextConfig; |
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
{ | |
"name": "may-nine", | |
"version": "0.1.0", | |
"private": true, | |
"scripts": { | |
"dev": "next dev", | |
"build": "next build", | |
"start": "next start", | |
"lint": "next lint", | |
"test:e2e": "playwright test", | |
"extract:i18n": "DEBUG=* formatjs extract '{pages,components,sections}/**/*.{js,ts,tsx}' --format simple --id-interpolation-pattern '[sha512:contenthash:base64:6]' --out-file content/locales/zh-CN.json", | |
"compile:i18n": "formatjs compile-folder --ast --format simple content/locales content/compiled-locales", | |
"i18n": "npm run extract:i18n && npm run compile:i18n", | |
"postinstall": "patch-package" | |
}, | |
"dependencies": { | |
"@mantine/core": "^4.2.4", | |
"@mantine/hooks": "^4.2.4", | |
"@mantine/next": "^4.2.4", | |
"next": "12.1.6", | |
"react": "18.1.0", | |
"react-dom": "18.1.0", | |
"react-intl": "^5.25.1", | |
"swr": "^1.3.0" | |
}, | |
"devDependencies": { | |
"@formatjs/cli": "^4.8.4", | |
"@playwright/test": "^1.21.1", | |
"@types/node": "17.0.31", | |
"@types/react": "18.0.9", | |
"@types/react-dom": "18.0.3", | |
"babel-plugin-formatjs": "^10.3.20", | |
"eslint": "8.15.0", | |
"eslint-config-next": "12.1.6", | |
"opencc": "^1.1.3", | |
"patch-package": "^6.4.7", | |
"typescript": "4.6.4" | |
} | |
} |
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
# create a patch for pnpm | |
npx @milahu/patch-package-with-pnpm-support @formatjs/cli | |
# extract and compile i18n | |
pnpm run i18n | |
# I use opencc to convert Chinese Simplified to Chinese Traditional (use Taiwan's configuration for better translations.) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment