Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check for hardcoded text in Rails views - I18n

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.

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

like image 381
supersize Avatar asked Feb 17 '17 09:02

supersize


2 Answers

I think you can get very far by just using 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.

like image 126
Andi Avatar answered Nov 12 '22 12:11

Andi


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.

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

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

like image 38
Sekalf Nroc Avatar answered Nov 12 '22 12:11

Sekalf Nroc