mirror of
https://github.com/sharevb/it-tools.git
synced 2026-11-23 06:11:06 +03:00
feat: translation scripts enhancements
extract-tools-desc: use t() to make recognizable extract-tools-string: search for attributes only in .vue + handle single directory/file from command line + useI18n only in .vue
This commit is contained in:
1 parent
9cb58fccf3
commit
a4576e040c
2 files changed
+32
-9
No files matched your search
@@ -29,10 +29,10 @@ function processFile(filePath) {
|
|||||||
|
|
||||||
// Modify name and description to use the translate function
|
// Modify name and description to use the translate function
|
||||||
const transformedContent = originalContent
|
const transformedContent = originalContent
|
||||||
.replace(/name: '((?:[^']+|(?<=\\)')+)'/, `name: translate('tools.${parentDir}.title')`)
|
.replace(/name: '((?:[^']+|(?<=\\)')+)'/, `name: t('tools.${parentDir}.title')`)
|
||||||
.replace(/description: '((?:[^']+|(?<=\\)')+)'/, `description: translate('tools.${parentDir}.description')`);
|
.replace(/description: '((?:[^']+|(?<=\\)')+)'/, `description: t('tools.${parentDir}.description')`);
|
||||||
|
|
||||||
fs.writeFileSync(filePath, `import { translate } from '@/plugins/i18n.plugin';\n${transformedContent}`, 'utf-8');
|
fs.writeFileSync(filePath, `import { translate as t } from '@/plugins/i18n.plugin';\n${transformedContent}`, 'utf-8');
|
||||||
console.log(`Transformed: ${filePath}`);
|
console.log(`Transformed: ${filePath}`);
|
||||||
|
|
||||||
// Update locales file with extracted name and description
|
// Update locales file with extracted name and description
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ function processVueComponents(toolDir, toolName) {
|
|||||||
|
|
||||||
// Function to process a Vue component file
|
// Function to process a Vue component file
|
||||||
function processVueComponent(filePath, toolName) {
|
function processVueComponent(filePath, toolName) {
|
||||||
|
console.log(`Processing ${filePath} in ${toolName}`);
|
||||||
|
|
||||||
let content = fs.readFileSync(filePath, 'utf8');
|
let content = fs.readFileSync(filePath, 'utf8');
|
||||||
const origContent = content;
|
const origContent = content;
|
||||||
|
|
||||||
@@ -83,17 +85,38 @@ function processVueComponent(filePath, toolName) {
|
|||||||
console.log(`Nothing to extract in ${filePath}`);
|
console.log(`Nothing to extract in ${filePath}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Ensure the useI18n import exists
|
|
||||||
if (!content.includes("const { t } = useI18n();")) {
|
|
||||||
content = content.replace(/\<script setup( lang="ts")?\>/, "<script setup lang=\"ts\">\nimport { useI18n } from 'vue-i18n';\nconst { t } = useI18n();");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (filePath.endsWith('.vue')) {
|
||||||
|
// Ensure the useI18n import exists
|
||||||
|
if (!content.includes("const { t } = useI18n();")) {
|
||||||
|
content = content.replace(/\<script setup( lang="ts")?\>/, "<script setup lang=\"ts\">\nimport { useI18n } from 'vue-i18n';\nconst { t } = useI18n();");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// Ensure the useI18n import exists
|
||||||
|
if (!content.includes("import { translate as t } from '@/plugins/i18n.plugin';")) {
|
||||||
|
content = 'import { translate as t } from "@/plugins/i18n.plugin";\n' + content;
|
||||||
|
}
|
||||||
|
}
|
||||||
fs.writeFileSync(filePath, content, 'utf8');
|
fs.writeFileSync(filePath, content, 'utf8');
|
||||||
console.log(`Processed: ${filePath}`);
|
console.log(`Processed: ${filePath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (process.argv[1]) {
|
const localFileOrDir = process.argv[2];
|
||||||
processVueComponent(process.argv[2], process.argv[3] || basename(process.argv[2]));
|
if (localFileOrDir) {
|
||||||
|
if (fs.statSync(localFileOrDir).isDirectory()) {
|
||||||
|
fs.readdirSync(localFileOrDir)
|
||||||
|
.filter((fileName) => {
|
||||||
|
const filePath = path.join(localFileOrDir, fileName);
|
||||||
|
return fs.statSync(filePath).isFile();
|
||||||
|
})
|
||||||
|
.forEach((fileName) => {
|
||||||
|
const filePath = path.join(localFileOrDir, fileName);
|
||||||
|
processVueComponent(filePath, process.argv[3] || basename(localFileOrDir));
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
processVueComponent(localFileOrDir, process.argv[3] || basename(localFileOrDir));
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
fs.readdirSync(toolsDir)
|
fs.readdirSync(toolsDir)
|
||||||
.filter((toolName) => {
|
.filter((toolName) => {
|
||||||
|
|||||||
Reference in new issue
Block a user