Skip to content

Commit

Permalink
refactor: rename locale keys
Browse files Browse the repository at this point in the history
  • Loading branch information
GalvinGao committed Feb 18, 2023
1 parent 9cb19f3 commit d09b9f0
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 132 deletions.
27 changes: 13 additions & 14 deletions src/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -918,22 +918,21 @@
},
"home": {
"quickStart": {
"title": "Quick Start",
"caption": "Click the\nbutton to expand menu and see more features"
"title": {
"report": "Report Drops",
"byItem": "Item Drop Rates",
"byStage": "Stage Drop Rates",
"planner": "Farming Planner"
},
"caption": "Click the\nbutton to expand menu and see more features",
"subtitle": {
"report": "I wanna report drops",
"byItem": "Where should I farm rocks?",
"byStage": "What are the drop rates of this stage?",
"planner": "Please gimme some farming advice"
}
}
},
"subtitle": {
"report": "I wanna report drops",
"byItem": "Where should I farm rocks?",
"byStage": "What are the drop rates of this stage?",
"planner": "Please gimme some farming advice"
},
"title": {
"report": "Report Drops",
"byItem": "Item Drop Rates",
"byStage": "Stage Drop Rates",
"planner": "Farming Planner"
},
"links": {
"tags": {
"apRanking": "AP Efficiency Ranking",
Expand Down
27 changes: 13 additions & 14 deletions src/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -748,22 +748,21 @@
},
"home": {
"quickStart": {
"title": "クイックスタート",
"caption": "左上の\nボタンをクリックしてメニューを展開すると、さらに多くの機能が表示されます。"
"title": {
"report": "ドロップ報告",
"byItem": "素材ドロップ率",
"byStage": "作戦ドロップ率",
"planner": "周回計画"
},
"caption": "左上の\nボタンをクリックしてメニューを展開すると、さらに多くの機能が表示されます。",
"subtitle": {
"report": "ドロップを報告したい",
"byItem": "岩ってどこ回ったらいいのかな",
"byStage": "1-7の泥率ってどれくらい?",
"planner": "周回アドバイスをください"
}
}
},
"subtitle": {
"report": "ドロップを報告したい",
"byItem": "岩ってどこ回ったらいいのかな",
"byStage": "1-7の泥率ってどれくらい?",
"planner": "周回アドバイスをください"
},
"title": {
"report": "ドロップ報告",
"byItem": "素材ドロップ率",
"byStage": "作戦ドロップ率",
"planner": "周回計画"
},
"links": {
"tags": {
"apRanking": "理性効率順位表",
Expand Down
27 changes: 13 additions & 14 deletions src/locales/ko.json
Original file line number Diff line number Diff line change
Expand Up @@ -869,22 +869,21 @@
},
"home": {
"quickStart": {
"title": "빠른 시작",
"caption": "\n버튼을 클릭해 더 많은 기능을 확인하세요"
"title": {
"report": "보고서 작성",
"byItem": "아이템별 드랍률",
"byStage": "스테이지별 드랍률",
"planner": "파밍 플래너"
},
"caption": "\n버튼을 클릭해 더 많은 기능을 확인하세요",
"subtitle": {
"report": "보고서를 작성하고 싶어요",
"byItem": "원암은 어디서 캐나요?",
"byStage": "이 스테이지의 드랍률은 어떤가요?",
"planner": "아이템 파밍에 도움이 필요해요"
}
}
},
"subtitle": {
"report": "보고서를 작성하고 싶어요",
"byItem": "원암은 어디서 캐나요?",
"byStage": "이 스테이지의 드랍률은 어떤가요?",
"planner": "아이템 파밍에 도움이 필요해요"
},
"title": {
"report": "보고서 작성",
"byItem": "아이템별 드랍률",
"byStage": "스테이지별 드랍률",
"planner": "파밍 플래너"
},
"links": {
"tags": {
"apRanking": "이성 효율 순위",
Expand Down
29 changes: 14 additions & 15 deletions src/locales/zh.json
Original file line number Diff line number Diff line change
Expand Up @@ -957,23 +957,22 @@
},
"home": {
"quickStart": {
"title": "快速上手",
"caption": "点击左上角\n按钮展开菜单以查看更多功能"
"title": {
"report": "掉落汇报",
"byItem": "素材掉率",
"byStage": "作战掉率",
"planner": "刷图规划"
},
"caption": "点击左上角\n按钮展开菜单以查看更多功能",
"subtitle": {
"report": "我要汇报掉落",
"byItem": "我该去哪刷石头?",
"byStage": "这图掉率如何?",
"search": "光速直达统计结果",
"planner": "请给我刷图建议"
}
}
},
"subtitle": {
"report": "我要汇报掉落",
"byItem": "我该去哪刷石头?",
"byStage": "这图掉率如何?",
"search": "光速直达统计结果",
"planner": "请给我刷图建议"
},
"title": {
"report": "掉落汇报",
"byItem": "素材掉率",
"byStage": "作战掉率",
"planner": "刷图规划"
},
"links": {
"tags": {
"apRanking": "理智效率排行",
Expand Down
107 changes: 40 additions & 67 deletions src/scripts/change-key-name-in-locales.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,73 +21,6 @@ async function main() {
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" } }
// notice that, from and to may have 1 to many levels of nesting, and they may have different levels of nesting as well

function changeKey(content, from, to) {
if (typeof content !== "object") {
return content;
}

if (Array.isArray(content)) {
return content.map((el) => changeKey(el, from, to));
}

const fromParts = from.split(".");
const toParts = to.split(".");

const fromKey = fromParts[0];
const toKey = toParts[0];

if (fromParts.length === 1 && toParts.length === 1) {
if (fromKey in content) {
const fromValue = content[fromKey];
delete content[fromKey];

content[toKey] = fromValue;
}

return content;
}

if (fromKey in content) {
const fromValue = content[fromKey];

if (fromParts.length > 1) {
content[fromKey] = changeKey(
fromValue,
fromParts.slice(1).join("."),
to
);
} else {
content[fromKey] = changeKey(
fromValue,
from,
toParts.slice(1).join(".")
);
}
}

if (toKey in content) {
const toValue = content[toKey];

if (toParts.length > 1) {
content[toKey] = changeKey(toValue, from, toParts.slice(1).join("."));
} else {
content[toKey] = changeKey(toValue, fromParts.slice(1).join("."), to);
}
}

return content;
}


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

Expand All @@ -99,3 +32,43 @@ async function main() {
}

main();

// changeKey shall change the key name in all messages.
// `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" } }
// notice that: `from` and `to` may have 1 to many levels of nesting, and they may have different levels of nesting as well
// also, the `to` key path may or may not exist in the content. If it does not exist, it shall be created as needed, nested as deep as needed
// the solution should involve recursion
// the content is a plain object, not a Map
// the solution should be generic, not specific to the example above
function changeKey(content, from, to) {
const fromKeys = from.split(".");
const toKeys = to.split(".");

if (fromKeys.length === 1) {
const value = content[fromKeys[0]];
delete content[fromKeys[0]];

if (toKeys.length === 1) {
content[toKeys[0]] = value;
} else {
let current = content;
for (let i = 0; i < toKeys.length - 1; i++) {
if (!current[toKeys[i]]) {
current[toKeys[i]] = {};
}
current = current[toKeys[i]];
}
current[toKeys[toKeys.length - 1]] = value;
}

return content;
}

changeKey(content[fromKeys[0]], fromKeys.slice(1).join("."), to);
return content;
}
16 changes: 8 additions & 8 deletions src/views/About/GettingStarted.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,26 @@ export default {
{
route: 'StatsByItem',
icon: 'mdi-treasure-chest',
title: 'title.byItem',
subtitle: 'subtitle.byItem'
title: 'home.quickStart.title.byItem',
subtitle: 'home.quickStart.subtitle.byItem'
},
{
route: 'StatsByStage',
icon: 'mdi-cube',
title: 'title.byStage',
subtitle: 'subtitle.byStage'
title: 'home.quickStart.title.byStage',
subtitle: 'home.quickStart.subtitle.byStage'
},
{
route: 'ReportByZone',
icon: 'mdi-upload',
title: 'title.report',
subtitle: 'subtitle.report'
title: 'home.quickStart.title.report',
subtitle: 'home.quickStart.subtitle.report'
},
{
route: 'Planner',
icon: 'mdi-directions-fork',
title: 'title.planner',
subtitle: 'subtitle.planner'
title: 'home.quickStart.title.planner',
subtitle: 'home.quickStart.subtitle.planner'
},
]
}
Expand Down

0 comments on commit d09b9f0

Please sign in to comment.