Coder Social home page Coder Social logo

i18next-locales-sync's Introduction

i18next-locales-sync

CI npm

Syncs i18next locale resource files against a primary language.

Installation

$ npm install --save-dev i18next-locales-sync

Features

  1. Supports namespaces.
  2. Full plural support, based on the real i18next pluralResolver.
  3. Supports JSON v4
  4. Sorting secondary locale keys by primary language order.
  5. Supports multiple locale folder structure, {lng}/{namespace}, {namespace}/{lng}.
  6. Creates missing locale files.
  7. Allows overriding plural rules.

Usage

1. CLI

$ npx i18next-locales-sync -p he -s en de ja -l path/to/locales/folder --spaces 2

or using config file

// localesSync.config.js
module.exports = {
  primaryLanguage: 'he',
  secondaryLanguages: ['en', 'de', 'ja'],
  localesFolder: './path/to/locales/folder',
  overridePluralRules: (pluralResolver) =>
    pluralResolver.addRule('he', pluralResolver.getRule('en')), // This is available only when using config file
  spaces: 2,
};
$ npx i18next-locales-sync -c ./localesSync.config.js

2. Node

import { syncLocales } from 'i18next-locales-sync';
import path from 'path';

syncLocales({
  primaryLanguage: 'en',
  secondaryLanguages: ['en', 'de', 'ja'],
  localesFolder: path.resolve('./path/to/locales/folder'),
  overridePluralRules: (pluralResolver) =>
    pluralResolver.addRule('he', pluralResolver.getRule('en')),
});

Options

Key Type Default value
primaryLanguage string
secondaryLanguages string[]
localesFolder string
outputFolder string? localesFolder
overridePluralRules (pluralResolver: PluralResolver)? => PluralResolver
useEmptyString boolean false
spaces number 2
compatibilityJSON string v4

Currently, the lib supports only .json locale files, PRs are welcome :].

Example

Given these files:

examples
├── en
│   └── namespace.json
├── he
│   └── namespace.json
└── ja
    └── namespace.json
// en/namespace.json
{
  "foo_male": "bar-male-en",
  "room_one": "room",
  "room_other": "rooms"
}
// he/namespace.json
{
  "room": "חדר",
  "foo_male": "bar-male-he",
  "room_few": "חדרים"
}
// ja/namespace.json
{
  "foo_male": "bar-male-ja",
  "room": "部屋",
  "room_other": "部屋"
}

Syncying he & ja against en

$ npx i18next-locales-sync -p en -s he ja -l ./examples/

Will result with

// en/namespace.json

// `en` remains untouched
{
  "foo_male": "bar-male-en",
  "room_one": "room",
  "room_other": "rooms"
}
// he/namespace.json

// sorted based on the primary lang file
// keeps existing plural form (room_3)
// added missing plural forms
{
  "foo_male": "bar-male-he",
  "room_one": "חדר",
  "room_two": "חדרים",
  "room_few": "rooms",
  "room_many": "rooms",
  "room_other": "rooms"
}
// ja/namespace.json

// keeps exising fields
// removed plural form since there is no plural form in Japanese
{
  "foo_male": "bar-male-ja",
  "room": "部屋"
}

Prior art

  1. i18next-json-sync

i18next-locales-sync's People

Contributors

dependabot[bot] avatar felixmosh avatar sjark avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sjark

i18next-locales-sync's Issues

Support `check` flag

@felixmosh Thanks for this library which supports namespace as well. I read the document but still want to confirm is there check flag available with i18next-locales-sync ? If no, then is it possible to add this flag, it will be really useful to check locales files in CI environment.

Cannot find module 'tslib'

Describe the bug
Can't run the utility, it crashes at start.

To Reproduce

npx i18next-locales-sync -p cs -s en -l src/main/resources/locales

Expected behavior
Not crashing

Environment (please complete the following information):

  • OS: Ubuntu 21.04
  • NodeJS: v16.8.0
  • NPM: 7.21.0

Additional context

Need to install the following packages:
  i18next-locales-sync
Ok to proceed? (y) y
node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module 'tslib'
Require stack:
- /home/pitel/.npm/_npx/63eea2b1d9392592/node_modules/i18next-locales-sync/dist/cli.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (/home/pitel/.npm/_npx/63eea2b1d9392592/node_modules/i18next-locales-sync/dist/cli.js:4:17)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:79:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/home/pitel/.npm/_npx/63eea2b1d9392592/node_modules/i18next-locales-sync/dist/cli.js'
  ]
}

