How can I identify unused i18n keys?

让人想犯罪 __ 提交于 2019-12-20 12:31:18

问题


I'm working on an existing Rails app and am using a localization file, en.yml, to hold most of the app's text. At the moment, we aren't localizing into any other languages, so there's just the one file, but the fact that we're putting translate('some.key') into our views means that adding another language will be as simple as adding another file - say, sp.yml

The problem is, en.yml has grown to the point that I doubt all the keys are being used.

Apart from git grepping for translate calls using each key, is there a quick way to identify localization keys that aren't being explicitly called by the app?


回答1:


Take a look at this article about "Key issues internationalizing your app". The paragraph that interests you is: "Getting rid of unused translations".

Specifically, it recommends looking through your source code and also logging what translation keys get used in your production app, as follows:

module I18n
  module Registry
    protected
    def lookup(locale, key, scope = [], options = {})
      @log ||= Logger.new(File.join(Rails.root, 'log', 'i18n_registry.log'))
      @log.info key
      super
    end
  end
end

I18n::Backend::Simple.send :include, I18n::Registry

Hope that helps.




回答2:


I18n-tasks gem

I just heard about this gem, which includes a task to show "potentially unused translations".

https://github.com/glebm/i18n-tasks




回答3:


It's been many years since I first arrived to this question as I had the exact same problem. The problem hasn't grown smaller, I am more frustrated than ever.

Here is an experimental project, it hooks into the translate lookup and increments the translation key counter in Redis:

https://github.com/paladinsoftware/i18n-counter

The idea is that you can pull the stats and compare. (WIP for the moment, I would love help ofc)

You may ask: "won't that slow down the lookups?"

And you are right of course, but the overhead is hardly noticeable, check out this benchmark.

require 'benchmark'
n = 100000
Benchmark.bm do |x|
  x.report { ENV['ENABLE_I18N_COUNTER'] = 'true'; n.times do ; I18n.translate('application.contract_not_available.header'); end }
  x.report { ENV['ENABLE_I18N_COUNTER'] = 'false'; n.times do ; I18n.translate('application.contract_not_available.header'); end }
end

 ---------------------------------------------
| Benchmark  | Seconds   | Sec pr translation |
|------------| --------- | ------------------ |
| with redis | 48.280000 | 0.0004828          |
| without    |  9.010000 | 0.0000901          |
 ---------------------------------------------

The overhead being about 3 ms pr lookup. It boils down to the number of lookups you do per page/request.




回答4:


Get the ones that are actively used, then remove the rest. That's what I use.

Actually I set them to active=0 but that may not work for you

Update
Turns out I was unclear.

There are two ways to look at this: from the source files or from the translation files. If you look from the source files you need to identify all strings that are in use and finally remove all unused strings.

If you look from the translation files, you need to look at the source and determine whether they are still used, as you mentioned in the question.

There's no other way.




回答5:


You might want to try

$ ruby script/plugin install http://github.com/o2sources/unused_translations/tree/master
$ script/unused_translations config/locales/en.yml 

Source: http://www.railslodge.com/plugins/1547-unused-i18n-translations



来源:https://stackoverflow.com/questions/8660613/how-can-i-identify-unused-i18n-keys

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!