ruby debug_localization.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby debug_localization.rb相关的知识,希望对你有一定的参考价值。

# Allow some helpful debugging of localization issues. 
# You can see translations in the UI by adding the following (or like it) to your CSS.
# .translation_missing { background-color: red; color: white !important; }
# .translation_fallback { background-color: green; color: white !important; }
#
# You will also need to have the following in your development group in your Gemfile:
#   gem 'term-ansicolor'
#
# To turn this off, change this to false.
LOG_LOCALIZATION_ERRORS = true unless defined?(LOG_LOCALIZATION_ERRORS)



if Rails.env.development? && LOG_LOCALIZATION_ERRORS

  module I18n::Backend
    module Fallbacks
      require 'term/ansicolor'
      include Term::ANSIColor
      
      def translate(locale, key, options = {})
        return fallback_message(locale, key, super) if options[:fallback]

        default = extract_non_symbol_default!(options) if options[:default]

        options[:fallback] = true
        I18n.fallbacks[locale].each do |fallback|
          catch(:exception) do
            translation = super(fallback, key, options)
            return translation if fallback == locale

            result = fallback_message(locale, key, translation)
            return result unless result.nil?
          end
        end
        options.delete(:fallback)

        return super(locale, nil, options.merge(:default => default)) if default
        Rails.logger.debug red("--- I18n Missing Translation: #{locale}::#{key} ---")
        throw(:exception, I18n::MissingTranslation.new(locale, key, options))
      end

      def extract_non_symbol_default!(options)
        defaults = [options[:default]].flatten
        first_non_symbol_default = defaults.detect{|default| !default.is_a?(Symbol)}
        if first_non_symbol_default
          options[:default] = defaults[0, defaults.index(first_non_symbol_default)]
        end
        return first_non_symbol_default
      end

      def fallback_message(locale, key, fallback_text)
        return nil if fallback_text.nil?
        
        Rails.logger.debug yellow("--- I18n Fallback: #{locale}::#{key} ---")
        %(<span class="translation_fallback" title="translation missing: #{locale}, #{key}">#{fallback_text}</span>).html_safe
      end

    end
  end
end

以上是关于ruby debug_localization.rb的主要内容,如果未能解决你的问题,请参考以下文章

Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速

如何学习ruby?Ruby学习技巧分享

ruby Ruby脚本,看看是否用openssl编译了ruby

什么是ruby?

ruby和ruby ee

ruby入门知识:了解ruby历史及特性