Slide 1

Slide 1 text

Localize your Rails apps like a pro

Slide 2

Slide 2 text

David @dabit

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

class Habitacion ; end class HabitacionesController ; end class Reservacion ; end class ReservacionesController ; end

Slide 8

Slide 8 text

class Room ; end class RoomsController ; end class Reservation ; end class ReservationsController ; end

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

class Room ; end class RoomsController ; end class Reservation ; end class ReservationsController ; end

Slide 12

Slide 12 text

Internationalization

Slide 13

Slide 13 text

i18n

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

i18n

Slide 16

Slide 16 text

The basics

Slide 17

Slide 17 text

I18n.t

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

es: pages: title: welcome: "Bienvenido"

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Welcome to RailsConf

I hope you enjoy my talk

Slide 24

Slide 24 text

<%= t('pages.welcome.title') %>

<%= t('pages.welcome.body') %>

Slide 25

Slide 25 text

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"

Slide 26

Slide 26 text

No content

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

No content

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

Split your .yml files

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

No content

Slide 37

Slide 37 text

# config/application.rb config.i18n.load_path += Dir[Rails.root.join('config', 'locales', '**', '*.{rb,yml}')]

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

I18n-debug gem

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Gemfile gem 'i18n-debug', group: :development

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

Default lookup tree

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

config/locale/attributes/es.yml es: attributes: name: Nombre

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

config/locales/activerecord/es.yml es: activerecord: attributes: author: name: Nombre de autor

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

app/views/authors/_form.html.erb
<%= f.label :name %> <%= f.text_field :name %>

Slide 60

Slide 60 text

Lazy lookup

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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') }

Slide 65

Slide 65 text

| irect_to @author, notice: I18n.t('.flash') } der :new }

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

app/views/authors/new.html.erb

New Author

<%= render 'form', author: @author %> <%= link_to 'Back', authors_path %>

Slide 70

Slide 70 text

<%= t('.page_title') %>

<%= render 'form', author: @author %> <%= link_to 'Back', authors_path %>

Slide 71

Slide 71 text

<%= t('.page_title') %>

<%= render 'form', author: @author %> <%= link_to 'Back', authors_path %>

Slide 72

Slide 72 text

No content

Slide 73

Slide 73 text

Localized Views

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

app/views/terms/show.html.erb

<%= t('.page_title') %>

<%= t('.title1') %>

<%= t('.paragraph1') %>

<%= t('.title2') %>

<%= t('.paragraph2') %>

<%= t('.title3') %>

<%= t('.paragraph3') %>

<%= t('.title4') %>

<%= t('.paragraph4') %>

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

Localized Views

Slide 79

Slide 79 text

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

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

It works with partials

Slide 88

Slide 88 text

It works with mailer views!

Slide 89

Slide 89 text

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

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

Fallbacks

Slide 92

Slide 92 text

American English UK English Soccer Football Football American Football Restroom Toilet Bill Note Shower Bath

Slide 93

Slide 93 text

config/application.rb module Railsconf class Application < Rails::Application config.i18n.fallbacks = { en_GB: [ :en ] } end end

Slide 94

Slide 94 text

No content

Slide 95

Slide 95 text

No content

Slide 96

Slide 96 text

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

Slide 97

Slide 97 text

No content

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

No content

Slide 100

Slide 100 text

Pluralization

Slide 101

Slide 101 text

0 No points 1 One point 20 20 points

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

No content

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

No content

Slide 108

Slide 108 text

No content

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

No content

Slide 111

Slide 111 text

No content

Slide 112

Slide 112 text

No content

Slide 113

Slide 113 text

No content

Slide 114

Slide 114 text

No content

Slide 115

Slide 115 text

Different backends

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

I18n::Backend::ActiveRecord

Slide 118

Slide 118 text

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

Slide 119

Slide 119 text

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

Slide 120

Slide 120 text

initializer require 'i18n/backend/active_record' I18n.backend = I18n::Backend::ActiveRecord.new

Slide 121

Slide 121 text

No content

Slide 122

Slide 122 text

No content

Slide 123

Slide 123 text

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'

Slide 124

Slide 124 text

No content

Slide 125

Slide 125 text

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

Slide 126

Slide 126 text

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

Slide 127

Slide 127 text

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

Slide 128

Slide 128 text

No content

Slide 129

Slide 129 text

The chain backend

Slide 130

Slide 130 text

I18n::Backend::Chain

Slide 131

Slide 131 text

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

Slide 132

Slide 132 text

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

Slide 133

Slide 133 text

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

Slide 134

Slide 134 text

No content

Slide 135

Slide 135 text

No content

Slide 136

Slide 136 text

No content

Slide 137

Slide 137 text

Times, dates and numbers

Slide 138

Slide 138 text

the L function

Slide 139

Slide 139 text

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"

Slide 140

Slide 140 text

No content

Slide 141

Slide 141 text

No content

Slide 142

Slide 142 text

distance_of_time_in_words_to_now

Slide 143

Slide 143 text

No content

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

No content

Slide 148

Slide 148 text

number_with_precision

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

No content

Slide 151

Slide 151 text

number_to_currency

Slide 152

Slide 152 text

No content

Slide 153

Slide 153 text

No content

Slide 154

Slide 154 text

number_to_human

Slide 155

Slide 155 text

No content

Slide 156

Slide 156 text

No content

Slide 157

Slide 157 text

No content

Slide 158

Slide 158 text

number_to_human_size

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

No content

Slide 161

Slide 161 text

That is it!

Slide 162

Slide 162 text

TL;DR

Slide 163

Slide 163 text

•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

Slide 164

Slide 164 text

Gracias Thank you Obrigado ありがとう

Slide 165

Slide 165 text