2

GitHub - Simonwep/li18nt: 🌎 Lint your i18n translation files. Detect conflicting...

 1 year ago
source link: https://github.com/Simonwep/li18nt
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Logo

i18n translation files linter.

Info: The README is always up-to-date with the latest commit, check out releases to see the docs for your version! If you're curious about what's coming in v4 checkout the next branch :)

This linter will do three major things:

  1. Finding conflicts: Comparing your files against each other to see if there are any properties with types not matching up.
  2. Finding duplicates: Finding duplicates to reduce redundancy and elimitate duplicate translations.
  3. Cleanup: Sorting all properties alphabetically which will make working with your file easier and maintain consistency across all your files.

It comes with a CLI and an API.

Gettings started

Install via npm:

$ npm install li18nt

... or using yarn:

$ yarn add li18nt

CLI Usage

Installing it will add li18nt (and the alias lint-i18n) to your command line.

Examples:

# Check if your files are prettified
$ li18nt locales/*.json --prettified

# Prettify your files, check for conflicts / missing properties and duplicates
$ li18nt locales/*.json --prettified --conflicts --duplicates --fix

# List all commands
$ li18nt --help
Usage: li18nt [files...] [options]

Lints your locales files, lint-i18n is an alias.

Options:
  -v, --version                  Output the current version
  -q, --quiet                    Print only errors and warnings
  -d, --debug                    Debug information
  -f, --fix                      Tries to fix existing errors
  -p, --prettified [number|tab]  Check if files are properly formatted (default: 4 spaces)
  --duplicates [off|warn|error]  Find duplicates (default: warn)
  --conflicts [off|warn|error]   Find type conflicts and missing properties (default: error)
  --config [path]                Use configuration file
  --skip-invalid                 Skip invalid files without exiting
  --report                       Print system information
  -h, --help                     Show this help text

The following file-names can be used as configuration: .li18ntrc, .li18nt.json,.li18ntrc.json or li18nt.config.js. A configuration file will override specified properties. Example:

{
    // true will use the default value (error), you may pass "warn", "strict" or "off" explicitly
    "conflicts": true,

    // Use a number for spaces, '\t' for tabs, false or leave it out to disable
    "prettified": 4,

    // Here you can either pass true ("loose"), false, "strict", "loose" or an extended configuration object.
    "duplicates": {
        "mode": "warn", // Mode is now a sub-property
        "ignore": [

            // You can also use the array-sytax e.g. ["pages", "dashboard", "dashboard"]
            // If the specified target is an object it'll be skipped, e.g. you can ignore entire sub-trees
            "pages.dashboard.dashboard"
        ]
    }
}

Modes

Both --conflicts and --duplicates both come with a loose and strict mode. loose means that, in case there is something wrong with your files, an error won't be thrown. strict tells the linter that an error should be thrown.

For --conflicts it's normally strict as you will probably want to keep your files consistent and for --duplicates it's loose because translations may differ.

Example output:

Using --conflicts, --duplicates and --prettify on "corrupt" files would look like this (example):

example output

API Usage

This library comes in commonjs and ES6 format, you can include it directly:

<script src="https://cdn.jsdelivr.net/npm/li18nt/lib/li18nt.min.js"></script>

... or using es6 modules:

import {...} from 'https://cdn.jsdelivr.net/npm/li18nt/lib/li18nt.min.mjs'

You can use the exported lint function to lint a set of objects. Option- and result-types can be found here:

import {lint} from 'li18nt';

const options = {
    prettified: 4, // 4 spaces, use '\t' for tabs
    duplicates: true, // We want to analyze our translations for duplicates
    conflicts: true // Find differences
};

const objects = [
    {a: 20, b: null, c: {x: 20}},
    {a: 50, b: 'Hello', c: {x: 100, y: 20}},
    {a: 'Five', b: 'Super', c: null}
];

const result = lint(options, objects);
console.log(result);

Utilities

Sometimes you may want to exclude certain properties from being linted, for that you can either specify a property path as array (e.g. ['foo', 'bar', 4]), as a string (foo.bar[4]), or you can use the propertyPath utility function to convert a string to an array:

import {lint, propertyPath} from 'li18nt';

const options = {
    duplicates: {
        ignore: [
            // Info: This is normally not requried as strings in ignore will automatically be converted to an array!

            /**
             * Returns ['b', 'a'], but you can use any valid js-property-path e.g.
             * foo[3].bar.baz['Hello "world"'].xy
             * would give us ['foo', 3, 'bar', 'baz', 'Hello "world"'].xy
             */
            propertyPath('b.a')
        ]
    }
};

const objects = [
    {a: 20, b: {a: 20}, c: {a: 20}}
];

const result = lint(options, objects);

// Will log Map {'a' => [['a'], [ 'c', 'a']]}
// The first element in the array will always be the first appereance of that property
console.log(result.duplicates[0]);

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK