检查Rails视图- I18n中的硬编码文本

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

some of our devs (me included) don't always take it serious to put text in a localization file, result is a lot of hardcoded texts scattered around a lot of views. I'm wondering if any of you has an idea to automate the search for hardcoded texts in views? Has anyone a tool or an approach how to check for this? I was thinking if a nifty bash script would do the job, but I'm a bit lost where to start. Any help much appreciated.

我们的一些开发人员(包括我)并不总是认真地将文本放到本地化文件中,结果是大量的硬编码文本分散在许多视图中。我想知道你们有没有人想过在视图中自动搜索硬编码文本?有人有工具或方法来检查这个吗?我在想,一个漂亮的bash脚本是否可以完成这项工作,但我有点不知所措。感谢任何帮助。

Edit: Not 100% accurate but works best for me so I accepted Andi's answer.

编辑:不是100%准确,但对我来说是最好的,所以我接受了安迪的回答。

4 个解决方案

#1


1  

I think you can get very far by just using grep:

我认为你可以通过使用grep得到很好的效果:

cat $(find . | grep .html.erb) | grep -v '[=<>{}$/;]' | grep '\w \w'

This finds texts based on the idea that there are some characters which are not typical for texts

这一发现基于这样一种观点,即有些字符并不代表文本。

grep -v '[=<>{}$/;]'

and that there should be at least one space with a preceding word character and one where a word character follows

并且至少应该有一个具有前一个字字符的空格,以及一个单词字符跟随的地方。

grep '\w \w'

This might not be a hundred percent accurate but is a fast and easy way to quickly check for hard coded text.

这可能不是百分之百的准确,但是这是一种快速和简单的方法来快速检查硬编码的文本。

#2


1  

If most lines of code are short and the hard-coded text is long, you can use strings -n [number] to find any text with a particular number of characters.

如果大多数代码行都很短,并且硬编码的文本很长,您可以使用string -n [number]来查找任何具有特定数量字符的文本。

  <html>                                  |
   <head>                                 |
     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                                          |
     <title>Example Page</title>          |
                                          |
   </head>                                |
                                          |
   <body>                                 |
     <h1><%= @page.name %></h1>           |
     <p>                                  |
       This is a piece of hard coded text which must be found.
     </p>                                 |
   </body>                                |
  </html>                                 | 40 characters

If you set the length to 40...

如果你把长度设置为40…

$ cat $(find . | grep .html.erb) | strings -n 40
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
  This is a piece of hard coded text which must be found.

It should be mostly accurate in finding hard-coded text.

在查找硬编码文本时,它应该是最准确的。

#3


1  

You could use a regular expression to find anything neither enclosed within angle brackets (catching most HTML tags and Ruby) nor inside style, script or title tags.

您可以使用正则表达式来查找任何不包含在尖括号内(捕获大部分HTML标记和Ruby)或内部样式、脚本或标题标记的内容。

^(?!.*(<(style|script|title).*?<\/\1>|<.*?>)).*$

^(? !。*(<(脚本风格| |标题)。* ? < \ / \ 1 > | < . * ? >)。*美元

If you discover that any other tags are getting through, just add them to the list of exceptions.

如果您发现任何其他标记正在通过,请将它们添加到异常列表中。

#4


0  

why don't you raise exception in development and test environments when the translations are missing. In development and test environments you can add this:

当转换丢失时,为什么不在开发和测试环境中提出异常呢?在开发和测试环境中,您可以添加以下内容:

Rails.application.configure do |config|
  config.action_view.raise_on_missing_translations = true
end

This should help. For more details read this.

这应该帮助。更多细节请阅读本文。


Also if you just want to find all the missing translations this gem looks promising. I've personally not used this gem, but seems like an ideal way to find missing translations instead of writing a script by myself:

如果你只是想找到所有丢失的翻译,这个宝石看起来很有希望。我个人没有使用过这个宝石,但似乎是一个寻找丢失翻译的理想方法,而不是自己写剧本:

i18n-tasks missing

Gem also has a task to find all unused translations.

Gem也有一个任务来查找所有未使用的翻译。

#1


1  

I think you can get very far by just using grep:

我认为你可以通过使用grep得到很好的效果:

cat $(find . | grep .html.erb) | grep -v '[=<>{}$/;]' | grep '\w \w'

This finds texts based on the idea that there are some characters which are not typical for texts

这一发现基于这样一种观点,即有些字符并不代表文本。

grep -v '[=<>{}$/;]'

and that there should be at least one space with a preceding word character and one where a word character follows

并且至少应该有一个具有前一个字字符的空格,以及一个单词字符跟随的地方。

grep '\w \w'

This might not be a hundred percent accurate but is a fast and easy way to quickly check for hard coded text.

这可能不是百分之百的准确,但是这是一种快速和简单的方法来快速检查硬编码的文本。

#2


1  

If most lines of code are short and the hard-coded text is long, you can use strings -n [number] to find any text with a particular number of characters.

如果大多数代码行都很短,并且硬编码的文本很长,您可以使用string -n [number]来查找任何具有特定数量字符的文本。

  <html>                                  |
   <head>                                 |
     <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                                          |
     <title>Example Page</title>          |
                                          |
   </head>                                |
                                          |
   <body>                                 |
     <h1><%= @page.name %></h1>           |
     <p>                                  |
       This is a piece of hard coded text which must be found.
     </p>                                 |
   </body>                                |
  </html>                                 | 40 characters

If you set the length to 40...

如果你把长度设置为40…

$ cat $(find . | grep .html.erb) | strings -n 40
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
  This is a piece of hard coded text which must be found.

It should be mostly accurate in finding hard-coded text.

在查找硬编码文本时,它应该是最准确的。

#3


1  

You could use a regular expression to find anything neither enclosed within angle brackets (catching most HTML tags and Ruby) nor inside style, script or title tags.

您可以使用正则表达式来查找任何不包含在尖括号内(捕获大部分HTML标记和Ruby)或内部样式、脚本或标题标记的内容。

^(?!.*(<(style|script|title).*?<\/\1>|<.*?>)).*$

^(? !。*(<(脚本风格| |标题)。* ? < \ / \ 1 > | < . * ? >)。*美元

If you discover that any other tags are getting through, just add them to the list of exceptions.

如果您发现任何其他标记正在通过,请将它们添加到异常列表中。

#4


0  

why don't you raise exception in development and test environments when the translations are missing. In development and test environments you can add this:

当转换丢失时,为什么不在开发和测试环境中提出异常呢?在开发和测试环境中,您可以添加以下内容:

Rails.application.configure do |config|
  config.action_view.raise_on_missing_translations = true
end

This should help. For more details read this.

这应该帮助。更多细节请阅读本文。


Also if you just want to find all the missing translations this gem looks promising. I've personally not used this gem, but seems like an ideal way to find missing translations instead of writing a script by myself:

如果你只是想找到所有丢失的翻译,这个宝石看起来很有希望。我个人没有使用过这个宝石,但似乎是一个寻找丢失翻译的理想方法,而不是自己写剧本:

i18n-tasks missing

Gem also has a task to find all unused translations.

Gem也有一个任务来查找所有未使用的翻译。