1.7.0 • Published 3 years ago

i18n-db v1.7.0

Weekly downloads
12
License
MIT
Repository
github
Last release
3 years ago

NPM Version CI Dependency Status Dev Dependency Status

i18n-db

A lightweight internationalization management tool that uses SQLite for managing translations, with JSON file output. The i18n CLI provides quick and easy interaction with the database for adding and editing locales, resources, and translations; Or you can access the SQLite database directly for complex queries using something like SQLite Browser.

Advantages over manually maintaining JSON files:

  • Automatically identify missing translations.
  • Audit fields track when translations change, identifying when existing translations need review.
  • Export/import translations files for working with translators or third party tools.
  • Write your own SQLite queries for complex or unique use cases.

Table of Contents

Features

  • 📥 Import/export xliff, csv, and json files.
  • 👀 Know exactly what resources need translations or updates.
  • 🎉 Works with any library that uses JSON as a source for locale resources.
  • 🚀 Nice and simple CLI.
  • 💪 Uses SQLite, so you can write custom queries or manipulate data however you want.

Installation

npm install -g i18n-db

Usage

i18n --help

init

Setup configuration and initialize database.

i18n init

Options:

OptionAliasTypeDescriptionDefault
--defaultLocale-lstringDefault locale to set.en-US
--directory-dstringDirectory to store database file, relative to root../i18n
--output-ostringDirectory to store generated files, relative to root../i18n

import [file]

Import translation records.

Options:

OptionAliasTypeDescriptionDefault
--format-fstringFile format. Available: xliff, csv, jsonxliff
--locale-lstringLocale code to import translations for.defaultLocale
--bump-bbooleanSave translations (bumping version number) even if the value has not changed.false

export

Alias: x

Export translations for translation. Files are created at ./i18n-export; This directory can be added to gitignore.

Options:

OptionAliasTypeDescriptionDefault
--format-fstringOutput file format. Available: xliff, csv, jsonxliff
--locale-lstringLocale code to export translations for.all

generate

Alias: g

Generate locale JSON files. A file for each locale will be generated that contains translated resources. Any resources that are missing a translation for a locale will not be included in the generated file.

Example output file structure:

./i18n
  en-US.json
  es-MX.json
  fr-CA.json

Example output file content:

// en-US.json

{
  "key": "translation value",
  "Greeting": "Hello World!",
  "Common": {
    "Yes": "Yes"
  },
  ...
}

locale list

Aliases:

  • locale ls
  • l ls

List all locales.

locale set [code]

Alias: l set [code]

Add/update a locale record.

Options:

OptionAliasTypeDescriptionDefault
--fullName-nstringLocale full name.n/a

locale remove [code]

Aliases:

  • l remove [code]
  • l rm [code]

Remove a locale record and all associated translations.

resource list

Aliases:

  • r list
  • r ls

List all resources.

resource rename [oldKey] [newKey]

Aliases:

  • r rename [oldKey] [newKey]
  • r ren [oldKey] [newKey]

Rename a resource key.

resource remove [key]

Aliases:

  • r remove [key]
  • r rm [key]

Remove a resource record and all associated translations.

translate list

Aliases:

  • t list [key]
  • t ls [key]

List translations.

OptionAliasTypeDescriptionDefault
--term-tstringOptional search term to filter by. Wildcard character: %n/a
--key-kstringOptional resource key filter by. Wildcard character: %n/a
--locale-lstringOptional locale code to filter by.n/a

translate duplicates

Aliases:

  • t duplicates
  • t d

Find duplicate translations.

translate set [key] [value]

Alias: t set [key] [value]

Add/update a translation record.

Options:

OptionAliasTypeDescriptionDefault
--desc-dstringResource description.n/a
--locale-lstringTranslation locale code.defaultLocale

Configuration

Configuration options are stored in a i18n.json file. The following properties are supported:

NameTypeDescription
defaultLocalestringDefault locale.
directorystringDirectory to store database file, relative to root.
outputstring or object[]Directory to store generated files, relative to root.
nestedbooleanDetermines if nested JSON should be generated.

Multiple Output

The output property can be an array of objects to define multiple output destinations.

{
  "output": [
    {
      "match": null,
      "directory": "./i18n/everything"
    },
    {
      "match": "Common.*",
      "directory": "./i18n/common-only"
    },
    {
      "match": "MyApp.*",
      "directory": "./projects/MyApp/i18n"
    }
  ]
}
OptionTypeDescription
matchstring or nullOptional pattern to filter resource by key. (1)
directorystringDirectory to store generated files, relative to root.

(1) See the micromatch package for details of the patterns you can specify.

Examples

Adding translations from scratch.

i18n init

# add translation to default locale
i18n translation set "Greeting" "Hello"

# add more locales
i18n locale set "es-MX"
i18n locale set "zh"

# add a translations of "Greeting" for new locales
i18n translation set "Greeting" "Hola" --locale "es-MX"
i18n translation set "Greeting" "你好" --locale "zh"

# generate locale files
i18n generate

Add translations from a translator.

i18n init

# add translation to default locale
i18n translation set "Greeting" "Hello"

# add another locale
i18n locale set "es-MX"

# export translation file(s) for translator
i18n export

# send "es-MX.xliff" file to translator ...

# import translations
i18n import ./es-MX_done.xliff

# generate locale files
i18n generate

Development

Clone the repo and run the following commands in the i18n-db directory:

npm ci
npm link
npm run build