Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

i18next-locales-sync

felixmosh18.5kMIT2.1.1TypeScript support: included

Syncs i18next locale resource files against a primary language.

i18next, locale, sync

readme

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_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

changelog

Changelog

All notable changes to this project will be documented in this file. Dates are displayed in UTC.

Generated by auto-changelog.

2.1.0

  • Bump deps d8fec60
  • Bump @babel/traverse from 7.15.0 to 7.23.2 beae5cd
  • switch to fdir and picomatch 41a8b68

2.0.1

10 August 2022

  • fix: stop confusing namespace with a language prefix 404ccda
  • Bump parse-url from 6.0.0 to 6.0.2 cb4ba7a
  • Update example 84c0fab

2.0.0

4 July 2022

1.2.1

24 June 2022

1.2.0

18 June 2022

  • Add support for custom spacing in resulting json files 1d6d355
  • Fix minor styling issues 0ed741d
  • Bump shelljs from 0.8.4 to 0.8.5 78666ae

1.1.2

28 April 2022

1.1.1

26 August 2021

1.1.0

1 June 2021

  • Bump handlebars from 4.7.6 to 4.7.7 #2
  • Deps bump 8f07edd
  • feat: add useEmptyString option to generate empty string as a value for new keys 22ce291
  • Bump browserslist from 4.14.5 to 4.16.6 e5e345c

1.0.4

2 May 2021

1.0.3

12 February 2021

1.0.2

12 February 2021

1.0.1

11 February 2021

1.0.0

11 February 2021