Skip to content

Commit

Permalink
feat(i18n): create-translation
Browse files Browse the repository at this point in the history
  • Loading branch information
gjulivan committed Oct 31, 2024
1 parent 0f91f65 commit 73f57cf
Show file tree
Hide file tree
Showing 51 changed files with 185 additions and 21 deletions.
72 changes: 72 additions & 0 deletions automation/utils/bin/rui-create-translation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env ts-node-script
import { mkdir, writeFile } from "fs/promises";
import path from "node:path";
import { dirname } from "path";
import { getPackageFileContent, PackageSchema } from "../src";
import { readXml } from "../src/package-xml";
import { findCaptionsAndDescriptions, getWidgetXMLName, type Result } from "../src/widget-file-xml";
async function main(): Promise<void> {
console.log(`Creating translation i18n format...`);
const path = process.cwd();

const raw = await getPackageFileContent(path);
// To get better error output from zod use empty objects
const target = {
mxpackage: {},
marketplace: {},
repository: {},
testProject: {},
...raw
};

// First, check common fields
const info = PackageSchema.parse(target);

switch (info.mxpackage.type) {
case "widget": {
createTranslation(path);
break;
}
case "module": {
// TODO: ?
break;
}
case "jsactions": {
// TODO: ?
break;
}
}
}

export async function createTranslation(cwd: string) {
const widgetXMLName = await getWidgetXMLName(cwd);
if (widgetXMLName) {
const obj = await readXml(path.join(cwd, "src", widgetXMLName));
const res = findCaptionsAndDescriptions(obj);
let fileName = "result";
if (obj && typeof obj === "object" && obj["widget"] && obj["widget"]["@_id"]) {
fileName = obj["widget"]["@_id"].toLowerCase();
}
writeResultToFile(res, `dist/locales/en-US/${fileName}.json`);
writeResultToFile(res, `../../../dist/locales/en-US/${fileName}.json`);
}
}

// Function to ensure a directory exists
async function ensureDirectoryExists(filePath: string): Promise<void> {
const dir = dirname(filePath);
await mkdir(dir, { recursive: true });
}

// Function to write JSON result to a file
async function writeResultToFile(result: Result, filename: string): Promise<void> {
await ensureDirectoryExists(filename);
const jsonContent = JSON.stringify(result, null, 2);
await writeFile(filename, jsonContent, "utf-8");
console.log(`JSON result has been written to ${filename}`);
}

