i18next-parser

repository·master·Indexed 20 days ago

https://github.com/i18next/i18next-parser

A command line tool for i18next (version 9.3.0) designed to automate the maintenance of translation catalogs by parsing source code for translation keys. It supports various file types via specialized lexers for JavaScript, JSX, HTML, and Handlebars, and integrates with CLI, Gulp, and Broccoli workflows. Note: This package is no longer maintained as of September 2025; migration to i18next-cli is recommended.

Tokens
5.3K
Snippets
26
Records
30
Agent score
69%

What's inside i18next-parser

  1. Configure Lexers for different file types

    master

    The lexers option maps file extensions to specific Lexer classes. There are four primary lexers available:

    • JavascriptLexer: Used for .js, .mjs, .ts files. It uses the TypeScript compiler to walk the code.
    • JsxLexer: Extends the Javascript lexer with support for JSX syntax (used for .jsx, .tsx).
    • HTMLLexer: Used for .html and .htm files.
    • HandlebarsLexer: Used for .hbs and .handlebars files.

    If you use .js files for JSX (common in Create React App), you must override the js lexer to use JsxLexer.

    // Example: Enabling JSX parsing in .js files
    {
      js: [{ lexer: 'JsxLexer' }],
    }
  2. Customize translation function detection

    master

    To extract keys from custom function calls (like __('key') or _e('key')), you must configure the functions property within the specific lexer in your configuration.

    Configuration Example

    {
      lexers: {
        js: [
          {
            lexer: 'JavascriptLexer',
            functions: ['t', 'TAPi18n.__', '__'],
          },
        ]
      }
    }

    Note: The parser does not match the closing parenthesis to allow for arguments to be passed to the function, and it handles escaped single or double quotes within keys automatically.

  3. Migrate default value configuration from 5.x to 6.x

    master

    In version 6.x, the skipDefaultValues and useKeysAsDefaultValues options were deprecated in favor of a single defaultValue function. This function provides complete control over the logic by accepting (locale, namespace, key, value) as arguments.

    To replace skipDefaultValues: true:

    {
      defaultValue: function (locale, namespace, key, value) {
        return '';
      }
    }

    To replace useKeysAsDefaultValues: true:

    {
      defaultValue: function (locale, namespace, key, value) {
        return key;
      }
    }

    For custom logic (e.g., different behavior for specific locales):

    {
      defaultValue: function (locale, namespace, key, value) {
        if (locale === 'fr') {
          return '';
        }
        return value || key;
      }
    }
    // 6.x.x
    {
      defaultValue: function (locale, namespace, key, value) {
        if (locale === 'fr') {
          return '';
        }
        return value || key;
      }
    }
  4. Install i18next-parser via npm or yarn

    master

    You can install i18next-parser globally to use the CLI from anywhere, or locally as a development dependency for Gulp or Broccoli integrations.

    # Global installation
    yarn global add i18next-parser
    npm install -g i18next-parser
    
    # Local development dependency
    yarn add -D i18next-parser
    npm install --save-dev i18next-parser
  5. Configure multiple locales

    master

    To generate separate directories for each locale (e.g., locales/en/, locales/de/), provide an array of locale strings to the locales option.

    CLI

    Add the locales key to your configuration file:

    {
      locales: ['en', 'de', 'sp']
    }

    Gulp

    Pass the locales array within the i18next plugin configuration:

    .pipe(i18next({ locales: ['en', 'de', 'sp'] }))
  6. Set a default namespace

    master

    To group all translations into a specific namespace file (e.g., locales/en/my_default_namespace.json), use the namespace option.

    CLI

    Add the namespace key to your configuration file:

    {
      namespace: 'my_default_namespace'
    }

    Gulp

    Pass the namespace string to the i18next plugin:

    pipe(i18next({ namespace: 'my_default_namespace' }))
  7. Parse TypeScript generic types for keys

    master

    If you use TypeScript generics to define translation keys (e.g., const MyKey T<{count: number}>('my_key')), you can enable parseGenerics and provide a typeMap to ensure the parser correctly identifies these keys.

    Configuration Example

    {
      lexers: {
        js: [
          {
            lexer: 'JavascriptLexer',
            parseGenerics: true,
            typeMap: { CountType: { count: '' } },
          },
        ]
      }
    }

    This allows the parser to detect patterns like:

    const MyKey T<{count: number}>('my_key');
    const MyOtherKey = T<CountType>('my_other_key');
  8. Migrate CLI syntax from 0.x to 1.x

    master

    The CLI syntax changed from a positional input:output pattern to using the --output option and glob patterns for filtering. The recursive, directoryFilter, and fileFilter flags were deprecated in favor of passing globs directly.

    Old syntax (0.x):

    i18next src --recursive --fileFilter '*.hbs,*.js' --directoryFilter '!.git'

    New syntax (1.x+):

    i18next 'src/**/*.{js,hbs}' '!.git'
    i18next 'src/**/*.{js,hbs}' '!.git'
  9. Configure namespace and key separators

    master

    You can customize how the parser identifies namespaces and subkeys within a string (e.g., parsing namespace?key_subkey into a nested JSON object). Use namespaceSeparator and keySeparator in your configuration.

    CLI

    {
      namespaceSeparator: '?',
      keySeparator: '_'
    }

    Gulp

    .pipe(i18next({namespaceSeparator: '?', keySeparator: '_'}))
  10. Change the output directory

    master

    You can specify where the generated translation files are created using the output option. The parser supports placeholders like $LOCALE and $NAMESPACE to dynamically name files and folders.

    Command Line

    Use the -o flag or the : syntax:

    $ i18next /path/to/file/or/dir -o /translations/$LOCALE/$NAMESPACE.json
    $ i18next /path/to/file/or/dir:/translations/$LOCALE/$NAMESPACE.json

    Gulp

    When using Gulp, ensure you call .dest() to actually write the files to the filesystem:

    .pipe(i18next({ output: 'translations/$LOCALE/$NAMESPACE.json' }))
    $ i18next /path/to/file/or/dir -o /translations/$LOCALE/$NAMESPACE.json
  11. Configure i18next-parser via a config file

    master

    You can use a configuration file (e.g., i18next-parser.config.js) to control how the parser treats your files. This allows you to define input/output paths, locales, separators, and lexer settings.

    Key configuration options include:

    • input: An array of globs describing source files relative to the config file.
    • output: Path template for locale files (supports $LOCALE and $NAMESPACE injection, e.g., 'locales/$LOCALE/$NAMESPACE.json').
    • locales: An array of locales used in your application (e.g., ['en', 'fr']).
    • keySeparator and namespaceSeparator: Define the characters used to split keys (e.g., . and :). Set both to false if using plain English keys to avoid conflicts.
    • defaultValue: The default value for keys with no value. Can be a string or a function.
    • keepRemoved: Determines if keys no longer present in code should be kept in the catalog. Can be a boolean or an array of patterns.
    • failOnUpdate: If true, the process exits with code 1 when translations are updated (useful for CI).
    // i18next-parser.config.js
    export default {
      contextSeparator: '_',
      createOldCatalogs: true,
      defaultNamespace: 'translation',
      defaultValue: '',
      indentation: 2,
      keepRemoved: false,
      keySeparator: '.',
      lexers: {
        hbs: ['HandlebarsLexer'],
        handlebars: ['HandlebarsLexer'],
        htm: ['HTMLLexer'],
        html: ['HTMLLexer'],
        mjs: ['JavascriptLexer'],
        js: ['JavascriptLexer'],
        ts: ['JavascriptLexer'],
        jsx: ['JsxLexer'],
        tsx: ['JsxLexer'],
        default: ['JavascriptLexer'],
      },
      lineEnding: 'auto',
      locales: ['en', 'fr'],
      namespaceSeparator: ':',
      output: 'locales/$LOCALE/$NAMESPACE.json',
      pluralSeparator: '_',
      input: undefined,
      sort: false,
      verbose: false,
      failOnWarnings: false,
      failOnUpdate: false,
      customValueTemplate: null,
      resetDefaultValueLocale: null,
      i18nextOptions: null,
      yamlOptions: null,
    }