Grep Rails i18n translation elements from your app/views folder

 

Stabbing through all the views of your existing and mature rails app can seem quite daunting. But with the right approach it can be be quite productive. Want to throw in twitter-bootstrap? Need the an i18n treatment (translation & localization) of all the text elements and labels. Use this handy grep command to find the lines you need to tackle.

To find all Rails internationalization strings in the app/views folder. Why are we doing this:

  • Quality assurance: Ensure that every page is properly translated.
  • Prepare for twitter-bootstrap CSS so we can throw out legacy form elements designed with < p > tags
  • Be able to add another language and have a fairly stable language file. Then I can hand off a fairly stable language file and get the translations without bothering about the organic growth of yet another few other words in the basic de.yml or en.yml file.

My current and ideal workflow that I use with Rails 3.1 apps - now that I have grown to like twitter-bootstrap - goes like this:

  rails g bootstrap:install 
  # create the model and the views with the regular scaffold controllers  
  rails g scaffold posts title body --skip-stylesheets 
  # create the views with bootstrap css  
  rails g bootstrap:themed posts -f   
  
  # for i18n the views go through the created views 
  # (mostly index, _form) and replace the the t(:fieldname)

Thats all nice and cosy for new Rails 3.1 apps, but what about those "legacy" Rails 2.x apps that, no doubt, you are maintaining as well. Now thats needs to be tackled a little differently. We need to find these strings so we can replace them. And a sense of having captured all the fields helps in getting it done.

use grep to help to find the i18n lines in app/views erb files

So Grep to the rescue. I use the UNIX Grep command to find the lines that need attention in the view files.

grep -r -n "h1\|<label\|submit\|<th\|legend\|btn" app/views/. |grep -v "t(:\|thead\|path\|admin\|each_with"

It goes like this: We tell it to look for H1 label submit elements (where we expect to find strings in need of translation) and then we pipe it to filter out elements that we want to ignore. Use the -r to recurse down the directory and the -n switch to get the line numbers: So we got can go straight to the line where the translation needs fixing.
The ignore list tends to grow as you wade through the views and you identify stuff you don't want to see reported out. Just add your terms to the end of the list and separate with backslash and pipe.

What we get its is a list of lines to look into where we need to tweak the i18n strings.

The help with the tweaking and to ensure that the view don't break I set up Guard. So I get to see immediately that we are breaking. I really like the auto reloader. You go to the page you want to check, and switch the on the auto reload Guard each time you save a view file it automatically reloads the page in the browser. So you get to stay in your editor and just glance over into the browser to see if the view is still behaving.

Here is my Guardfile

guard 'livereload' do   
   watch(%r{app/.+\.(erb|haml|prawn)})  
   watch(%r{app/helpers/.+\.rb})  
   watch(%r{app/models/.+\.rb})  
   watch(%r{app/controllers/.+\.rb})  
   watch(%r{public/.+\.(css|js|html)})  
   watch(%r{config/locales/.+\.yml})  
end
Author: , igumbi.com. Ich bin auf twitter zu finden: @smtm, und als roland.oth auf Facebook.
igumbi online Hotelsoftware - jetzt unverbindlich probieren
Hotelsoftware Demo: System Übersicht der Funktionen

igumbi ist eine online Hotelsoftware mit einem einfachen und schnellen Buchungstool für Ihre Website. Mit dynamischen Preisen auf Ihrer Website und in den Portalen erhöhen Sie Ihren Umsatz und Gewinn.

Wir steigern Ihre provisionsfreien Direktbuchungen. Sie sparen Zeit und haben mit der iPhone App unterwegs Zugriff auf Ihre Daten.

Testen Sie die igumbi Hotelsoftware für 30 Tage. Eine Kreditkarte ist nicht erforderlich.

Diese Artikel könnten Sie auch interessieren:
  • YAML i18n language files for JSON Rails internationalization (i18n) tip for getting your language/locale translations to javascript: parse the i18n YAML files to produce a JSON. For DOM translations
  • Facebook locale Settings für Österreich Um Seiten im Facebook "likeable "zu machen gibt es Open Graph Tags für den HEAD Bereich einer Webpage. Sprachversion für Österreich mit de_DE.
  • Offline App with rails 3.1 and rack-offline Use rack-offline for your mobile HTML5 offline App for a dynamic application.manifest with a revision hash that updates when your source code changes.
  • Liste der wichtigsten Länder Die Reihenfolge in der Länderliste kann nun angepasst werden . Die Länder werden bei Adressen in der Datenbank mit dem zwei Buchstaben ISO Codes gespeichert. Grundlage für Übersetzung der Ländernamen bei Vorlagen und Mails.

Kommentare

Kommentar hinzufügen
erforderlich

erforderlich, wird nicht publiziert