Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Translate Rails apps like a pro

Translate Rails apps like a pro

If you have ever worked in a Rails applications that needs to be available in more than one language you probably know how hard to maintain it can become over time, specially if more than one developer is involved in the process.

If you have never worked with localizations you probably will at some point in the future.

I want to share with you my experience. As a Spanish speaking developer I have worked in many multi-language apps, I have advice and a list of good practices that can help you in future localized projects.

David Padilla

May 01, 2019
Tweet

More Decks by David Padilla

Other Decks in Technology

Transcript

  1. class Room ; end class RoomsController ; end class Reservation

    ; end class ReservationsController ; end
  2. class Room ; end class RoomsController ; end class Reservation

    ; end class ReservationsController ; end
  3. i n t e r n a t i o

    n a l i z a t i o n 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
  4. en.yml es.yml en: pages: welcome: title: "Welcome to RailsConf" body:

    "I hope you enjoy my talk" es: pages: welcome: title: "Bienvenidos a RailsConf" body: "Espero que disfrutes mi charla"
  5. app/controllers/authors_controller.rb # POST /authors def create @author = Author.new(author_params) respond_to

    do |format| if @author.save format.html { redirect_to @author, notice: 'Author was successfully created.' } else format.html { render :new } end end end
  6. POST /authors ef create @author = Author.new(author_params) respond_to do |format|

    if @author.save format.html { redirect_to @author, notice: 'Author was successfully created.' } else format.html { render :new } end end
  7. POST /authors ef create @author = Author.new(author_params) respond_to do |format|

    if @author.save format.html { redirect_to @author, notice: I18n.t('.flash') }
  8. app/views/terms/show.html.erb <h1><%= t('.page_title') %></h1> <h2><%= t('.title1') %></h2> <p><%= t('.paragraph1') %></p>

    <h2><%= t('.title2') %></h2> <p><%= t('.paragraph2') %></p> <h2><%= t('.title3') %></h2> <p><%= t('.paragraph3') %></p> <h2><%= t('.title4') %></h2> <p><%= t('.paragraph4') %></p>
  9. en.yml en_GB.yml en: fallbacks: show: soccer: Soccer football: Football restroom:

    Restroom bill: Bill shower: Shower words: WORDS in: IN common: COMMON en_GB: fallbacks: show: soccer: Football football: American Football restroom: Toilet bill: Note shower: Bath
  10. migration class CreateTranslations < ActiveRecord::Migration def self.up create_table :translations do

    |t| t.string :locale t.string :key t.text :value t.text :interpolations t.boolean :is_proc, :default => false t.timestamps end end end
  11. create Translation = I18n::Backend::ActiveRecord::Translation Translation.create locale: :es, key: 'authors.new.page_title', value:

    'Autor Nuevo' Translation.create locale: :es, key: 'helpers.label.author.name', value: 'Nombre de autor' Translation.create locale: :es, key: 'attributes.bio', value: 'Biografía' Translation.create locale: :es, key: 'attributes.age', value: 'Edad' Translation.create locale: :es, key: 'attributes.category', value: 'Categoría' Translation.create locale: :es, key: 'helpers.submit.create', value: 'Crear %{model}' Translation.create locale: :es, key: 'classic', value: 'Clásico'
  12. en.yml en: time: formats: default: "%H:%M" formal: "The time is

    now %H:%M good sir" informal: "The time is %H, bye" thing: "Clobberin' time" rock: "It doesn't matter what time it is"
  13. •Be nice, Split your YAML files •Use the default lookup

    tree and lazy lookup •You can localize views and partials •Use fallbacks for small language variations •Use pluralization •You can have as many backends as you want •Use the included methods to localize times, dates and numbers