Do not show I18n missing translation tooltips in production

Problem:
Lots of stuff does not need any translation, so we simply leave it untranslated (e.g. firstname shows as Firstname). But this leaves ugly “missing translation” spans in out html.
Instead of entering senseless translations for everything thats missing, we simply deactivate the tooltips in production, while also avoiding raising/rescuing theses MissingTranslation errors to improve performance(rescue/raise is not cheap).

Code:

# https://grosser.it/2012/02/04/do-not-show-i18n-missing-translation-tooltips-in-production/
# config/initializers/disable_i18n_tooltips.rb
# missing translations
# - Do not show tooltips in production/test
# - Do not raise ( speedup) for every missing translations
I18n::Backend::Base.class_eval do
  def translate_with_default(locale, key, options = {})
    if options[:rescue_format] == :html && ['test','production'].include?(Rails.env)
      default = key.to_s.gsub('_', ' ').gsub(/\b('?[a-z])/) { $1.capitalize }
      options.reverse_merge!(default: default)
    end
    translate_without_default(locale, key, options)
  end

  alias_method_chain :translate, :default
end

3 thoughts on “Do not show I18n missing translation tooltips in production

  1. many thanks for this solution it helped me alot translating my current project
    but it is missing one trick
    the default variable should be only the last word of translatation part
    so we need to add one line after declare defualt variable
    default = default.split(“.”).last

    ###########################################################

    so the whole code will be

    I18n::Backend::Base.class_eval do
    def translate_with_default(locale, key, options = {})
    if options[:rescue_format] == :html && [‘test’,’production’,’development’].include?(Rails.env)
    default = key.to_s.gsub(‘_’, ‘ ‘).gsub(/\b(‘?[a-z])/) { $1.capitalize }
    default = default.split(“.”).last #the new line
    options.reverse_merge!(default: default)
    end
    translate_without_default(locale, key, options)
    end

    alias_method_chain :translate, :default
    end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s