### Install rubocop-i18n gem Source: https://github.com/rubocop/rubocop-i18n/blob/master/README.md Instructions for adding the gem to a Ruby project via Gemfile or manual installation. ```ruby gem 'rubocop-i18n' ``` ```bash $ bundle $ gem install rubocop-i18n ``` -------------------------------- ### Configuring RuboCop I18n for GetText and Rails Source: https://context7.com/rubocop/rubocop-i18n/llms.txt A comprehensive YAML configuration example for .rubocop.yml, detailing settings for GetText and Rails I18n cops, including auto-correction and specific rule enforcement. ```yaml plugins: - rubocop-i18n I18n: Enabled: true I18n/GetText: Enabled: true I18n/GetText/DecorateString: Enabled: true AutoCorrect: true Exclude: - 'spec/**/*' - 'test/**/*' I18n/RailsI18n/DecorateString: Enabled: true IgnoreExceptions: false EnforcedSentenceType: sentence Exclude: - 'db/migrate/**/*' - 'config/**/*' ``` -------------------------------- ### Install rubocop-i18n Gem Source: https://context7.com/rubocop/rubocop-i18n/llms.txt Instructions for adding the rubocop-i18n gem to a Ruby project's Gemfile and installing it. This is the first step to enabling i18n checks. ```ruby # Gemfile gem 'rubocop-i18n' ``` ```bash # Install dependencies bundle install # Or install directly gem install rubocop-i18n ``` -------------------------------- ### Decorate Sentences with Rails I18n Methods (Ruby) Source: https://context7.com/rubocop/rubocop-i18n/llms.txt This cop identifies strings that appear to be sentences but are not wrapped in Rails I18n translation methods like `t()` or `translate()`. It supports configuration to ignore exception messages and enforce specific sentence structures (e.g., starting with an uppercase letter and ending with punctuation). ```ruby # bad - undecorated sentence "Result is bad." raise "Warning is bad." flash[:notice] = "User was updated successfully." # good - decorated with Rails I18n methods t("result_is_good") translate("result_is_good") I18n.t("result_is_good") flash[:notice] = t("users.update.success") # Configuration options in .rubocop.yml: # IgnoreExceptions: true - ignore strings in raise/fail calls # I18n/RailsI18n/DecorateString: # Enabled: true # IgnoreExceptions: true # Now this is OK: raise "Some error message" # EnforcedSentenceType options: # 'sentence' (default) - uppercase start + space + ending punctuation # 'fragmented_sentence' - uppercase start OR ending punctuation # 'fragment' - any series of words with spaces # I18n/RailsI18n/DecorateString: # Enabled: true # EnforcedSentenceType: sentence # Custom Regexp for matching # I18n/RailsI18n/DecorateString: # Enabled: true # Regexp: ^only-this-text$ # Only match this specific string # Example with different sentence types: # With EnforcedSentenceType: sentence # "Hello world." # detected (uppercase + space + punctuation) # "hello world." # ignored (no uppercase start) # With EnforcedSentenceType: fragmented_sentence # "Hello world" # detected (has uppercase start) # "hello world." # detected (has ending punctuation) # With EnforcedSentenceType: fragment # "hello world" # detected (multiple words) ``` -------------------------------- ### Decorate Raise/Fail Messages (GetText) Source: https://context7.com/rubocop/rubocop-i18n/llms.txt Ensures messages in `raise` and `fail` calls are decorated with gettext functions. Handles simple, multi-line, concatenated, and interpolated strings. Auto-correction is supported for simple string decoration. ```ruby # bad - simple undecorated message raise("Warning") fail("Something went wrong") # good - decorated message raise(_("Warning")) fail(_("Something went wrong")) # bad - multi-line message (causes translation issues) raise("this is a multi" \ "line message") # good - single line message raise(_("this is a multi line message")) # bad - concatenated message raise("this is a concatenated " + "message") # good - single decorated message raise(_("this is a concatenated message")) # bad - Ruby string interpolation variable = "critical" raise("this is an interpolated message: #{variable}") # good - gettext-style interpolation with named placeholders raise(_("this is an interpolated message: %{value}") % { value: variable }) # Example with exception class # bad raise ArgumentError, "Invalid parameter provided" # good raise ArgumentError, _("Invalid parameter provided") # Running RuboCop with this cop # $ rubocop --only I18n/GetText/DecorateFunctionMessage app/ # Example output: # app/services/payment.rb:42:7: C: I18n/GetText/DecorateFunctionMessage: # 'raise' function, message string should be decorated. ``` -------------------------------- ### Configure RuboCop for I18n Source: https://context7.com/rubocop/rubocop-i18n/llms.txt YAML configuration for .rubocop.yml to enable rubocop-i18n. It shows how to select either GetText or RailsI18n style checking by enabling the corresponding plugin and disabling the other. ```yaml # .rubocop.yml - Basic configuration plugins: - rubocop-i18n # Choose ONE framework style (GetText OR RailsI18n) # For GetText-style checking: I18n/GetText: Enabled: true I18n/RailsI18n: Enabled: false # OR for Rails I18n-style checking: # I18n/RailsI18n: # Enabled: true # I18n/GetText: # Enabled: false ``` -------------------------------- ### Decorate Undecorated Sentence Strings (GetText) Source: https://context7.com/rubocop/rubocop-i18n/llms.txt Detects and corrects undecorated sentence strings using gettext functions like `_()`. Sentences are identified by capitalization, spaces, and punctuation. Auto-correction wraps strings with `_()`. ```ruby # bad - undecorated sentence strings puts "Result is bad." message = "Welcome to our application!" status = "Operation completed successfully." # good - properly decorated strings puts _("Result is good.") message = _("Welcome to our application!") status = _("Operation completed successfully.") # ignored - strings that don't match sentence pattern "string" # no spaces "A string without punctuation at the end" # no ending punctuation "a string that doesn't start with capital." # lowercase start # Running RuboCop with this cop # $ rubocop --only I18n/GetText/DecorateString app/ # Example output: # app/models/user.rb:15:5: C: I18n/GetText/DecorateString: decorator is missing around sentence # "User was created successfully." # ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ``` -------------------------------- ### Disabling RuboCop I18n Cops Inline and via Exclusions Source: https://context7.com/rubocop/rubocop-i18n/llms.txt Demonstrates how to suppress specific i18n linting rules for individual lines, blocks of code, or entire files using inline comments and YAML configuration. ```ruby # Disable specific cops inline when needed raise("We don't want this translated.") # rubocop:disable I18n/GetText/DecorateString raise("Technical error log entry.") # rubocop:disable I18n/RailsI18n/DecorateString raise("Debug message") # rubocop:disable I18n/GetText/DecorateFunctionMessage # Disable interpolation cops when dynamic strings are intentional raise(_("Debug: #{debug_info}")) # rubocop:disable I18n/GetText/DecorateStringFormattingUsingInterpolation raise(_("Size: %d bytes") % [size]) # rubocop:disable I18n/GetText/DecorateStringFormattingUsingPercent # Disable for a block of code # rubocop:disable I18n/GetText/DecorateString LOG_MESSAGES = [ "Starting process...", "Process completed.", "Shutting down." ].freeze # rubocop:enable I18n/GetText/DecorateString # Configure exclusions in .rubocop.yml for entire files or directories I18n/GetText/DecorateString: Exclude: - 'spec/**/*' - 'db/seeds.rb' - 'lib/tasks/**/*' ``` -------------------------------- ### Configure rubocop-i18n in rubocop.yml Source: https://github.com/rubocop/rubocop-i18n/blob/master/README.md Configuration snippet for enabling or disabling specific I18n cops within the RuboCop configuration file. ```yaml plugins: - rubocop-i18n I18n/GetText: Enabled: true I18n/RailsI18n: Enabled: false I18n/GetText/DecorateString: Enabled: false AutoCorrect: false ``` -------------------------------- ### Enforce GetText string decoration Source: https://github.com/rubocop/rubocop-i18n/blob/master/README.md Demonstrates the I18n/GetText/DecorateString cop which ensures sentences are wrapped in the _() translation method. ```ruby # Bad "Result is bad." # Good _("Result is good.") ``` -------------------------------- ### Use Decorators for Rails i18n Strings Source: https://github.com/rubocop/rubocop-i18n/blob/master/README.md This cop checks for the correct usage of decorators around rails-i18n methods, ensuring that translated strings are properly enclosed. It flags instances where standard string formatting is used instead of translation functions like `t()`, `translate()`, or `I18n.t()`. Dependencies: Ruby, Rails i18n. ```ruby raise("Warning is %s" % ['bad']) ``` ```ruby raise(t("Warning is %{value}") % { value: 'good' }) ``` ```ruby raise(translate("Warning is %{value}") % { value: 'good' }) ``` ```ruby raise(I18n.t("Warning is %{value}") % { value: 'good' }) ``` -------------------------------- ### Ignoring RuboCop i18n Rules Source: https://github.com/rubocop/rubocop-i18n/blob/master/README.md Demonstrates how to disable specific RuboCop i18n cops for individual lines of code using standard RuboCop disable comments. This is useful for cases where i18n rules cannot be applied or are intentionally bypassed. Dependencies: Ruby, RuboCop. ```ruby raise("We don't want this translated.") # rubocop:disable I18n/GetText/DecorateString raise("We don't want this translated.") # rubocop:disable I18n/RailsI18n/DecorateString raise("We don't want this translated") # rubocop:disable I18n/GetText/DecorateFunctionMessage raise(_("We don't want this translated #{crazy}") # rubocop:disable I18n/GetText/DecorateStringFormattingUsingInterpolation) raise(_("We don't want this translated %s") % ['crazy'] # rubocop:disable I18n/GetText/DecorateStringFormattingUsingPercent) ``` -------------------------------- ### Enforce GetText decoration for raise/fail messages Source: https://github.com/rubocop/rubocop-i18n/blob/master/README.md Covers various rules for the I18n/GetText/DecorateFunctionMessage cop, including simple decoration, multi-line, concatenation, and interpolation requirements. ```ruby # Simple decoration raise(_("Warning")) # Multi-line handling raise(_("this is a multi line message")) # Interpolation handling raise(_("this is an interpolated message IE %{value0}") % {value0: var,}) ``` -------------------------------- ### Avoid Sprintf Formatting in GetText Decorators Source: https://github.com/rubocop/rubocop-i18n/blob/master/README.md This cop identifies and flags the use of sprintf-style string formatting (e.g., '%s') within decorated gettext methods like `_()`. It enforces the use of interpolation with named placeholders for safer and more maintainable internationalized strings. Dependencies: Ruby, gettext. ```ruby raise(_("Warning is %s") % ['bad']) ``` ```ruby raise(_("Warning is %{value}") % { value: 'bad' }) ``` -------------------------------- ### Avoid Interpolation in Gettext Formatting (Ruby) Source: https://context7.com/rubocop/rubocop-i18n/llms.txt This cop checks decorated gettext methods to ensure their strings do not use Ruby string interpolation (`#{} `). Interpolation can cause issues with translation extraction tools. It enforces the use of named placeholders for dynamic values. ```ruby # bad n_("Found #{count} result", "Found #{count} results", count) # good n_("Found %{num} result", "Found %{num} results", count) % { num: count } ``` -------------------------------- ### Avoid Interpolation in Decorated GetText Strings Source: https://context7.com/rubocop/rubocop-i18n/llms.txt Checks decorated gettext methods (`_()`, `n_()`, etc.) to prevent Ruby string interpolation (`#{}`). It enforces the use of gettext-style placeholders for translatable strings. ```ruby # bad - interpolation inside decorated string puts _("a message with a #{'interpolation'}") puts _("Hello #{user_name}, welcome!") puts _("Order ##{order_id} has been processed.") # good - use gettext-style placeholders puts _("a message that is %{type}") % { type: 'translatable' } puts _("Hello %{name}, welcome!") % { name: user_name } puts _("Order #%{id} has been processed.") % { id: order_id } # Supported gettext decorator functions: # _(), n_(), np_(), ns_(), N_(), Nn_() # D_(), Dn_(), Ds_(), Dns_() ``` -------------------------------- ### Prevent string interpolation in GetText Source: https://github.com/rubocop/rubocop-i18n/blob/master/README.md Demonstrates the I18n/GetText/DecorateStringFormattingUsingInterpolation cop, which forbids Ruby string interpolation '#{}' inside _() calls in favor of %{} formatting. ```ruby # Bad puts _("a message with a #{'interpolation'}") # Good puts _("a message that is %{type}") % { type: 'translatable' } ``` -------------------------------- ### Avoid Interpolation in Rails I18n Keys (Ruby) Source: https://context7.com/rubocop/rubocop-i18n/llms.txt This cop ensures that the translation key strings used with Rails I18n methods (like `t()`) do not contain Ruby string interpolation (`#{} `). Interpolated keys prevent I18n extraction tools from finding all necessary translation keys, potentially leading to missing translations. It recommends using static keys or explicit mapping for dynamic values. ```ruby # bad - interpolation in translation key puts t("status.#{status_string}") puts translate("errors.#{error_type}.message") puts I18n.t("users.#{action}.title") # bad - string concatenation in key puts t("status." + "accepted") puts t("prefix." + suffix) # good - static translation keys puts t("status.accepted") puts t("status.pending") puts t("status.rejected") # good - use I18n's built-in interpolation for dynamic values puts t("status.message", status: status_string) puts t("errors.generic.message", type: error_type) # When you need dynamic keys, use a lookup hash or case statement: # good - explicit key mapping # STATUS_KEYS = { # 'active' => 'status.active', # 'pending' => 'status.pending', # 'closed' => 'status.closed' # }.freeze # puts t(STATUS_KEYS[status_string]) # Supported decorator functions: t, t!, translate, translate! ``` -------------------------------- ### Avoid String Interpolation in Rails i18n Keys Source: https://github.com/rubocop/rubocop-i18n/blob/master/README.md This cop targets decorated rails-i18n methods (e.g., `t()`, `translate()`) and ensures that message keys do not contain Ruby string interpolation (`#{} `). It promotes the use of simple, static keys for translation lookup, improving i18n management. Dependencies: Ruby, Rails i18n. ```ruby puts t("path.to.key.with.#{'interpolation'}") ``` ```ruby puts t("path.to.key.with.interpolation") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.