简体   繁体   English

Angular i18n json 文件中的自动比较检查

[英]Automatic compare check in Angular i18n json files

I have 2 files en.json and xx.json with translations in Angular application.我有 2 个文件en.jsonxx.json ,在 Angular 应用程序中有翻译。 Common idea is to create translation in both files for multilanguage application, but sometimes some programmers adding translation just in one of these file, because they testing app only in their language.常见的想法是为多语言应用程序在两个文件中创建翻译,但有时一些程序员只在其中一个文件中添加翻译,因为他们只用他们的语言测试应用程序。

I am looking for tool that check same structure in both JSON files, otherwise it throw error and it helps with addition when you creating translations for second language.我正在寻找在两个 JSON 文件中检查相同结构的工具,否则它会抛出错误并且在您为第二语言创建翻译时有助于添加。 Do you know any good practices, tools or plugins for this?你知道这方面的任何好的做法、工具或插件吗? I am using WebStorm.我正在使用 WebStorm。

Use a small Node.js script使用一个小的 Node.js 脚本

Since you already have Angular installed (which means you have NPM & Node.Js), you can find inconsistencies in your translation JSON files with a script.由于您已经安装了 Angular(这意味着您有 NPM 和 Node.Js),您可以使用脚本在您的翻译 JSON 文件中找到不一致的地方。 Here's how it looks这是它的外观

Code代码

const fs = require('fs');

// Define your file paths here
const files = ['./english.json', './russian.json']

// Read contents and parse JSON
const filesWithKeys = files.map(f => ({
  name: f,
  content: JSON.parse(fs.readFileSync(f, 'utf8'))
}));

// Gather all the keys used in all files in one array
const allKeys = filesWithKeys.map(f => Object.keys(f.content)).flat();

// Find the missing keys by file
const missingKeysByFile = filesWithKeys.map(f => ({
  name: f.name,
  missingKeys: allKeys.filter(k => !(k in f.content))
})).filter(f => f.missingKeys.length > 0);


// Print the result
missingKeysByFile.forEach(f => {
  console.log(`File "${f.name}" is missing keys [ ${f.missingKeys.join(' ')} ]`);
});

Sample output样本输出

File "english.json" is missing keys [ appTitle, contentHeader ]
File "russian.json" is missing keys [ usernameHint ]

Try POEditor .试试POEditor It's free up to 1000 strings.它最多可免费使用 1000 个字符串。

您可以查看 Pootle ( http://pootle.translatehouse.org/index.html ) 或 Poedit ( https://poedit.net ) 或 POEditor ( https://poeditor.com/ )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM