How can I identify unused i18n keys?

后端 未结 5 1618
萌比男神i
萌比男神i 2021-02-04 03:42

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

相关标签:
5条回答
  • 2021-02-04 03:58

    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.

    0 讨论(0)
  • 2021-02-04 04:03

    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.

    0 讨论(0)
  • 2021-02-04 04:14

    I18n-tasks gem

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

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

    0 讨论(0)
  • 2021-02-04 04:18

    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.

    0 讨论(0)
  • 2021-02-04 04:19

    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

    0 讨论(0)
提交回复
热议问题