locos x rails

Post on 19-Jan-2015

4.752 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

DESCRIPTION

My talk at Locos x Rails, in Argentina, about Rails I18n (internationalization) support, tips and tricks.

TRANSCRIPT

I18nRails Globalization

Tuesday, April 7, 2009

Fabio Akita

AkitaOnRails.com

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tutorial-like

Tuesday, April 7, 2009

Tuesday, April 7, 2009

I18n Before 2.2

Tuesday, April 7, 2009

Chaos

Tuesday, April 7, 2009

Monkey Patch Approach

Tuesday, April 7, 2009

Many incompatible

projects

Tuesday, April 7, 2009

Reinventing the Wheel all the time

Tuesday, April 7, 2009

September 2007

Tuesday, April 7, 2009

Rails I18n Project

Tuesday, April 7, 2009

Lead fromSven Fuchs

adva-cms.org

Tuesday, April 7, 2009

Every plugin creator gathered

Tuesday, April 7, 2009

Common Goals:

Tuesday, April 7, 2009

100% swappable

Tuesday, April 7, 2009

The Least Denominator

Tuesday, April 7, 2009

Not a full blown L10n solution

Tuesday, April 7, 2009

Common, Powerful API

Tuesday, April 7, 2009

Simple Backend(en_US only)

Tuesday, April 7, 2009

Getting Started ...

Tuesday, April 7, 2009

rails locos

Tuesday, April 7, 2009

rails locos

./script/generate nifty_layout

Tuesday, April 7, 2009

rails locos

./script/generate nifty_layout

Tuesday, April 7, 2009

rails locos

./script/generate nifty_layout

./script/generate nifty_scaffold Contact name:string email:string phone:string mobile:string

Tuesday, April 7, 2009

rails locos

./script/generate nifty_layout

./script/generate nifty_scaffold Contact name:string email:string phone:string mobile:string

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Getting i18n files

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

config/locales/pt-BR_rails.yml

Tuesday, April 7, 2009

config/locales/pt-BR.rb

Tuesday, April 7, 2009

>> I18n.locale = :en

Tuesday, April 7, 2009

>> I18n.locale = :en

>> I18n.translate "hello"=> "Hello world"

Tuesday, April 7, 2009

>> I18n.locale = :en

>> I18n.translate "hello"=> "Hello world"

>> I18n.localize Time.now=> "Wed, 01 Apr 2009 22:52:08 -0300"

Tuesday, April 7, 2009

>> I18n.locale = :en

>> I18n.translate "hello"=> "Hello world"

>> I18n.localize Time.now=> "Wed, 01 Apr 2009 22:52:08 -0300"

Tuesday, April 7, 2009

>> I18n.locale = :en

>> I18n.translate "hello"=> "Hello world"

>> I18n.localize Time.now=> "Wed, 01 Apr 2009 22:52:08 -0300"

Tuesday, April 7, 2009

>> I18n.locale = :en

>> I18n.t "hello"=> "Hello world"

>> I18n.l Time.now=> "Wed, 01 Apr 2009 22:52:08 -0300"

Tuesday, April 7, 2009

>> I18n.locale = :"pt-BR"

Tuesday, April 7, 2009

>> I18n.locale = :"pt-BR"

>> I18n.t "hello"=> "Alo Mundo"

Tuesday, April 7, 2009

>> I18n.locale = :"pt-BR"

>> I18n.t "hello"=> "Alo Mundo"

>> I18n.l Time.now=> "Quarta, 01 de Abril de 2009, 22:49 hs"

Tuesday, April 7, 2009

>> I18n.locale = :"pt-BR"

>> I18n.t "hello"=> "Alo Mundo"

>> I18n.l Time.now=> "Quarta, 01 de Abril de 2009, 22:49 hs"

>> I18n.l Time.now, :format => :short=> "01/04, 22:52 hs"

Tuesday, April 7, 2009

>> I18n.locale = :"pt-BR"

>> I18n.t "hello"=> "Alo Mundo"

>> I18n.l Time.now=> "Quarta, 01 de Abril de 2009, 22:49 hs"

>> I18n.l Time.now, :format => :short=> "01/04, 22:52 hs"

Tuesday, April 7, 2009

>> time_ago_in_words(1.month.ago)=> "about 1 month">> time_ago_in_words(1.5.hours.ago)=> "about 2 hours"

Tuesday, April 7, 2009

>> time_ago_in_words(1.month.ago)=> "about 1 month">> time_ago_in_words(1.5.hours.ago)=> "about 2 hours"

>> I18n.locale = :"pt-BR"

>> time_ago_in_words(1.month.ago)=> "aproximadamente 1 mês">> time_ago_in_words(1.5.hours.ago)=> "aproximadamente 2 horas"

