From 2f02a4379cee0bf192dc681d18c9e15fd589dc3b Mon Sep 17 00:00:00 2001 From: Joseph Garrone Date: Wed, 25 Sep 2024 10:26:25 +0200 Subject: [PATCH] Enable i18n in Single-Page account theme --- package.json | 23 ++++--- scripts/generate-i18n-messages.ts | 2 +- .../generateResourcesForMainTheme.ts | 67 +++++++++++++++++-- yarn.lock | 7 ++ 4 files changed, 80 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 5a034a65..5ff4eb7b 100644 --- a/package.json +++ b/package.json @@ -68,6 +68,7 @@ "@babel/core": "^7.24.5", "@babel/generator": "^7.24.5", "@babel/parser": "^7.24.5", + "@babel/preset-env": "7.24.8", "@babel/types": "^7.24.5", "@emotion/react": "^11.11.4", "@octokit/rest": "^20.1.1", @@ -75,20 +76,27 @@ "@storybook/builder-webpack5": "^6.5.13", "@storybook/manager-webpack5": "^6.5.13", "@storybook/react": "^6.5.13", - "eslint-plugin-storybook": "^0.6.7", "@types/babel__generator": "^7.6.4", + "@types/dompurify": "^2.0.0", "@types/make-fetch-happen": "^10.0.1", "@types/minimist": "^1.2.2", "@types/node": "^18.15.3", + "@types/properties-parser": "^0.3.3", "@types/react": "^18.0.35", "@types/react-dom": "^18.0.11", "@types/yauzl": "^2.10.3", "@vercel/ncc": "^0.38.1", + "babel-loader": "9.1.3", "chalk": "^4.1.2", "cheerio": "1.0.0-rc.12", "chokidar-cli": "^3.0.0", "cli-select": "^1.1.2", + "dompurify": "^3.1.6", + "eslint-plugin-storybook": "^0.6.7", + "evt": "^2.5.7", + "html-entities": "^2.5.2", "husky": "^4.3.8", + "isomorphic-dompurify": "^2.15.0", "lint-staged": "^11.0.0", "magic-string": "^0.30.7", "make-fetch-happen": "^11.0.3", @@ -103,20 +111,13 @@ "termost": "^v0.12.1", "tsc-alias": "^1.8.10", "tss-react": "^4.9.10", + "tsx": "^4.15.5", "typescript": "^4.9.4", "vite": "^5.2.11", "vitest": "^1.6.0", - "yauzl": "^2.10.0", - "zod": "^3.17.10", - "evt": "^2.5.7", - "tsx": "^4.15.5", - "html-entities": "^2.5.2", - "isomorphic-dompurify": "^2.15.0", - "dompurify": "^3.1.6", - "@types/dompurify": "^2.0.0", "webpack": "5.93.0", "webpack-cli": "5.1.4", - "babel-loader": "9.1.3", - "@babel/preset-env": "7.24.8" + "yauzl": "^2.10.0", + "zod": "^3.17.10" } } diff --git a/scripts/generate-i18n-messages.ts b/scripts/generate-i18n-messages.ts index 23ca76e8..a50ff2e6 100644 --- a/scripts/generate-i18n-messages.ts +++ b/scripts/generate-i18n-messages.ts @@ -13,7 +13,7 @@ import { getThisCodebaseRootDirPath } from "../src/bin/tools/getThisCodebaseRoot import { deepAssign } from "../src/tools/deepAssign"; import { THEME_TYPES } from "../src/bin/shared/constants"; import { transformCodebase } from "../src/bin/tools/transformCodebase"; -const propertiesParser: any = require("properties-parser"); +import propertiesParser from "properties-parser"; if (require.main === module) { generateI18nMessages(); diff --git a/src/bin/keycloakify/generateResources/generateResourcesForMainTheme.ts b/src/bin/keycloakify/generateResources/generateResourcesForMainTheme.ts index f1667cd1..ef77db22 100644 --- a/src/bin/keycloakify/generateResources/generateResourcesForMainTheme.ts +++ b/src/bin/keycloakify/generateResources/generateResourcesForMainTheme.ts @@ -31,6 +31,7 @@ import { objectEntries } from "tsafe/objectEntries"; import { escapeStringForPropertiesFile } from "../../tools/escapeStringForPropertiesFile"; import * as child_process from "child_process"; import { getThisCodebaseRootDirPath } from "../../tools/getThisCodebaseRootDirPath"; +import propertiesParser from "properties-parser"; export type BuildContextLike = BuildContextLike_kcContextExclusionsFtlCode & BuildContextLike_generateMessageProperties & { @@ -239,21 +240,73 @@ export async function generateResourcesForMainTheme(params: { .toString("utf8") .trim(); - const messagesDirPath = pathJoin(accountUiDirPath, "messages"); + const messageDirPath_defaults = pathJoin(accountUiDirPath, "messages"); - if (!fs.existsSync(messagesDirPath)) { + if (!fs.existsSync(messageDirPath_defaults)) { throw new Error( `Please update @keycloakify/keycloak-account-ui to 25.0.4-rc.5 or later.` ); } + const messagesDirPath_dest = pathJoin( + getThemeTypeDirPath({ themeType: "account" }), + "messages" + ); + transformCodebase({ - srcDirPath: messagesDirPath, - destDirPath: pathJoin( - getThemeTypeDirPath({ themeType: "account" }), - "messages" - ) + srcDirPath: messageDirPath_defaults, + destDirPath: messagesDirPath_dest }); + + apply_theme_changes: { + const messagesDirPath_theme = pathJoin( + buildContext.themeSrcDirPath, + "account", + "messages" + ); + + if (!fs.existsSync(messagesDirPath_theme)) { + break apply_theme_changes; + } + + fs.readdirSync(messagesDirPath_theme).forEach(basename => { + const filePath_src = pathJoin(messagesDirPath_theme, basename); + const filePath_dest = pathJoin(messagesDirPath_dest, basename); + + if (!fs.existsSync(filePath_dest)) { + fs.cpSync(filePath_src, filePath_dest); + } + + const messages_src = propertiesParser.parse( + fs.readFileSync(filePath_src).toString("utf8") + ); + const messages_dest = propertiesParser.parse( + fs.readFileSync(filePath_dest).toString("utf8") + ); + + const messages = { + ...messages_dest, + ...messages_src + }; + + const editor = propertiesParser.createEditor(); + + Object.entries(messages).forEach(([key, value]) => { + editor.set(key, value); + }); + + fs.writeFileSync( + filePath_dest, + Buffer.from(editor.toString(), "utf8") + ); + }); + } + + languageTags = fs + .readdirSync(messagesDirPath_dest) + .map(basename => + basename.replace(/^messages_/, "").replace(/\.properties$/, "") + ); } keycloak_static_resources: { diff --git a/yarn.lock b/yarn.lock index cf46d64f..abab9a2c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3722,6 +3722,13 @@ resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.5.tgz#5f19d2b85a98e9558036f6a3cacc8819420f05cf" integrity sha512-JCB8C6SnDoQf0cNycqd/35A7MjcnK+ZTqE7judS6o7utxUCg6imJg3QK2qzHKszlTjcj2cn+NwMB2i96ubpj7w== +"@types/properties-parser@^0.3.3": + version "0.3.3" + resolved "https://registry.yarnpkg.com/@types/properties-parser/-/properties-parser-0.3.3.tgz#107eef7e637c969ae1fe6ed86f373a57309bdd2c" + integrity sha512-VZhGpE+QQ2JNbaY4B4Y2iM/jdUsq3HO75uBKLk07VT6P2Kg1aifeYL6I3RosFniSdAb4PtuH5UaY8jXU7JeIYA== + dependencies: + "@types/node" "*" + "@types/qs@^6.9.5": version "6.9.7" resolved "https://registry.yarnpkg.com/@types/qs/-/qs-6.9.7.tgz#63bb7d067db107cc1e457c303bc25d511febf6cb"