i18n-js

repository·main·Indexed 25 days ago

https://github.com/fnando/i18n-js

A tool to export Ruby i18n translations to JSON format for use in JavaScript applications. It includes a CLI for exporting and linting, a Ruby API via I18nJS.call, and support for custom plugins. Features include translation pattern filtering, fallback embedding, and integration with Watchman, Guard, and Listen for automatic exports. The linting tools can identify missing translation keys across locales or verify that translation calls in JS/TS files have corresponding keys in the exported JSON.

Tokens
7.1K
Snippets
26
Records
55
Agent score
87%

What's inside i18n-js

  1. Upgrade the i18n configuration file (v3 to v4)

    main

    The structure of the config/i18n.yml file has changed in v4. The only and except keys from v3 are replaced by a single patterns key. In v4, exclusions are handled using the ! prefix within the patterns list.

    Example Migration:

    v3 (Old):

    translations:
      - file: "app/assets/javascripts/other.js"
        only: ["*.activerecord", "*.admin.*.title"]
      - file: "app/assets/javascripts/everything_else.js"
        except:
          - "*.activerecord"

    v4 (New):

    translations:
      - file: "app/assets/javascripts/other.js"
        patterns:
          - "*.activerecord"
          - "*.admin.*.title"
      - file: "app/assets/javascripts/everything_else.js"
        patterns:
          - "*"
          - "!*.activerecord"
    translations:
      - file: "app/assets/javascripts/date_formats.js"
        patterns:
          - "*.date.formats"
      - file: "app/assets/javascripts/other.js"
        patterns:
          - "*.activerecord"
          - "*.admin.*.title"
      - file: "app/assets/javascripts/everything_else.js"
        patterns:
          - "*"
          - "!*.activerecord"
          - "!*.admin.*.title"
          - "!*.date.formats"
  2. Automatically export translations using Guard

    main

    To use guard for automatic exports, install guard and guard-compat, then create a Guardfile with the following configuration. Ensure you adjust the file paths if your locales are not in the standard app/config/locales directory.

    guard(:"i18n-js",
          run_on_start: true,
          config_file: "./config/i18n.yml",
          require_file: "./config/environment.rb") do
      watch(%r{^(app|config)/locales/.+\.(yml|po)$})
      watch(%r{^config/i18n.yml$})
      watch("Gemfile")
    end
  3. Migrate configuration to the `pipeline:` API

    main

    When migrating to the pipeline: API, move plugin configurations from the top level of your configuration file (e.g., config/i18n.yml) into a single pipeline: array.

    Key changes:

    • Plugins are now listed as objects within the pipeline: array.
    • Each entry must include a plugin: key specifying the plugin name.
    • The order of entries in the pipeline: array determines the execution order.
    • Plugin-specific options (like files: for export_files) must be moved inside the pipeline stage object, alongside plugin: and enabled:.
    translations:
      - file: app/javascript/locales/%{locale}.json
        patterns:
          - "*"
    
    pipeline:
      - plugin: embed_fallback_translations
        enabled: true
    
      - plugin: export_files
        enabled: true
        files:
          - template: path/to/template.erb
            output: "%{dir}/%{base_name}.ts"
  4. Disable a plugin in the pipeline

    main

    To temporarily disable a plugin without removing it from your configuration, set enabled: false on its specific pipeline stage. This allows you to re-enable it easily later.

    pipeline:
      - plugin: embed_fallback_translations
        enabled: false   # temporarily disabled
  5. Export translations using the i18n CLI

    main
    In v4, the build process relies on an external CLI called i18n. You must execute the i18n export command during your build step to generate the JSON files for your translations. This command should be run as part of a precompilation step for production; do not run automated file-watching export tools in a production environment.
    i18n export
  6. Automatically export translations using Watchman

    main

    You can use watchman to trigger the i18n export command whenever locale files or the configuration file change.

    1. Create a script at bin/i18n-watch with the Watchman trigger configuration.
    2. Make it executable: chmod +x bin/i18n-watch.
    3. Run it: ./bin/i18n-watch.

    If using Foreman, uncomment the while true loop in the script to keep the process running and add it to your Procfile:

    i18n: ./bin/i18n-watch
    #!/usr/bin/env bash
    
    root=`pwd`
    
    watchman watch-del "$root"
    watchman watch-project "$root"
    watchman trigger-del "$root" i18n
    
    watchman -j <<-JSON
    [
      "trigger",
      "$root",
      {
        "name": "i18n",
        "expression": [
          "anyof",
          ["match", "config/locales/**/*.yml", "wholename"],
          ["match", "config/i18n.yml", "wholename"]
        ],
        "command": ["i18n", "export"]
      }
    ]
    JSON
  7. Migrate plugin configuration to the pipeline array

    main

    Plugin configuration has moved from top-level YAML keys to a pipeline: array. Each entry in the array must include a plugin: key (using the snake_case name of the plugin) and the plugin's specific configuration keys.

    Example Configuration Transformation:

    Before:

    embed_fallback_translations:
      enabled: true
    
    export_files:
      enabled: true
      files:
        - template: templates/export.erb
          output: "%{dir}/%{base_name}-%{digest}.ts"

    After:

    pipeline:
      - plugin: embed_fallback_translations
        enabled: true
    
      - plugin: export_files
        enabled: true
        files:
          - template: templates/export.erb
            output: "%{dir}/%{base_name}-%{digest}.ts"
  8. Export translations without a database

    main

    If you are running in an environment without a database (e.g., a Docker build process), you can use the --require flag with the i18n export command to provide a custom loading file. This allows you to manually load the necessary files (like ActiveSupport or specific locale paths) required for the export to function.

    i18n export --require ./config/i18n_export.rb
  9. Automatically export translations using Listen

    main

    You can use the i18n-js/listen integration to watch for changes in a Rails application. Create a file at config/initializers/i18n.rb and call I18nJS.listen within an after_initialize block.

    Configuration Options for I18nJS.listen

    • config_file: The i18n-js configuration file path.
    • locales_dir: One or multiple directories to watch for locale changes.
    • options: Options passed directly to the listen gem.
    • run_on_start: Whether to export files on start. Defaults to true.
    Rails.application.config.after_initialize do
      require "i18n-js/listen"
      I18nJS.listen
    end