Tuesday, April 7, 2009

>> I18n.t [:hello, :world]=> ["alo", "mundo"]

Tuesday, April 7, 2009

>> I18n.t [:hello, :world]=> ["alo", "mundo"]

>> I18n.t :brazil, :defaults => [:argentina, "Chile"]=> "Argentina"

Tuesday, April 7, 2009

message: "Hello, {{name}}!"

Tuesday, April 7, 2009

message: "Hello, {{name}}!"

I18n.t :message, :name => "John" # => "Hello John!"

Tuesday, April 7, 2009

message: "Hello, {{name}}!"

I18n.t :message, :name => "John" # => "Hello John!"

# en-US :days => { :one => "one day" :other => "{{count}} days" }

Tuesday, April 7, 2009

message: "Hello, {{name}}!"

I18n.t :message, :name => "John" # => "Hello John!"

# en-US :days => { :one => "one day" :other => "{{count}} days" }

I18n.t :days, :count => 1 # => "one day" I18n.t :days, :count => 2 # => "2 days"

Tuesday, April 7, 2009

Extracting Strings

Tuesday, April 7, 2009

<tr> <th>Name</th> <th>Email</th> <th>Phone</th> <th>Mobile</th></tr>

app/views/contacts/views/index.html.erb

Tuesday, April 7, 2009

<tr> <th>Name</th> <th>Email</th> <th>Phone</th> <th>Mobile</th></tr>

app/views/contacts/views/index.html.erb

config/locales/pt-BR.rb

Tuesday, April 7, 2009

<tr> <th>Name</th> <th>Email</th> <th>Phone</th> <th>Mobile</th></tr>

app/views/contacts/views/index.html.erb

config/locales/pt-BR.rb{ :"pt-BR" => { :hello => "Alô Mundo", :contacts => { :index => { :name => "Nome", :email => "Email", :phone => "Telefone", :mobile => "Celular" } } }}

Tuesday, April 7, 2009

<tr> <th><%= t("contacts.index.name") %></th> <th><%= t("index.email", :scope => "contacts") %></th> <th><%= t("phone", :scope => "contacts.index") %></th> <th><%= t(".mobile") %></th></tr>

app/views/contacts/views/index.html.erb

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

en: hello: "Hello world" contacts: index: name: Name email: Email phone: Phone mobile: Mobile

config/locales/en.yml

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Setting Locale

Tuesday, April 7, 2009

class ApplicationController < ActionController::Base helper :all protect_from_forgery before_filter :set_locale private def set_locale I18n.locale = params[:locale] if params[:locale] endend

app/controllers/application_controller.rb

Tuesday, April 7, 2009

class ApplicationController < ActionController::Base helper :all protect_from_forgery before_filter :set_locale private def set_locale I18n.locale = params[:locale] if params[:locale] endend

app/controllers/application_controller.rb

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

More strings

Tuesday, April 7, 2009

<td><%= link_to t("common.show"), contact %></td><td><%= link_to t("common.edit"), edit_contact_path(contact) %></td><td><%= link_to t("common.destroy"), contact, :confirm => t('common.destroy_confirm'), :method => :delete %></td>

app/views/contacts/views/index.html.erb

Tuesday, April 7, 2009

{ :"pt-BR" => { :hello => "Alô Mundo", :common => { :show => "Mostrar", :edit => "Editar", :destroy => "Apagar", :destroy_confirm => "Tem certeza?" }, ... }}

config/locales/pt-BR.rb

Tuesday, April 7, 2009

en: hello: "Hello world"

common: show: Show edit: Edit destroy: Destroy destroy_confirm: Are you sure? ...

config/locales/en.yml

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

ActiveRecord

Tuesday, April 7, 2009

<% form_for @contact do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <p> <%= f.label :email %><br /> <%= f.text_field :email %> </p> <p> <%= f.label :phone %><br /> <%= f.text_field :phone %> </p> <p> <%= f.label :mobile %><br /> <%= f.text_field :mobile %> </p> <p><%= f.submit t("common.submit") %></p><% end %>

Tuesday, April 7, 2009

<% form_for @contact do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <p> <%= f.label :email %><br /> <%= f.text_field :email %> </p> <p> <%= f.label :phone %><br /> <%= f.text_field :phone %> </p> <p> <%= f.label :mobile %><br /> <%= f.text_field :mobile %> </p> <p><%= f.submit t("common.submit") %></p><% end %>

Tuesday, April 7, 2009

...:activerecord => { :models => { :contact => "Contato" }, :attributes => { :contact => { :name => "Nome", :email => "Email", :phone => "Telefone", :mobile => "Celular" } }}...

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

./script/plugin install git://github.com/iain/i18n_label.git

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

