Grep Rails i18n translation elements from your app/views folder

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

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 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.

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

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

1-300x250-nyc-rooftop-hotel

Diese Artikel könnten Sie auch interessieren:

Kommentare

Kommentar hinzufügen

 erforderlich

 erforderlich, wird nicht publiziert


Tags

rails i18n

Cookies helfen uns bei der Bereitstellung unserer Dienste. Durch die Nutzung unserer Dienste erklären Sie sich damit einverstanden, dass wir Cookies setzen. Datenschutzerklärung & Cookies