i18n-debug

repository·master·Indexed 41 days ago

https://github.com/fphilipe/i18n-debug

A Ruby gem for identifying I18n keys looked up by Rails or other gems. It provides real-time logging of translation keys and resolved values, and allows for custom logging via I18n::Debug.logger and custom lookup hooks via I18n::Debug.on_lookup.

Tokens
774
Snippets
5
Records
6
Agent score
2%

What's inside i18n-debug

  1. Understand the default debug log format

    master

    When using the default logger, the gem outputs debug entries in the following format:

    [i18n-debug] <key> => <value>

    If a translation is missing, the value will be nil: [i18n-debug] activerecord.attributes.order/billing_address.name => nil

    Once the translation is defined, it shows the resolved string: [i18n-debug] activerecord.attributes.order/billing_address.name => "Order billing name"

  2. Install i18n-debug

    master

    Add the gem to your Gemfile. It is recommended to place it within the development group to avoid overhead in production environments.

    Note: If you are using Ruby version 2.0 or lower, you must use version 1.0.0 of the gem.

    gem 'i18n-debug', group: :development
  3. Configure a custom logger for i18n-debug

    master

    By default, the gem uses Rails.logger.debug if Rails is present, or Logger.new($stdout).debug otherwise. You can override this by assigning a custom logger object to I18n::Debug.logger. The object must respond to the debug method.

    I18n::Debug.logger = my_custom_logger
  4. Use I18n::Debug.on_lookup to intercept translation lookups

    master

    The gem provides a hook via I18n::Debug.on_lookup. Every time a translation is looked up, this lambda is invoked with two arguments: key (the translation key) and value (the translation value found).

    You can use this to change the logging format or to collect custom statistics about which keys are being used in your application.

    # Example: Collect stats on I18n key usage.
    i18n_stats = Hash.new { |stats, key| stats[key] = 0 }
    
    I18n::Debug.on_lookup do |key, value|
      i18n_stats[key] += 1 if value
    end
  5. Configure the I18n::Debug logger

    master

    You can customize where the debug information is sent by assigning a logger instance to I18n::Debug.logger. By default, if Rails is defined, it uses Rails.logger; otherwise, it initializes a new Logger instance pointing to $stdout.

    # Example: Redirecting debug output to a specific file
    require 'logger'
    I18n::Debug.logger = Logger.new('i18n_debug.log')
  6. Register a custom lookup hook with I18n::Debug.on_lookup

    master

    Use I18n::Debug.on_lookup to define a custom callback that executes whenever an i18n key is looked up.

    • To get the current hook: Call I18n::Debug.on_lookup without a block.
    • To set a new hook: Pass a block to I18n::Debug.on_lookup. The block receives two arguments: key (the normalized string representation of the key) and result (the value returned by the lookup).

    By default, the library logs the key and the inspected result to the configured logger.

    # Example: Customizing the output format of lookups
    I18n::Debug.on_lookup do |key, result|
      puts "DEBUG: Found [#{key}] with value: #{result}"
    end