Reputation: 126122
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?
Upvotes: 23
Views: 8631
Reputation: 40900
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.
Upvotes: 1
Reputation: 5095
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.
Upvotes: 16
Reputation: 126122
I just heard about this gem, which includes a task to show "potentially unused translations".
https://github.com/glebm/i18n-tasks
Upvotes: 10
Reputation: 1045
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
Upvotes: -2
Reputation: 29985
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.
Upvotes: -1