npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

i18next-json-sync-ai

v0.0.10

Published

keep i18next json resource files in sync, uses open ai to translate

Downloads

76

Readme

Build Status npm

i18next-json-sync-ai

Keeps i18next-ai JSON resource files in sync against a primary language, forked form i18next, including plural forms. When hooked up to a build process/CI server, ensures keys added/removed from one language are correctly propagated to the other languages, reducing the chance for missing or obselete keys, merge conflicts, and typos.

With the added capability to traverse in-depth of the JSON files, it syncs the JSON files completely, no matter how deep you go down.

Example

Given these files:

 locales
 ├── en.json
 ├── fr.json
 └── ru.json
en.json
{
  "key_one": "value",
  "book": "book",
  "book_plural": "books"
}

fr.json
{
  "key_one": "french value"
}

ru.json
{
  "extra_key": "extra value"
}

fr.json and ru.json can be synced against en.json:

import sync from 'i18next-json-sync'
sync({
  files: 'locales/*.json',
  primary: 'en'
});

resulting in:

en.json
{
  "key_one": "value",
  "book": "book",
  "book_plural": "books"
}

fr.json
{
  "key_one": "french value",
  "book": "book",
  "book_plural": "books"
}

ru.json
{
  "key_one": "value",
  "book_0": "books",
  "book_1": "books",
  "book_2": "books"
}

key_one was left alone in fr.json since it's already localized, but book and book_plural were copied over. An extraneous key in ru.json was deleted and keys from en.json copied over.

This works on one folder at a time, but can deal with whatever the files glob returns. Files are grouped into directories before processing starts. Folders without a 'primary' found are ignored.

Usage

$ npm install i18next-json-sync-ai --save-dev

In node.js

import sync from 'i18next-json-sync-ai';
//or in ES5 world:
//const sync = require('i18next-json-sync-ai').default;

//defaults are inline:
sync({
  /** Audit files in memory instead of changing them on the filesystem and
    * throw an error if any changes would be made */
  check: false,
  /** Glob pattern for the resource JSON files */
  files: '**/locales/*.json',
  /** An array of glob patterns to exclude from the files search */
  excludeFiles: ['**/node_modules/**'],
  /** Primary localization language. Other language files will be changed to match */
  primary: 'en',
  /** Language files to create if they don't exist, e.g. ['es, 'pt-BR', 'fr'] */
  createResources: [],
  /** Space value used for JSON.stringify when writing JSON files to disk */
  space: 4,
  /** Line endings used when writing JSON files to disk. Either LF or CRLF */
  lineEndings: 'LF',
  /** Insert a final newline when writing JSON files to disk */
  finalNewline: false,
  /** Use empty string for new keys instead of the primary language value */
  newKeysEmpty: false
  /** Use OpenAI for translation */
  useOpenAi: true
})

CLI

It can be installed globally and run with sync-i18n, but package.json scripts are a better fit.

{
  "name": "my-app",
  "scripts": {
    "i18n": "sync-i18n --files '**/locales/*.json' --primary en --languages es fr ja zh ko --space 2",
    "check-i18n": "npm run i18n -- --check"
  }
}

Then use npm run i18n to sync on the filesystem and npm run check-i18n to validate.

All options are available via CLI. Use -h or --help to get help output.

License

MIT