Skip to content

Commit

Permalink
refactor: add locale key renamer
Browse files Browse the repository at this point in the history
  • Loading branch information
GalvinGao committed Feb 18, 2023
1 parent 18fde98 commit 16d68b4
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/components/stats/DataTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@

<span
class="scroll-keyword text--text"
>{{ $t('scroll') }}</span>
>{{ $t('stats.scroll') }}</span>

<v-icon
:size="20"
Expand Down
4 changes: 2 additions & 2 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@
}
},
"name": "Statistics",
"title": "Statistics of {stage}"
"title": "Statistics of {stage}",
"scroll": "Scroll to view details"
},
"contribute": {
"repo": "Project repositories: ",
Expand Down Expand Up @@ -896,7 +897,6 @@
"success": "Website Updated!"
},
"switchedTo": "Server switched to",
"scroll": "Scroll to view details",
"link": {
"docs": {
"title": "For Developers",
Expand Down
4 changes: 2 additions & 2 deletions src/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@
}
},
"name": "統計結果",
"title": "{stage} 統計結果"
"title": "{stage} 統計結果",
"scroll": "左右にスクロールでデータを表示"
},
"contribute": {
"repo": "プロジェクトリポジトリ:",
Expand Down Expand Up @@ -727,7 +728,6 @@
"success": "Website Updated!"
},
"switchedTo": "サーバー切り替え中",
"scroll": "左右にスクロールでデータを表示",
"link": {
"docs": {
"title": "開発者向けのリソース",
Expand Down
4 changes: 2 additions & 2 deletions src/locales/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@
}
},
"name": "통계 결과",
"title": "{stage}의 통계 결과"
"title": "{stage}의 통계 결과",
"scroll": "스크롤로 세부 사항을 볼 수 있습니다"
},
"contribute": {
"repo": "프로젝트 저장소: ",
Expand Down Expand Up @@ -848,7 +849,6 @@
"refresh": "Refresh",
"success": "Website Updated!"
},
"scroll": "스크롤로 세부 사항을 볼 수 있습니다",
"link": {
"docs": {
"title": "개발자용",
Expand Down
4 changes: 2 additions & 2 deletions src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,8 @@
}
},
"name": "统计结果",
"title": "{stage} 统计结果"
"title": "{stage} 统计结果",
"scroll": "左右滑动查看数据"
},
"contribute": {
"repo": "项目仓库:",
Expand Down Expand Up @@ -935,7 +936,6 @@
"success": "更新啦!"
},
"switchedTo": "服务器已切换至",
"scroll": "左右滑动查看数据",
"link": {
"docs": {
"title": "开发者中心",
Expand Down
85 changes: 85 additions & 0 deletions src/scripts/change-key-name-in-locales.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import fs from "fs/promises";

async function main() {
const locales = await fs.readdir("./src/locales");
const messages = await Promise.all(
locales
.filter((el) => el.endsWith(".json"))
.map(async (locale) => ({
locale: locale.replace(".json", ""),
messages: (await import(`../locales/${locale}`)).default,
}))
);

if (process.argv.length < 4) {
console.log(
"Usage: npx esno src/scripts/change-key-name-in-locales.js <from> <to>"
);
return;
}

const from = process.argv[2];
const to = process.argv[3];

// change the key name in all locales
// `from` and `to` can all be in form of a key path, e.g. "foo.bar"
// for example:
// - content = { "foo": { "bar": "baz" } }, from = "foo.bar", to = "foo.baz": content = { "foo": { "baz": "baz" } }
// - content = { "foo": { "bar": "baz" } }, from = "foo.bar", to = "foo": content = { "foo": "baz" }
// - content = { "foo": { "bar": "baz", "baz": "qux" } }, from = "foo.bar", to = "foo.halo": content = { "foo": { "baz": "qux", "halo": "baz" } }
// - content = { "foo": "bar", "bar": { "k0": "v0", "k1": "v1" } }, from = "foo", to = "bar.k2": content = { "bar": { "k0": "v0", "k1": "v1", "k2": "bar" } }

const changeKey = (content, from, to) => {
const fromPath = from.split(".");
const toPath = to.split(".");
const fromKey = fromPath.pop();
const toKey = toPath.pop();

if (fromPath.length) {
content[fromPath[0]] = changeKey(
content[fromPath[0]],
fromPath.slice(1).join("."),
toPath.slice(1).join(".")
);
}

if (toPath.length) {
if (!content[toPath[0]]) {
content[toPath[0]] = {};
}

// content[toPath[0]][toKey] = content[fromPath[0]][fromKey];
// fromPath can be empty, so we need to check it
content[toPath[0]][toKey] = fromPath.length
? content[fromPath[0]][fromKey]
: content[fromKey];
} else {
content[toKey] = content[fromPath[0]][fromKey];
}

// delete content[fromPath[0]][fromKey];
// TypeError: Cannot convert undefined or null to object
// fromPath can be empty, so we need to check it
if (fromPath.length) {
delete content[fromPath[0]][fromKey];
} else {
delete content[fromKey];
}

return content;
};

// fix changeKey function: TypeError: Cannot read properties of undefined (reading 'bar')


messages.forEach(({ locale, messages }) => {
const newMessages = changeKey(messages, from, to);

fs.writeFile(
`./src/locales/${locale}.json`,
JSON.stringify(newMessages, null, 2)
);
});
}

main();

0 comments on commit 16d68b4

Please sign in to comment.