Skip to content

Commit

Permalink
feat: added dict management script
Browse files Browse the repository at this point in the history
  • Loading branch information
ImJustChew committed Aug 19, 2024
1 parent c263ec0 commit 812534e
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ You can clone the repository and start the development server via `npm run dev`

If you wish to participate in this development, feel free to email [nthumods@gmail.com](mailto:nthumods@gmail.com) in the meantime while we figure out the system for contributing.

Translation Dictionary Management Script `npm run dict --` to manage the translation dictionary. Eg `npm run dict -- create settings.ais.login 登入 Login`

## Contributing

We welcome contributions from everyone, regardless of experience level. Here’s how you can get started:
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"ionic:serve": "npm run start",
"sync:ios": "npx cap sync ios",
"prepare": "husky",
"format:all": "prettier --write ."
"format:all": "prettier --write .",
"dict": "node --import=tsimp/import ./scripts/dict.ts"
},
"dependencies": {
"@capacitor-community/media": "6.0.0",
Expand Down
197 changes: 197 additions & 0 deletions scripts/dict.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
/**
* @fileoverview
* This script is a dev manager for dictionary files.
* It is used to manage the dictionary files in the `src/dictionaries` directory.
* Use this script to create, update, and delete dictionary items.
*/

import fs from "fs";
import path from "path";
import { Command } from "commander";

const program = new Command();
program.version("0.0.1");

// create a new key in the dictionary, requires a key, zh, and en
program
.command("create <key> <zh> <en>")
.description("Create a new dictionary item")
.alias("c")
.action((key: string, zh: string, en: string) => {
const enPath = path.resolve(__dirname, "../src/dictionaries/en.json");
const zhPath = path.resolve(__dirname, "../src/dictionaries/zh.json");

const enDict = require(enPath);
const zhDict = require(zhPath);

const keys = key.split(".") ?? [];
if (keys.length === 0) {
console.error("Key not be empty and is traversed by .");
process.exit(1);
}
// if keys starts with dict, remove it
if (keys[0] === "dict") keys.shift();
const lastKey = keys.pop();

if (!lastKey) {
console.error("Key should not be empty");
process.exit(1);
}

let enPointer = enDict;
let zhPointer = zhDict;

keys.forEach((k) => {
if (!enPointer[k]) enPointer[k] = {};
if (!zhPointer[k]) zhPointer[k] = {};

enPointer = enPointer[k];
zhPointer = zhPointer[k];
});

function write() {}

// check if key already exists, confirm overwrite
if (enPointer[lastKey] || zhPointer[lastKey])
console.log("Key already exists, overwritting");
enPointer[lastKey!] = en;
zhPointer[lastKey!] = zh;

fs.writeFileSync(enPath, JSON.stringify(enDict, null, 2));
fs.writeFileSync(zhPath, JSON.stringify(zhDict, null, 2));

console.log("Dictionary item created successfully");
});

// remove a key from the dictionary
program
.command("remove <key>")
.description("Remove a dictionary item")
.alias("r")
.action((key: string) => {
const enPath = path.resolve(__dirname, "../src/dictionaries/en.json");
const zhPath = path.resolve(__dirname, "../src/dictionaries/zh.json");

const enDict = require(enPath);
const zhDict = require(zhPath);

const keys = key.split(".") ?? [];
if (keys.length === 0) {
console.error("Key not be empty and is traversed by .");
process.exit(1);
}
// if keys starts with dict, remove it
if (keys[0] === "dict") keys.shift();
const lastKey = keys.pop();

if (!lastKey) {
console.error("Key should not be empty");
process.exit(1);
}

let enPointer = enDict;
let zhPointer = zhDict;

keys.forEach((k) => {
if (!enPointer[k]) enPointer[k] = {};
if (!zhPointer[k]) zhPointer[k] = {};

enPointer = enPointer[k];
zhPointer = zhPointer[k];
});

if (!enPointer[lastKey] || !zhPointer[lastKey]) {
console.error("Key does not exist");
process.exit(1);
}

delete enPointer[lastKey!];
delete zhPointer[lastKey!];

fs.writeFileSync(enPath, JSON.stringify(enDict, null, 2));
fs.writeFileSync(zhPath, JSON.stringify(zhDict, null, 2));

console.log("Dictionary item removed successfully");
});

// move a key in the dictionary
program
.command("move <key> <newKey>")
.description("Move a dictionary item")
.alias("m")
.action((key: string, newKey: string) => {
const enPath = path.resolve(__dirname, "../src/dictionaries/en.json");
const zhPath = path.resolve(__dirname, "../src/dictionaries/zh.json");

const enDict = require(enPath);
const zhDict = require(zhPath);

const keys = key.split(".") ?? [];
if (keys.length === 0) {
console.error("Key not be empty and is traversed by .");
process.exit(1);
}
// if keys starts with dict, remove it
if (keys[0] === "dict") keys.shift();
const lastKey = keys.pop();

if (!lastKey) {
console.error("Key should not be empty");
process.exit(1);
}

let enPointer = enDict;
let zhPointer = zhDict;

keys.forEach((k) => {
if (!enPointer[k]) enPointer[k] = {};
if (!zhPointer[k]) zhPointer[k] = {};

enPointer = enPointer[k];
zhPointer = zhPointer[k];
});

if (!enPointer[lastKey] || !zhPointer[lastKey]) {
console.error("Key does not exist");
process.exit(1);
}

// copy the key to the new location, remove the original key
let enNewPointer = enDict;
let zhNewPointer = zhDict;

const newKeys = newKey.split(".") ?? [];
if (keys.length === 0) {
console.error("Key not be empty and is traversed by .");
process.exit(1);
}
// if keys starts with dict, remove it
if (newKeys[0] === "dict") newKeys.shift();
const newLastKey = newKeys.pop();

if (!newLastKey) {
console.error("Key should not be empty");
process.exit(1);
}

newKeys.forEach((k) => {
if (!enNewPointer[k]) enNewPointer[k] = {};
if (!zhNewPointer[k]) zhNewPointer[k] = {};

enNewPointer = enNewPointer[k];
zhNewPointer = zhNewPointer[k];
});

enNewPointer[newLastKey!] = enPointer[lastKey];
zhNewPointer[newLastKey!] = zhPointer[lastKey];

delete enPointer[lastKey!];
delete zhPointer[lastKey!];

fs.writeFileSync(enPath, JSON.stringify(enDict, null, 2));
fs.writeFileSync(zhPath, JSON.stringify(zhDict, null, 2));

console.log("Dictionary item moved successfully");
});

program.parse();

0 comments on commit 812534e

Please sign in to comment.