aboutsummaryrefslogtreecommitdiff
path: root/scripts/build-locales-files.mjs
diff options
context:
space:
mode:
authorGravatar Corentin THOMASSET <corentin.thomasset74@gmail.com> 2024-02-01 18:05:05 +0100
committerGravatar GitHub <noreply@github.com> 2024-02-01 17:05:05 +0000
commit7f5fa001473bca4c2a7cf7952c7586546e9da9a8 (patch)
treea3b0930911ce2a7943f3d1d377154d97dba009e2 /scripts/build-locales-files.mjs
parent1334bff30a8ce3fbd269438de774b5569dbee6ef (diff)
downloadit-tools-7f5fa001473bca4c2a7cf7952c7586546e9da9a8.tar.gz
it-tools-7f5fa001473bca4c2a7cf7952c7586546e9da9a8.tar.zst
it-tools-7f5fa001473bca4c2a7cf7952c7586546e9da9a8.zip
feat(i18n): added missing locale files in tools (#863)
Diffstat (limited to 'scripts/build-locales-files.mjs')
-rw-r--r--scripts/build-locales-files.mjs61
1 files changed, 61 insertions, 0 deletions
diff --git a/scripts/build-locales-files.mjs b/scripts/build-locales-files.mjs
new file mode 100644
index 0000000..db1483d
--- /dev/null
+++ b/scripts/build-locales-files.mjs
@@ -0,0 +1,61 @@
+import { existsSync, writeFileSync } from 'node:fs';
+import { Glob } from 'bun';
+import _ from 'lodash';
+
+async function getPathsFromGlobs({ patterns, onlyFiles = true }) {
+ const filePaths = [];
+
+ for (const pattern of patterns) {
+ const glob = new Glob(pattern);
+
+ for await (const filePath of glob.scan({ onlyFiles, cwd: '.' })) {
+ filePaths.push(filePath);
+ }
+ }
+
+ return { filePaths };
+}
+
+function getLocaleKey({ filePath }) {
+ const fileName = filePath.split('/').pop();
+ return fileName.replace(/\.yml$/, '');
+}
+
+async function createMissingLocaleFile({ localeKey }) {
+ const fileName = `${localeKey}.yml`;
+
+ const { filePaths: localesDirs } = await getPathsFromGlobs({
+ patterns: [
+ 'locales',
+ 'src/tools/*/locales',
+ ],
+ onlyFiles: false,
+ });
+
+ for (const localesDir of localesDirs) {
+ const filePath = `${localesDir}/${fileName}`;
+
+ if (existsSync(filePath)) {
+ console.log(`Locale file already exists: ${filePath}`);
+ continue;
+ }
+
+ console.log(`Creating missing locale file: ${filePath}`);
+ writeFileSync(filePath, '', 'utf8');
+ }
+}
+
+const { filePaths } = await getPathsFromGlobs({
+ patterns: [
+ 'locales/*.yml',
+ 'src/tools/*/locales/*.yml',
+ ],
+});
+
+await Promise.all(
+ _.chain(filePaths)
+ .map(filePath => getLocaleKey({ filePath }))
+ .uniq()
+ .map(localeKey => createMissingLocaleFile({ localeKey }))
+ .value(),
+);