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. 3.
  2. 4.
  3. 5.
  4. 6.
  5. 8.

    class Room ; end class RoomsController ; end class Reservation

    ; end class ReservationsController ; end
  6. 9.
  7. 10.
  8. 11.

    class Room ; end class RoomsController ; end class Reservation

    ; end class ReservationsController ; end
  9. 13.
  10. 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
  11. 15.
  12. 17.
  13. 22.
  14. 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"
  15. 26.
  16. 27.
  17. 28.
  18. 29.
  19. 31.
  20. 32.
  21. 33.
  22. 34.
  23. 35.
  24. 36.
  25. 38.
  26. 40.
  27. 42.
  28. 43.
  29. 44.
  30. 46.
  31. 47.
  32. 48.
  33. 49.
  34. 50.
  35. 52.
  36. 53.
  37. 54.
  38. 56.
  39. 57.
  40. 58.
  41. 61.
  42. 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
  43. 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
  44. 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') }
  45. 66.
  46. 67.
  47. 68.
  48. 72.
  49. 74.
  50. 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>
  51. 76.
  52. 77.
  53. 80.
  54. 81.
  55. 82.
  56. 83.
  57. 84.
  58. 85.
  59. 86.
  60. 91.
  61. 94.
  62. 95.
  63. 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
  64. 97.
  65. 98.
  66. 99.
  67. 102.
  68. 103.
  69. 104.
  70. 105.
  71. 106.
  72. 107.
  73. 108.
  74. 109.
  75. 110.
  76. 111.
  77. 112.
  78. 113.
  79. 114.
  80. 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
  81. 121.
  82. 122.
  83. 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'
  84. 124.
  85. 128.
  86. 134.
  87. 135.
  88. 136.
  89. 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"
  90. 140.
  91. 141.
  92. 143.
  93. 144.
  94. 145.
  95. 146.
  96. 147.
  97. 149.
  98. 150.
  99. 152.
  100. 153.
  101. 155.
  102. 156.
  103. 157.
  104. 159.
  105. 160.
  106. 162.
  107. 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