Sync with empty value

First of all, thanks for creating this package so I don't have to hack something together manually 😅 It works great, however I have one gripe:

Is your feature request related to a problem? Please describe.
I don't want the values of my main language's file to be used as the values for my other language's files.

Describe the solution you'd like
Should be able to specify if empty values should be used (this should be the default imo).

Additional context

Syncing

// en/namespace.json
{
  "foo": "bar-en",
  "bar": "foo-en",
}

and

// nl/namespace.json
{
  "foo": "bar-nl",
}

should result in

// nl/namespace.json
{
  "foo": "bar-nl",
  "bar": "",
}

and not

// nl/namespace.json
{
  "foo": "bar-nl",
  "bar": "foo-en",
}

Namespace directory starting with language code produces unexpected results

Describe the bug
Whenever namespace directory starts with language code being processed all produced files are having empty values.

To Reproduce
Steps to reproduce the behavior:

  1. Create following directory structure:
.
├── package.json
├── config.js
├── src
│   ├── x
│   │   ├── en.json
│   │   ├── de.json
│   │   ├── fr.json
│   ├── french
│   │   ├── en.json
│   │   ├── de.json
│   │   ├── fr.json

  1. config.js:
module.exports = {
    primaryLanguage: 'en',
    secondaryLanguages: ['de', 'fr'],
    localesFolder: './src',
    outputFolder: './new',
    useEmptyString: true
  };
  1. Content of [en|de|fr].json:
{
    "test": "Baguette"
}
  1. Run: npx i18next-locales-sync -c ./config.js
  2. new directory will reflect src directory structure, but fr.json will have following content:
{
  "test": ""
}

Expected behavior
I would expect fr.json to have following content:

{
    "test": "Baguette"
}

Environment

  • OS: Windows 11
  • i18next-locales-sync: 2.0.0
  • node: 14.20.0
  • npm: 6.14.17

Support for parsing JSON files containing c++ style comments

The JSON files we are trying to keep synced include C++ style comments.

For example:

//------------------
//Config Translation
//------------------

While I realize comments, in general, are not officially supported in the JSON spec, many JSON parsers do handle it. It would be nice if this project could, at minimum, ignore C++ style comments when attempting to sync keys.

Currently, it just halts due to / being an unexpected token.

Doesn't work with `-sp` flag in CLI

Describe the bug
It fails with following error if I try to use -sp 4 parameter in CLI -

$ npx i18next-locales-sync -p en -s fr ja -l public/locales -sp 4
/Users/.../node_modules/i18next-locales-sync/dist/generateLocaleFiles.js:103
        throw new Error(`${constats_1.LIB_PREFIX} There are no files for your primary language (${primaryLanguage})`);
        ^

Error: [ i18next-locales-sync ]: There are no files for your primary language (4,en)
    at generateLocaleFiles (/Users/.../node_modules/i18next-locales-sync/dist/generateLocaleFiles.js:103:15)
    at syncLocales (/Users/.../node_modules/i18next-locales-sync/dist/index.js:18:71)
    at Object.<anonymous> (/Users/.../node_modules/i18next-locales-sync/dist/cli.js:104:25)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)

Without this parameter everything works as expected.
Paths have been changed to hide my personal info.

The same options but with config files also work as expected:

module.exports = {
    primaryLanguage: 'en',
    secondaryLanguages: ['fr', 'ja'],
    localesFolder: './public/locales',
    spaces: 4,
};

Environment (please complete the following information):

 - OS: macOS v.11.6.4 
 - [email protected]
 - [email protected]

It removes options with context from synced locales

I have the following translation json files:

// en/history.json
{
    "providerName": "{{ context }}"
}

// fr/history.json
{
    "providerName": "{{ context }}",
    "providerName_password": "mot de passe",
    "providerName_email": "courriel",
}

After command running, all options with namespaces are removed from all secondary locales if they are not present in the primary one (en in my case).

$ npx i18next-locales-sync -p en -s fr -l public/locales

// RESULTS
// en/history.json
{
    "providerName": "{{ context }}"
}

// fr/history.json
{
    "providerName": "{{ context }}"
}

Expected behavior
All options with namespaces should be saved even if they are not present in the primary locale file. i18next allows this, so it's correct and acceptable.

Environment (please complete the following information):

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.