How can I identify unused i18n keys?

后端 未结 5 1669
萌比男神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 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.

提交回复
热议问题