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.

0f9c9bbecc4067b9bce445cb11ed5d53?s=128

David Padilla

May 01, 2019
Tweet

Transcript

  1. Localize your Rails apps like a pro

  2. David @dabit

  3. None
  4. None
  5. None
  6. None
  7. class Habitacion ; end class HabitacionesController ; end class Reservacion

    ; end class ReservacionesController ; end
  8. class Room ; end class RoomsController ; end class Reservation

    ; end class ReservationsController ; end
  9. None
  10. None
  11. class Room ; end class RoomsController ; end class Reservation

    ; end class ReservationsController ; end
  12. Internationalization

  13. i18n

  14. 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
  15. i18n

  16. The basics

  17. I18n.t

  18. I18n.t('welcome', scope: 'pages.title') I18n.t('pages.title.welcome')

  19. es: pages: title: welcome: "Bienvenido"

  20. I18n.t('welcome', scope: 'pages.title') # => "Bienvenido"

  21. I18n.t('pages.title.welcome') # => "Bienvenido"

  22. None
  23. <h1>Welcome to RailsConf</h1> <p>I hope you enjoy my talk</p>

  24. <h1><%= t('pages.welcome.title') %></h1> <p><%= t('pages.welcome.body') %></p>

  25. 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"
  26. None
  27. None
  28. None
  29. None
  30. Split your .yml files

  31. None
  32. None
  33. None
  34. None
  35. None
  36. None
  37. # config/application.rb config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]

  38. None
  39. I18n-debug gem

  40. None
  41. Gemfile gem 'i18n-debug', group: :development

  42. None
  43. None
  44. None
  45. Default lookup tree

  46. None
  47. None
  48. None
  49. None
  50. None
  51. config/locale/attributes/es.yml es: attributes: name: Nombre

  52. None
  53. None
  54. None
  55. config/locales/activerecord/es.yml es: activerecord: attributes: author: name: Nombre de autor

  56. None
  57. None
  58. None
  59. app/views/authors/_form.html.erb <div class="field"> <%= f.label :name %> <%= f.text_field :name

    %> </div>
  60. Lazy lookup

  61. None
  62. 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
  63. 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
  64. 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') }
  65. | irect_to @author, notice: I18n.t('.flash') } der :new }

  66. None
  67. None
  68. None
  69. app/views/authors/new.html.erb <h1>New Author</h1> <%= render 'form', author: @author %> <%=

    link_to 'Back', authors_path %>
  70. <h1><%= t('.page_title') %></h1> <%= render 'form', author: @author %> <%=

    link_to 'Back', authors_path %>
  71. <h1><%= t('.page_title') %></h1> <%= render 'form', author: @author %> <%=

    link_to 'Back', authors_path %>
  72. None
  73. Localized Views

  74. None
  75. 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>
  76. None
  77. None
  78. Localized Views

  79. app/views/terms/show.html.erb app/views/terms/show.es.html.erb app/views/terms/show.jp.html.erb

  80. None
  81. None
  82. None
  83. None
  84. None
  85. None
  86. None
  87. It works with partials

  88. It works with mailer views!

  89. app/views/newsletter_mailer/monday.en.html.erb app/views/newsletter_mailer/monday.es.html.erb app/views/newsletter_mailer/monday.jp.html.erb app/views/newsletter_mailer/monday.cn.html.erb

  90. app/views/newsletter_mailer/monday.en.html.erb app/views/newsletter_mailer/monday.es.html.erb app/views/newsletter_mailer/monday.jp.html.erb app/views/newsletter_mailer/monday.cn.html.erb

  91. Fallbacks

  92. American English UK English Soccer Football Football American Football Restroom

    Toilet Bill Note Shower Bath
  93. config/application.rb module Railsconf class Application < Rails::Application config.i18n.fallbacks = {

    en_GB: [ :en ] } end end
  94. None
  95. None
  96. 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
  97. None
  98. None
  99. None
  100. Pluralization

  101. 0 No points 1 One point 20 20 points

  102. None
  103. None
  104. None
  105. None
  106. None
  107. None
  108. None
  109. None
  110. None
  111. None
  112. None
  113. None
  114. None
  115. Different backends

  116. initializer # config/initializers/translations.rb I18n.backend = x

  117. I18n::Backend::ActiveRecord

  118. Gemfile gem 'i18n-active_record', require: 'i18n/active_record'

  119. 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
  120. initializer require 'i18n/backend/active_record' I18n.backend = I18n::Backend::ActiveRecord.new

  121. None
  122. None
  123. 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'
  124. None
  125. Backends I18n::Backend::ActiveRecord I18n::Backend::Redis I18n::Backend::Mongodb I18n::Backend::Gettext I18n::Backend::Simple

  126. config/initializers/translations.rb # config/initializers/translations.rb require 'i18n/backend/hodor' I18n.backend = I18n::Backend::Hodor.new

  127. lib/i18n/backend/hodor.rb require 'i18n/backend/base' class I18n::Backend::Hodor include I18n::Backend::Base def lookup(locale, key,

    scope = [], options = {}) "Hodor" end end
  128. None
  129. The chain backend

  130. I18n::Backend::Chain

  131. Backends I18n::Backend::ActiveRecord I18n::Backend::Redis I18n::Backend::Mongodb I18n::Backend::Gettext I18n::Backend::Simple

  132. config/initailizers/translations.rb require 'i18n/backend/active_record' I18n.backend = I18n::Backend::Chain.new( I18n::Backend::ActiveRecord.new, I18n.backend )

  133. require 'i18n/backend/active_record' I18n.backend = I18n::Backend::Chain.new( I18n::Backend::ActiveRecord.new, I18n.backend )

  134. None
  135. None
  136. None
  137. Times, dates and numbers

  138. the L function

  139. 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"
  140. None
  141. None
  142. distance_of_time_in_words_to_now

  143. None
  144. None
  145. None
  146. None
  147. None
  148. number_with_precision

  149. None
  150. None
  151. number_to_currency

  152. None
  153. None
  154. number_to_human

  155. None
  156. None
  157. None
  158. number_to_human_size

  159. None
  160. None
  161. That is it!

  162. TL;DR

  163. •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
  164. Gracias Thank you Obrigado ありがとう

  165. @dabit david@michelada.io