Reputation: 14833
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.
Upvotes: 17
Views: 1870
Reputation: 483
There are some "extractors" mentioned in the custom tasks page for i18n-tasks.
They offer to automatically extract hardcoded text from your Views into your Yaml files, or even the database (Lost In Translation).
Most seem to offer an interactive mode so you could use them just to identify hardcoded text, even if you don't want to auto-extract it.
I have tried any of them so can't comment on their effectiveness.
Upvotes: 2
Reputation: 3846
I was inspired by Andi's answer but also wanted an easy way to jump straight to the file and line (and to instead search for words that start with a capital letter)
grep -r -n ".\+[ >^=]\([A-Z][a-z]\+\b\)" .
This command recursively greps all files in a folder and puts the filename and line number in each result, like so:
./interviews/show.html.erb:17: Your interview has been scheduled
./interviews/show.html.erb:49: Click the button below to add this event to your calendar.
Upvotes: 1
Reputation: 1161
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.
Upvotes: -2
Reputation: 1287
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.
Upvotes: 2
Reputation: 467
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.
Upvotes: 1
Reputation: 467
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.
<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...
$ 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.
Upvotes: 1