代码之家  ›  专栏  ›  技术社区  ›  Gregor Albert

自动比较检查角度i18n json文件

  •  2
  • Gregor Albert  · 技术社区  · 7 年前

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

    我正在寻找在两个JSON文件中检查相同结构的工具,否则它会抛出错误,并且在您为第二语言创建翻译时,它有助于进行添加。你知道有什么好的实践,工具或插件吗?我在用网络风暴。

    3 回复  |  直到 7 年前
        1
  •  1
  •   Gabriel Csöllei    7 年前

    尝试 POEditor . 它最多可以释放1000根弦。

        2
  •  1
  •   Ankur Jain    7 年前
        3
  •  0
  •   molamk    7 年前

    使用小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 ]