diff --git a/package.json b/package.json index 9be7c26b..9b8917cc 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,8 @@ "script:create:tool": "node scripts/create-tool.mjs", "script:create:ui": "hygen generator ui-component", "release": "node ./scripts/release.mjs", - "script:extract:desc": "node ./scripts/extract-tools-desc.mjs" + "script:extract:desc": "node ./scripts/extract-tools-desc.mjs", + "script:extract:strings": "node ./scripts/extract-tools-strings.mjs" }, "dependencies": { "@chenfengyuan/vue-barcode": "^2.0.2", diff --git a/scripts/extract-tools-strings.mjs b/scripts/extract-tools-strings.mjs new file mode 100644 index 00000000..ea879857 --- /dev/null +++ b/scripts/extract-tools-strings.mjs @@ -0,0 +1,106 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import yaml from 'yaml'; + +const toolsDir = 'src/tools'; +const localesFile = 'locales/en.yml'; + +// Load existing locales file or initialize an empty object +let localesData = {}; +if (fs.existsSync(localesFile)) { + localesData = yaml.parse(fs.readFileSync(localesFile, 'utf-8')) || {}; +} + +// Function to process Vue components in a directory +function processVueComponents(toolDir, toolName) { + fs.readdirSync(toolDir) + .filter(file => file.endsWith('.vue')) + .forEach(file => processVueComponent(path.join(toolDir, file), toolName)); +} + +// Function to process a Vue component file +function processVueComponent(filePath, toolName) { + let content = fs.readFileSync(filePath, 'utf8'); + const origContent = content; + + // Update locales file with extracted name and description + if (!localesData.tools) { + localesData.tools = {}; + } + if (!localesData.tools[toolName]) { + localesData.tools[toolName] = {}; + } + + // Regex to find label or placeholder attributes + let regex = /\b(label|text|message):\s*'((?:[^']|\\')+)'/g; + content = content.replace(regex, (match, attr, text) => { + if (!text?.trim()) { + return match; + } + const key = (attr + '-' + text.replace(/[\p{P}\p{S}\s]+/gu, '-').replace(/^-+|-+$/g, '').replace(/-+/g, '-')).toLowerCase(); + if (!localesData.tools[toolName].texts) { + localesData.tools[toolName].texts = {}; + } + localesData.tools[toolName].texts[key] = text; + return `${attr}: t('tools.${toolName}.texts.${key}')`; + }); + + // Regex to find label or placeholder attributes + regex = /(? { + if (!text?.trim()) { + return match; + } + const key = (attr + '-' + text.replace(/[\p{P}\p{S}\s]+/gu, '-').replace(/^-+|-+$/g, '').replace(/-+/g, '-')).toLowerCase(); + if (!localesData.tools[toolName].texts) { + localesData.tools[toolName].texts = {}; + } + localesData.tools[toolName].texts[key] = text; + return `:${attr}="t('tools.${toolName}.texts.${key}')"`; + }); + + // Regex to find text inside HTML tags + const textInsideTagsRegex = /(?(?:\n\s*)?([^\<\>\n]+)(?:\n\s*)?\ { + if (!text?.trim()) { + return match; + } + if (text.match(/[\}\{\|\[\<\>\]]/)) { + return match; + } + const key = ('tag-' + text.trim().replace(/[\p{P}\p{S}\s]+/gu, '-').replace(/^-+|-+$/g, '').replace(/-+/g, '-')).toLowerCase(); + if (!localesData.tools[toolName].texts) { + localesData.tools[toolName].texts = {}; + } + localesData.tools[toolName].texts[key] = text.trim(); + return `>{{ t('tools.${toolName}.texts.${key}') }}<`; + }); + + if (origContent === content) { + console.log(`Nothing to extract in ${filePath}`); + return; + } + // Ensure the useI18n import exists + if (!content.includes("const { t } = useI18n();")) { + content = content.replace(/\