main().catch(e => {
console.error(e);
process.exit(1);
});
3 changes: 2 additions & 1 deletion automation/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"rui-publish-marketplace": "bin/rui-publish-marketplace.ts",
"rui-update-changelog-module": "bin/rui-update-changelog-module.ts",
"rui-update-changelog-widget": "bin/rui-update-changelog-widget.ts",
"rui-verify-package-format": "bin/rui-verify-package-format.ts"
"rui-verify-package-format": "bin/rui-verify-package-format.ts",
"rui-create-translation": "bin/rui-create-translation.ts"
},
"scripts": {
"start": "tsc --watch",
Expand Down
39 changes: 39 additions & 0 deletions automation/utils/src/widget-file-xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { readPackageXml } from "./package-xml";
export interface Result {
[key: string]: string;
}

export async function getWidgetXMLName(cwd: string): Promise<string | undefined> {
try {
const packageXml = (await readPackageXml(cwd)) as {
package?: {
clientModule?: {
widgetFiles: any;
};
};
};
return packageXml?.package?.clientModule?.widgetFiles.widgetFile["@_path"];
} catch {
return undefined;
}
}

// Function to recursively find all captions and descriptions
export function findCaptionsAndDescriptions(obj: any, result: Result = {}): Result {
for (const key in obj) {
if (typeof obj[key] === "object") {
findCaptionsAndDescriptions(obj[key], result);
} else if (obj[key].trim() !== "") {
if (
key.includes("@_caption") ||
key.toLowerCase().includes("caption") ||
key.toLowerCase().includes("description")
) {
if (!result[obj[key]]) {
result[obj[key]] = obj[key];
}
}
}
}
return result;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"build": "turbo run build",
"release": "turbo run release",
"create-gh-release": "turbo run create-gh-release --concurrency 1",
"create-translation": "turbo run create-translation",
"publish-marketplace": "turbo run publish-marketplace",
"version": "pnpm --filter @mendix/automation-utils run version",
"changelog": "pnpm --filter @mendix/automation-utils run changelog"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"lint": "eslint --ext .jsx,.js,.ts,.tsx src/",
"release": "pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
Expand Down
1 change: 1 addition & 0 deletions packages/pluggableWidgets/accordion-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"test": "pluggable-widgets-tools test:unit:web",
"release": "pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/area-chart-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"test": "pluggable-widgets-tools test:unit:web",
"release": "pluggable-widgets-tools release:web",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget"
"update-changelog": "rui-update-changelog-widget",
"create-translation": "rui-create-translation"
},
"devDependencies": {
"@mendix/automation-utils": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/pluggableWidgets/badge-button-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"test": "pluggable-widgets-tools test:unit:web",
"release": "cross-env MPKOUTPUT=BadgeButton.mpk pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
Expand Down
1 change: 1 addition & 0 deletions packages/pluggableWidgets/badge-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"e2edev": "run-e2e dev --with-preps",
"release": "cross-env MPKOUTPUT=Badge.mpk pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget"
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/bar-chart-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"test": "pluggable-widgets-tools test:unit:web",
"release": "pluggable-widgets-tools release:web",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget"
"update-changelog": "rui-update-changelog-widget",
"create-translation": "rui-create-translation"
},
"devDependencies": {
"@mendix/automation-utils": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/pluggableWidgets/barcode-scanner-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"e2edev": "run-e2e dev --with-preps",
"release": "pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget"
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/bubble-chart-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"test": "pluggable-widgets-tools test:unit:web",
"release": "pluggable-widgets-tools release:web",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget"
"update-changelog": "rui-update-changelog-widget",
"create-translation": "rui-create-translation"
},
"devDependencies": {
"@mendix/automation-utils": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/pluggableWidgets/carousel-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"test": "jest --projects jest.config.js",
"release": "cross-env MPKOUTPUT=Carousel.mpk pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/chart-playground-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"start": "pluggable-widgets-tools start:server",
"test": "pluggable-widgets-tools test:unit:web",
"update-changelog": "rui-update-changelog-widget",
"verify": "rui-verify-package-format"
"verify": "rui-verify-package-format",
"create-translation": "rui-create-translation"
},
"devDependencies": {
"@mendix/automation-utils": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/pluggableWidgets/color-picker-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"test": "pluggable-widgets-tools test:unit:web",
"release": "cross-env MPKOUTPUT=ColorPicker.mpk pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/column-chart-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
"e2e": "run-e2e ci",
"e2edev": "run-e2e dev --with-preps"
"e2edev": "run-e2e dev --with-preps",
"create-translation": "rui-create-translation"
},
"devDependencies": {
"@mendix/automation-utils": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/pluggableWidgets/combobox-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"e2edev": "run-e2e dev --with-preps",
"release": "pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"update-changelog": "rui-update-changelog-widget",
"e2e": "run-e2e ci",
"e2edev": "run-e2e dev --with-preps",
"e2e-update-project": "pnpm --filter data-widgets run build:include-deps"
"e2e-update-project": "pnpm --filter data-widgets run build:include-deps",
"create-translation": "rui-create-translation"
},
"dependencies": {
"@mendix/widget-plugin-external-events": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"update-changelog": "rui-update-changelog-widget",
"e2e": "run-e2e ci",
"e2edev": "run-e2e dev --with-preps",
"e2e-update-project": "pnpm -w exec turbo run build:module --filter data-widgets --force"
"e2e-update-project": "pnpm -w exec turbo run build:module --filter data-widgets --force",
"create-translation": "rui-create-translation"
},
"dependencies": {
"@mendix/widget-plugin-external-events": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"update-changelog": "rui-update-changelog-widget",
"e2e": "run-e2e ci",
"e2edev": "run-e2e dev --with-preps",
"e2e-update-project": "pnpm --filter data-widgets run build:include-deps"
"e2e-update-project": "pnpm --filter data-widgets run build:include-deps",
"create-translation": "rui-create-translation"
},
"dependencies": {
"@mendix/widget-plugin-external-events": "workspace:*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"update-changelog": "rui-update-changelog-widget",
"e2e": "run-e2e ci",
"e2edev": "run-e2e dev --with-preps",
"e2e-update-project": "pnpm --filter data-widgets run build:include-deps"
"e2e-update-project": "pnpm --filter data-widgets run build:include-deps",
"create-translation": "rui-create-translation"
},
"dependencies": {
"@mendix/widget-plugin-component-kit": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/datagrid-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"release": "pluggable-widgets-tools release:ts",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
"e2e-update-project": "pnpm --filter data-widgets run build:include-deps"
"e2e-update-project": "pnpm --filter data-widgets run build:include-deps",
"create-translation": "rui-create-translation"
},
"dependencies": {
"@mendix/widget-plugin-filtering": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/dropdown-sort-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"update-changelog": "rui-update-changelog-widget",
"e2e": "run-e2e ci",
"e2edev": "run-e2e dev --with-preps",
"e2e-update-project": "pnpm -w exec turbo run build:module --filter data-widgets --force"
"e2e-update-project": "pnpm -w exec turbo run build:module --filter data-widgets --force",
"create-translation": "rui-create-translation"
},
"dependencies": {
"@mendix/widget-plugin-filtering": "workspace:*"
Expand Down
1 change: 1 addition & 0 deletions packages/pluggableWidgets/events-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"e2edev": "run-e2e dev --with-preps",
"release": "pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget"
Expand Down
1 change: 1 addition & 0 deletions packages/pluggableWidgets/fieldset-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"test": "pluggable-widgets-tools test:unit:web",
"release": "pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/fileuploader-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"test": "pluggable-widgets-tools test:unit:web",
"release": "pluggable-widgets-tools release:web",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget"
"update-changelog": "rui-update-changelog-widget",
"create-translation": "rui-create-translation"
},
"dependencies": {
"classnames": "^2.2.6",
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/gallery-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"update-changelog": "rui-update-changelog-widget",
"e2e": "run-e2e ci",
"e2edev": "run-e2e dev --with-preps",
"e2e-update-project": "pnpm -w exec turbo run build:module --filter data-widgets --force"
"e2e-update-project": "pnpm -w exec turbo run build:module --filter data-widgets --force",
"create-translation": "rui-create-translation"
},
"dependencies": {
"@mendix/widget-plugin-external-events": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/pluggableWidgets/google-tag-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/heatmap-chart-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
"e2e": "run-e2e ci",
"e2edev": "run-e2e dev --with-preps"
"e2edev": "run-e2e dev --with-preps",
"create-translation": "rui-create-translation"
},
"devDependencies": {
"@mendix/automation-utils": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/html-element-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"start": "pluggable-widgets-tools start:server",
"test": "pluggable-widgets-tools test:unit:web",
"update-changelog": "rui-update-changelog-widget",
"verify": "rui-verify-package-format"
"verify": "rui-verify-package-format",
"create-translation": "rui-create-translation"
},
"devDependencies": {
"@mendix/automation-utils": "workspace:*",
Expand Down
1 change: 1 addition & 0 deletions packages/pluggableWidgets/image-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"test": "pluggable-widgets-tools test:unit:web",
"release": "pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"test": "pluggable-widgets-tools test:unit:web",
"release": "pluggable-widgets-tools release:web",
"create-gh-release": "rui-create-gh-release",
"create-translation": "rui-create-translation",
"publish-marketplace": "rui-publish-marketplace",
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
Expand Down
3 changes: 2 additions & 1 deletion packages/pluggableWidgets/line-chart-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"verify": "rui-verify-package-format",
"update-changelog": "rui-update-changelog-widget",
"e2e": "run-e2e ci",
"e2edev": "run-e2e dev --with-preps"
"e2edev": "run-e2e dev --with-preps",
"create-translation": "rui-create-translation"
},
"devDependencies": {
"@mendix/automation-utils": "workspace:*",
Expand Down
Loading

0 comments on commit 73f57cf

Please sign in to comment.