i18n-tasks Documentation

repository·main·Indexed 24 days ago

https://github.com/glebm/i18n-tasks

A tool for managing Ruby i18n translations. i18n-tasks provides utilities to find missing or unused keys, automate translations via backends like Google Translate, DeepL, OpenAI, and watsonx, and maintain normalized locale files through static analysis of the codebase. It supports Rails and standard Ruby projects, offering commands for health checks, key reorganization, and CSV import/export.

Tokens
10.4K
Snippets
28
Records
93
Agent score
79%

What's inside i18n-tasks

  1. How translation routers work

    main

    i18n-tasks uses routers to determine how and where translation keys are written to files. There are three types:

    1. Conservative router (Default): Keeps keys where they are found or infers the path from the base locale. If a key is entirely new, it falls back to pattern_router behavior.
    2. Pattern router: Organizes keys based on a list of key patterns. Matches are processed from top to bottom.
    3. Isolating router: Assumes each YAML file is independent. It writes translations to an alternate target file for each source file (only changing the %{locale} part). This is useful for avoiding collisions in setups like ViewComponent sidecars, though it cannot detect missing keys from code usage due to lack of scope awareness.

    To reorganize existing keys using a pattern router, use the normalize -p command.

    # Example Pattern Router configuration
    data:
      router: pattern_router
      write:
        - ["{models,views}.*", 'config/locales/\1.%{locale}.yml']
        - ["{:}.*", 'config/locales/\1.%{locale}.yml']
        - "config/locales/%{locale}.yml"
  2. Handle dynamic translation keys

    main

    By default, i18n-tasks does not recognize dynamic keys like t "cats.#{cat}.name". You have two options for handling these:

    1. Use hints (Recommended): Mark these keys with i18n-tasks-use hints to explicitly tell the tool they are used.
    2. Enable dynamic key inference: Set search.strict to false in your configuration.

    Warning on inference: When search.strict is false, only one section of the key is treated as a wildcard per string interpolation. For example, if cats.#{cat}.name is interpolated, cats.tenderlove.name will be marked as used, but cats.tenderlove.special.name will still be reported as unused.

  3. Supported key types and limitations

    main

    Supported

    • Relative keys: Keys relative to the file path (e.g., t '.title') and keys relative to controller.action_name in Rails controllers.
    • Plural keys: Keys using pluralization syntax like key.{one,many,other,...}.
    • Reference keys: Keys containing :symbol values are supported. They can be looked up by reference or value in find and are copied as-is during add/translate-missing.
    • t() keyword arguments: The scope argument is fully supported. The default argument is supported by the AST scanner for pre-filling locale files.

    Limitations

    • Dynamic keys: Not recognized by default (see 'Handle dynamic translation keys' for solutions).
    • I18n.localize: Not supported because the generated key depends on the object type and cannot be determined statically. Use i18n-tasks-use hints instead.
    • YAML Normalization: Because the tool uses Psych, multi-line strings might undergo unexpected normalization (e.g., changing from literal block | to quoted "" if trailing spaces are present).
  4. Compose i18n-tasks using pipes

    main

    Tasks in i18n-tasks are composable. You can pipe the output of one task into another to perform complex manipulations.

    Examples:

    1. Add missing keys with a template: missing (finds keys) $\rightarrow$ tree-set-value (sets values) $\rightarrow$ data-merge (writes to files).

    2. Remove unused keys without confirmation: unused $\rightarrow$ data-remove.

    3. Remove keys from a target locale that don't exist in the base locale: missing (finds diff) $\rightarrow$ tree-mv (moves keys to target) $\rightarrow$ data-remove (removes them from target).

    # Add missing keys with a template
    $ bundle exec i18n-tasks missing -f yaml fr | bundle exec i18n-tasks tree-set-value 'TRME %{value}' | bundle exec i18n-tasks data-merge
    
    # Remove unused keys
    $ bundle exec i18n-tasks unused -f yaml | bundle exec i18n-tasks data-remove
    
    # Remove all keys from 'fr' that do not exist in 'en'
    $ bundle exec i18n-tasks missing -t diff -f yaml en | bundle exec i18n-tasks tree-mv en fr | bundle exec i18n-tasks data-remove
  5. Quick Start with i18n-tasks

    main

    To set up i18n-tasks in a Ruby project using the i18n gem (such as Rails), follow these steps:

    1. Add the gem to your Gemfile in the :development group:
      gem 'i18n-tasks', '~> 1.1.2', group: :development
    2. Copy the default configuration template to your config/ directory:
      $ cp $(bundle exec i18n-tasks gem-path)/templates/config/i18n-tasks.yml config/
    3. Run your first health check:
      $ bundle exec i18n-tasks health

    To automate translation checks in CI, you can also copy the provided test templates for RSpec or Minitest:

    # RSpec
    $ cp $(bundle exec i18n-tasks gem-path)/templates/rspec/i18n_spec.rb spec/
    
    # Minitest
    $ cp $(bundle exec i18n-tasks gem-path)/templates/minitest/i18n_test.rb test/
  6. Use the i18n-tasks interactive console

    main

    You can start an IRB session within the i18n-tasks context to interact with the tool programmatically. Once inside the session, you can type guide to see more information.

    bundle exec i18n-tasks irb
  7. Configure i18n-tasks

    main

    Configuration is managed via config/i18n-tasks.yml or config/i18n-tasks.yml.erb. You can inspect your current configuration by running bundle exec i18n-tasks config. Settings are compatible with Rails by default.

    To install the default configuration template, run:

    $ cp $(bundle exec i18n-tasks gem-path)/templates/config/i18n-tasks.yml config/
  8. Fine-tune translation detection with magic comments

    main

    You can provide hints to the static analysis scanner using magic comments. This tells i18n-tasks that a specific key is intentionally used, preventing it from being flagged as unused.

    Use the following syntax in your Ruby files:

    # i18n-tasks-use t('activerecord.models.user') # let i18n-tasks know the key is used
    User.model_name.human
    # i18n-tasks-use t('activerecord.models.user')
    User.model_name.human
  9. Tree-based translation manipulation commands

    main

    The tree command group allows for advanced manipulation of translation forests (hierarchical structures of keys and values) via the CLI. These commands can operate on files provided as positional arguments or via stdin. Most commands support specifying a data format (e.g., YAML, JSON) to control input and output.

    Commonly used flags across these commands:

    • -a, --all-locales: Used in specific commands like tree_mv to determine if the operation should be scoped to all locales or just the root.
  10. Configure Google Translate backend

    main

    To use Google Translate for translate-missing, provide an API key via the google_translate_api_key config option or the GOOGLE_TRANSLATE_API_KEY environment variable.

    translation:
      backend: google
      google_translate_api_key: <Google Translate API key>