<p> <strong>Name:</strong> <%=h @contact.name %></p><p> <strong>Email:</strong> <%=h @contact.email %></p><p> <strong>Phone:</strong> <%=h @contact.phone %></p><p> <strong>Mobile:</strong> <%=h @contact.mobile %></p>

app/views/contacts/views/show.html.erb

Tuesday, April 7, 2009

<p> <strong>Name:</strong> <%=h @contact.name %></p><p> <strong>Email:</strong> <%=h @contact.email %></p><p> <strong>Phone:</strong> <%=h @contact.phone %></p><p> <strong>Mobile:</strong> <%=h @contact.mobile %></p>

app/views/contacts/views/show.html.erb

Tuesday, April 7, 2009

<p> <strong><%= Contact.human_attribute_name('name') %>:</strong> <%=h @contact.name %></p><p> <strong><%= Contact.human_attribute_name('email') %>:</strong> <%=h @contact.email %></p><p> <strong><%= Contact.human_attribute_name('phone') %>:</strong> <%=h @contact.phone %></p><p> <strong><%= Contact.human_attribute_name('mobile') %>:</strong> <%=h @contact.mobile %></p>

app/views/contacts/views/show.html.erb

Tuesday, April 7, 2009

<p> <strong><%= Contact.human_attribute_name('name') %>:</strong> <%=h @contact.name %></p><p> <strong><%= Contact.human_attribute_name('email') %>:</strong> <%=h @contact.email %></p><p> <strong><%= Contact.human_attribute_name('phone') %>:</strong> <%=h @contact.phone %></p><p> <strong><%= Contact.human_attribute_name('mobile') %>:</strong> <%=h @contact.mobile %></p>

app/views/contacts/views/show.html.erb

Tuesday, April 7, 2009

Tuesday, April 7, 2009

flash[:notice] = "Successfully created contact."

flash[:notice] = t("contacts.messages.successful_create")

messages: successful_create: Successfully created contact. successful_update: Successfully updated contact. successful_destroy: Successfully destroyed contact.

:messages => { :successful_create => "Contato criado com sucesso.", :successful_update => "Contato atualizado com sucesso.", :successful_destroy => "Contato apagado com sucesso.",},

app/controllers/contacts_controller.rb

Tuesday, April 7, 2009

class Contact < ActiveRecord::Base validates_presence_of :nameend

app/models/contact.rb

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Routing

Tuesday, April 7, 2009

http://localhost:3000/pt-BR/contacts/new

{ :locale => :"pt-BR", :controller => "contacts", :action => "new"}

Tuesday, April 7, 2009

./script/plugin install git://github.com/svenfuchs/routing-filter.git

http://localhost:3000/pt-BR/contacts/new

{ :locale => :"pt-BR", :controller => "contacts", :action => "new"}

Tuesday, April 7, 2009

./script/plugin install git://github.com/svenfuchs/routing-filter.git

ActionController::Routing::Routes.draw do |map| map.filter 'locale' map.resources :contactsend

http://localhost:3000/pt-BR/contacts/new

{ :locale => :"pt-BR", :controller => "contacts", :action => "new"}

Tuesday, April 7, 2009

./script/plugin install git://github.com/svenfuchs/routing-filter.git

ActionController::Routing::Routes.draw do |map| map.filter 'locale' map.resources :contactsend

http://localhost:3000/pt-BR/contacts/new

{ :locale => :"pt-BR", :controller => "contacts", :action => "new"}

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Views

Tuesday, April 7, 2009

./script/generate controller page

Tuesday, April 7, 2009

./script/generate controller page

class PageController < ApplicationController def show render :template => params[:id] endend

Tuesday, April 7, 2009

./script/generate controller page

class PageController < ApplicationController def show render :template => params[:id] endend

app/views/page/_header.html.erbapp/views/page/about.html.erb

Tuesday, April 7, 2009

Tuesday, April 7, 2009

app/views/page/_header.pt-BR.html.erbapp/views/page/about.pt-BR.html.erb

Tuesday, April 7, 2009

app/views/page/_header.pt-BR.html.erbapp/views/page/about.pt-BR.html.erb

Tuesday, April 7, 2009

<%= render :partial => "header" %>

<p>Faster than a bullet<br/>Terrifying scream<br/>Enraged and full of anger<br/>He’s half man and half machine</p>

app/views/page/about.html.erb

Tuesday, April 7, 2009

<%= render :partial => "header" %>

<p>Faster than a bullet<br/>Terrifying scream<br/>Enraged and full of anger<br/>He’s half man and half machine</p>

<%= render :partial => "header" %>

<p>Mais rápido que uma bala<br/>Um grito apavorante<br/>Enfurecido e cheio de raiva<br/>Ele é metade homem, metade máquina</p>

app/views/page/about.html.erb

app/views/page/about.pt-BR.html.erb

