How can I identify unused i18n keys?
Asked Answered
W

5

23

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?

Wobble answered 28/12, 2011 at 19:56 Comment(0)
G
16

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.

Genital answered 18/4, 2012 at 14:27 Comment(0)
W
10

I18n-tasks gem

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

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

Wobble answered 12/9, 2013 at 10:59 Comment(3)
Potentially unused in a sense that it will give false positives for hard to detect cases, e.g. I18n.t(method_returning_the_key). It is possible to ignore such keys with config/i18n-tasks.yml.Purkey
@Purkey - Thanks for the clarification. By the way, how did you find this mention of your gem so quickly?Wobble
I searched for the name on SO :)Purkey
D
1

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.

Dannettedanni answered 28/9, 2017 at 14:50 Comment(0)
M
-1

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.

Mcclish answered 28/12, 2011 at 20:10 Comment(6)
I don't understand what you mean. I'm talking about lines in a file. Are you talking about database rows?Wobble
I don't have any experience with rails, but most i18n solutions I know have a way of exporting all translatable strings. Export all translatable strings, then loop through all strings and check which ones aren't used anymore (look in your export).Mcclish
I'm not asking how to compare two localization files; I'm asking how to check which localization keys are used by the app's code.Wobble
And I'm answering that question :-) One file uses the translations, the other file holds the translations.Mcclish
"The other file" is my application code, which spans dozens of files. I'm really looking for a Rails-specific solution; perhaps a gem which exercises the app code and looks for calls to the translate method.Wobble
Then how did you generate the translation files in the first place? Didn't you export them with a tool like xgettext? If you manually moved all translatable strings to a file then you usually end up doing everything manually. If you used standard tools like xgettext, these things become a lot easier.Mcclish
B
-2

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

Bis answered 23/2, 2012 at 15:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.