Skip to content

Commit

Permalink
Merge pull request #36 from melfore/34-i18n-msg-input-file-language-d…
Browse files Browse the repository at this point in the history
…uplication

Add duplicate lang
  • Loading branch information
CrisGrud authored Dec 21, 2023
2 parents 6c9fa88 + 2d4a6f6 commit 84d24e5
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .clibelt.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"i18nMsg": {
"input": "./test/resources/messages-all-langs",
"input": "./test/resources/messages-all-langs.js",
"outMsg": "./test/out",
"outTemp": "./test/out/temp",
"outTs": "./test/out/i18nMessages.ts"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ jobs:
git commit -m "Release Ops" -a
- name: Increase Version
run: npm version patch
run: npm version major

- name: Push Updates
uses: ad-m/github-push-action@master
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Add required CLIs to your `package.json`, for example:
```json
"scripts": {
"i18n": "i18n-msg",
"i18n:copy": "i18n-copy",
"pwd2hash": "pwd2hash"
}
```
Expand All @@ -34,7 +35,7 @@ Configuration example:
```json
{
"i18nMsg": {
"input": "./resources/messages-all-langs",
"input": "./resources/messages-all-langs.js",
"outMsg": "./src/messages",
"outTs": "./src/messages/i18nMessages.ts"
}
Expand All @@ -48,6 +49,7 @@ Add arguments to script in `package.json`, for example:
```json
"scripts": {
"i18n": "i18n-msg --input new/path --outTs new/constants.ts",
"i18n:copy": "i18n-copy --from=it --to=fr",
}
```

Expand Down Expand Up @@ -81,7 +83,7 @@ Example:
```json
{
"i18nMsg": {
"input": "./resources/messages-all-langs",
"input": "./resources/messages-all-langs.js",
"outMsg": "./src/messages",
"outTs": "./src/messages/i18nMessages.ts"
}
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
],
"bin": {
"pwd2hash": "dist/cli/pwd2hash/index.js",
"i18n-msg": "dist/cli/i18n-msg/index.js"
"i18n-msg": "dist/cli/i18n-msg/index.js",
"i18n-copy": "dist/cli/i18n-copy/index.js"
},
"engines": {
"node": ">=18 <19",
Expand All @@ -33,7 +34,8 @@
"prettify": "prettier --write .",
"preversion": "npm run lintify",
"version": "npm run prettify",
"upload": "npm run build && npm publish --access public"
"upload": "npm run build && npm publish --access public",
"i18n:copy": "ts-node src/cli/i18n-copy/index.ts"
},
"dependencies": {
"convict": "^6.2.4",
Expand Down
94 changes: 94 additions & 0 deletions src/cli/i18n-copy/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const fs = require("fs");
import config from "../../config";
import path from "path";
import * as prettier from "prettier";

interface Translation {
l: string;
v: string;
}

interface CustomerOverride {
customer: string;
tr: Translation[];
}

interface Message {
code: string;
tr: Translation[];
overrides?: CustomerOverride[];
}

//checked single file
const checkFile = async (inputPath: string, from: string, to: string) => {
const fileContent = require(inputPath);
let addValue = "";
let existLang = false;

const newContent = fileContent.messages.map((messages: Message) => {
messages.tr.forEach((value: Translation) => {
if (value.l === from) {
addValue = value.v;
}
if (value.l === to) {
existLang = true;
}
});

if (!existLang) {
messages.tr.push({ l: to, v: addValue });
}

if (messages.overrides) {
messages.overrides.forEach((i: CustomerOverride, index) => {
let addOverrideValue = "";
let existOverridesLang = false;
i.tr.forEach((value: Translation) => {
if (value.l === from) {
addOverrideValue = value.v;
}
if (value.l === to) {
existOverridesLang = true;
}
});
if (!existOverridesLang) {
messages.overrides![index].tr.push({ l: to, v: addOverrideValue });
}
});
}

return messages;
});

const writeContent = "exports.messages = " + JSON.stringify(newContent);
const prettierContent = await prettier.format(writeContent, { parser: "babel" });
fs.writeFileSync(inputPath, prettierContent, "utf-8");
};

const langCopy = () => {
const from = process.env.npm_config_from;
const to = process.env.npm_config_to;
const inputPath = path.join(process.cwd(), config.get("i18nMsg").input);
const isDir = fs.lstatSync(inputPath).isDirectory();

if (!from || !to) {
console.error("Missing params from and to");
return 1;
}

if (isDir) {
fs.readdirSync(inputPath).map(async (fileName: any) => {
const filePath = path.join(inputPath, fileName);
checkFile(filePath, from, to);
});
console.log("Successfully copied");
} else {
checkFile(inputPath, from, to);
console.log("Successfully copied");
}
return 0;
};

//Main
const res = langCopy();
process.exit(res);
1 change: 0 additions & 1 deletion src/cli/i18n-msg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import fs from "fs";
import path from "path";
import { EOL } from "os";
import config from "../../config";
import { emitWarning } from "process";

export const CLI_NAME = "i18nMsg";

Expand Down

0 comments on commit 84d24e5

Please sign in to comment.