自动比较检查 Angular i18n json 文件

Automatic compare check in Angular i18n json files

我在 Angular 应用程序中有 2 个文件 en.jsonxx.json 带有翻译。通常的想法是为多语言应用程序在两个文件中创建翻译,但有时一些程序员只在其中一个文件中添加翻译,因为他们只用他们的语言测试应用程序。

我正在寻找可以检查两个 JSON 文件中相同结构的工具,否则它会抛出错误,并且在您为第二语言创建翻译时它有助于添加。你知道这方面的任何好的做法、工具或插件吗?我正在使用 WebStorm。

尝试POEditor。它最多可免费提供 1000 个字符串。

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

使用小 Node.js 脚本

因为您已经安装了 Angular(这意味着您有 NPM 和 Node.Js),您可以使用脚本在翻译 JSON 文件中发现不一致之处。这是它的样子

代码

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(' ')} ]`);
});

示例输出

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

我使用这个工具来处理我项目中的翻译 https://translation-manager-86c3d.firebaseapp.com/