如何识别未使用的i18n密钥?

时间:2022-05-17 07:32:11

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

我正在开发一个现有的Rails应用程序,并使用一个本地化文件en。yml,用来保存应用程序的大部分文本。目前,我们没有本地化到任何其他语言,所以只有一个文件,但我们将翻译('some.key')放入视图中意味着添加另一种语言就像添加另一个文件一样简单——比如,sp.yml。

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

问题是,en。yml已经发展到我怀疑所有的钥匙都在被使用的程度。

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?

除了使用每个键进行转换调用的git grepping之外,是否有一种快速的方法来识别应用程序没有显式调用的本地化键?

5 个解决方案

#1


11  

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


6  

I18n-tasks gem

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

我刚刚听说了这个gem,它包含一个任务来显示“潜在未使用的翻译”。

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

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

#3


0  

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

实际上,我将它们设为active=0,但这可能对你不起作用

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.

没有其他的方式。

#4


0  

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:

这是一个实验性的项目,它与翻译查找相挂钩,并在Redis中增加翻译的关键计数器:

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

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)

这个想法是你可以拉出统计数据并进行比较。(就目前而言,我希望能帮助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.

这个开销大约是3毫秒的pr查找。它归结为每个页面/请求执行的查找数量。

#5


-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

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

#1


11  

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


6  

I18n-tasks gem

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

我刚刚听说了这个gem,它包含一个任务来显示“潜在未使用的翻译”。

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

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

#3


0  

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

实际上,我将它们设为active=0,但这可能对你不起作用

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.

没有其他的方式。

#4


0  

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:

这是一个实验性的项目,它与翻译查找相挂钩,并在Redis中增加翻译的关键计数器:

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

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)

这个想法是你可以拉出统计数据并进行比较。(就目前而言,我希望能帮助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.

这个开销大约是3毫秒的pr查找。它归结为每个页面/请求执行的查找数量。

#5


-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

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