Tuesday, April 7, 2009

<%= render :partial => "header" %>

<p>Faster than a bullet<br/>Terrifying scream<br/>Enraged and full of anger<br/>He’s half man and half machine</p>

<%= render :partial => "header" %>

<p>Mais rápido que uma bala<br/>Um grito apavorante<br/>Enfurecido e cheio de raiva<br/>Ele é metade homem, metade máquina</p>

app/views/page/about.html.erb

app/views/page/about.pt-BR.html.erb

Tuesday, April 7, 2009

<%= render :partial => "header" %>

<p>Faster than a bullet<br/>Terrifying scream<br/>Enraged and full of anger<br/>He’s half man and half machine</p>

<%= render :partial => "header" %>

<p>Mais rápido que uma bala<br/>Um grito apavorante<br/>Enfurecido e cheio de raiva<br/>Ele é metade homem, metade máquina</p>

app/views/page/about.html.erb

app/views/page/about.pt-BR.html.erb

Tuesday, April 7, 2009

<% title "Hino Oficial do Projeto Rails I18n" %><h3>Judas Priest - Painkiller</h3>

<% title "Official Rails I18n Project Hymn" %><h3>Judas Priest - Painkiller</h3>

app/views/page/_header.html.erb

app/views/page/_header.pt-BR.erb

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Browser Languages

Tuesday, April 7, 2009

Tuesday, April 7, 2009

./script/plugin install git://github.com/iain/http_accept_language.git

Tuesday, April 7, 2009

./script/plugin install git://github.com/iain/http_accept_language.git

app/controllers/application_controller.rb

Tuesday, April 7, 2009

./script/plugin install git://github.com/iain/http_accept_language.git

app/controllers/application_controller.rb

def set_locale # comment the next line to disable http_accept_language recognition params[:locale] = request.compatible_language_from( I18n.available_locales) unless params[:locale] I18n.locale = params[:locale] if params[:locale]end

Tuesday, April 7, 2009

./script/plugin install git://github.com/iain/http_accept_language.git

app/controllers/application_controller.rb

def set_locale # comment the next line to disable http_accept_language recognition params[:locale] = request.compatible_language_from( I18n.available_locales) unless params[:locale] I18n.locale = params[:locale] if params[:locale]end

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Fast Backend

Tuesday, April 7, 2009

./script/plugin install git://github.com/thedarkone/i18n.git

Tuesday, April 7, 2009

./script/plugin install git://github.com/thedarkone/i18n.git

I18n.backend = I18n::Backend::Fast.new

Tuesday, April 7, 2009

./script/plugin install git://github.com/thedarkone/i18n.git

6 ~ 20x faster at lookups

Slightly less memory efficient

Slower to reload

Faster for production

I18n.backend = I18n::Backend::Fast.new

Tuesday, April 7, 2009

Globalize 2

Tuesday, April 7, 2009

./script/plugin install git://github.com/joshmh/globalize2.git

Tuesday, April 7, 2009

./script/generate migration AddDescriptionFieldForContact

Tuesday, April 7, 2009

./script/generate migration AddDescriptionFieldForContact

class AddDescriptionFieldForContact < ActiveRecord::Migration def self.up add_column :contacts, :description, :text Contact.create_translation_table! :description => :text end

def self.down remove_column :contacts, :description Contact.drop_translation_table! endend

Tuesday, April 7, 2009

class Contact < ActiveRecord::Base translates :description validates_presence_of :nameend

app/models/contact.rb

Tuesday, April 7, 2009

class Contact < ActiveRecord::Base translates :description validates_presence_of :nameend

app/models/contact.rb

Tuesday, April 7, 2009

class Contact < ActiveRecord::Base translates :description validates_presence_of :nameend

...<p> <%= f.label :description %><br /> <%= f.text_area :description %></p><p><%= f.submit t("common.submit") %></p>

app/models/contact.rb

app/views/contacts/views/_form.html.erb

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

I18n.fallbacks[:en]

config/initializers/globalize.rb

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Tuesday, April 7, 2009

Globalize2

• Model Translation

• Custom Pluralization

• Locale Fallbacks

• Globalize::LoadPath

• Globalize::Translation classes

• Missing Translation Log Handler

Tuesday, April 7, 2009

More Resources• Gettext

• Masao’s Mutoh

• http://github.com/mutoh/gettext

• Fernando Blat

• http://github.com/ferblape/i18n_gettext

• http://guides.rails.info/i18n.html

• http://rails-i18n.org/

Tuesday, April 7, 2009

git clone git://github.com/akitaonrails/locosxrails_i18n_demo.git

Tuesday, April 7, 2009

Muchas Gracias!

www.akitaonrails.comfabioakita@gmail.com

Tuesday, April 7, 2009

top related