I18n:如何检查翻译键/值对是否缺失?

use*_*882 17 ruby ruby-on-rails internationalization ruby-on-rails-3 ruby-on-rails-3.1

我正在使用Ruby on Rails 3.1.0和I18n gem.我(正在实现一个插件)我想在运行时检查I18n是否缺少翻译键/值对,如果是,则使用自定义字符串.也就是说,我有:

validates :link_url,
  :format     => {
    :with => REGEX,
    :message  => I18n.t(
      'custom_invalid_format',
      :scope => 'activerecord.errors.messages'
  )
}
Run Code Online (Sandbox Code Playgroud)

如果在.yml文件中没有以下代码

activerecord:
  errors:
    messages:
      custom_invalid_format: This is the test error message 1
Run Code Online (Sandbox Code Playgroud)

我想用This is the test error message 2.可能吗?如果是这样,我该怎么做?

顺便说一句:出于性能原因,建议在运行时检查翻译键/值对是否存在?

Eya*_*alB 23

您可以将:default参数传递给I18n.t:

I18n.t :missing, :default => 'Not here'
# => 'Not here'
Run Code Online (Sandbox Code Playgroud)

你可以在这里阅读更多相关信息.


小智 20

我只是有同样的问题,我想计算一个自动字符串,以防翻译丢失.如果我使用:default选项,我每次都要计算自动字符串,即使翻译没有丢失.所以我搜索了另一个解决方案.

您可以添加选项:raise => true或使用I18n.translate!而不是I18n.translate.如果找不到翻译,则会引发异常.

begin
  I18n.translate!('this.key.should.be.translated', :raise => true) 
rescue I18n::MissingTranslationData
  do_some_resource_eating_text_generation_here
end
Run Code Online (Sandbox Code Playgroud)


Viv*_*123 16

我不知道如何在运行时,但你可以使用rake找到它.你将为此创建自己的rake任务.这是一个:

namespace :i18n do
  desc "Find and list translation keys that do not exist in all locales"
  task :missing_keys => :environment do

    def collect_keys(scope, translations)
      full_keys = []
      translations.to_a.each do |key, translations|
        new_scope = scope.dup << key
        if translations.is_a?(Hash)
          full_keys += collect_keys(new_scope, translations)
        else
          full_keys << new_scope.join('.')
        end
      end
      return full_keys
    end

    # Make sure we've loaded the translations
    I18n.backend.send(:init_translations)
    puts "#{I18n.available_locales.size} #{I18n.available_locales.size == 1 ? 'locale' : 'locales'} available: #{I18n.available_locales.to_sentence}"

    # Get all keys from all locales
    all_keys = I18n.backend.send(:translations).collect do |check_locale, translations|
      collect_keys([], translations).sort
    end.flatten.uniq
    puts "#{all_keys.size} #{all_keys.size == 1 ? 'unique key' : 'unique keys'} found."

    missing_keys = {}
    all_keys.each do |key|

      I18n.available_locales.each do |locale|
        I18n.locale = locale
        begin
          result = I18n.translate(key, :raise => true)
        rescue I18n::MissingInterpolationArgument
          # noop
        rescue I18n::MissingTranslationData
          if missing_keys[key]
            missing_keys[key] << locale
          else
            missing_keys[key] = [locale]
          end
        end
      end
    end
    puts "#{missing_keys.size} #{missing_keys.size == 1 ? 'key is missing' : 'keys are missing'} from one or more locales:"
    missing_keys.keys.sort.each do |key|
      puts "'#{key}': Missing from #{missing_keys[key].join(', ')}"
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

将给定的.rake文件放在lib/tasks目录中并执行:

rake i18n:missing_keys 
Run Code Online (Sandbox Code Playgroud)

信息来源是在这里和代码在GitHub上这里.