Upload
tasawr-interactive
View
2.302
Download
1
Embed Size (px)
DESCRIPTION
Localization in Rails
Citation preview
Localization in RailsLocalization in Rails
Localization in RailsLocalization in Rails
Muntasim AhmedMuntasim Ahmed
[email protected]@tekSymmetry.com
4 January 20104 January 2010
Localization?
Language localizationLanguage localization Internationalization and localizationInternationalization and localization
Localization in Rails
Localization(i10n)Localization(i10n) Localization SimplifiedLocalization Simplified Localization EnhancedLocalization Enhanced GibberishGibberish GettextGettext I18n GlobalizeGlobalize Globalize2
Rails Internationalization (I18n)
http://guides.rubyonrails.org/i18n.htmlhttp://guides.rubyonrails.org/i18n.html Gem with rails 2.2+Gem with rails 2.2+ Available as I18n throughout the applicationAvailable as I18n throughout the application
I18n Continued..
Step 1Step 1#in application controller#in application controller
before_filter :set_locale before_filter :set_locale
def set_locale def set_locale
I18n.locale = params[:locale] I18n.locale = params[:locale]
end end
Step 2Step 2
in config/locales directory we should have translation files for all in config/locales directory we should have translation files for all locales(like en.yml,ar.yml, bn.yml )locales(like en.yml,ar.yml, bn.yml )
I18n Continued..
Step 3Step 3# app/controllers/home_controller.rb # app/controllers/home_controller.rb
class HomeController < ApplicationController class HomeController < ApplicationController
def index def index
flash[:notice] = t(:hello_flash) flash[:notice] = t(:hello_flash)
end end
end end
# app/views/home/index.html.erb # app/views/home/index.html.erb
<h1><%=t :hello_world %></h1> <h1><%=t :hello_world %></h1>
<p><%= flash[:notice] %></p> <p><%= flash[:notice] %></p>
I18n Continued..
I18n Continued..
# config/locales/en.yml # config/locales/en.yml
en: en:
hello_world: 'Hello World' hello_world: 'Hello World'
hello_flash: 'Hello Flash' hello_flash: 'Hello Flash'
# config/locales/ar.yml # config/locales/ar.yml
ar: ar:
hello_world: '########' hello_world: '########'
hello_flash: '#:):*)(*&' hello_flash: '#:):*)(*&'
Now..
Globalize2Globalize2 Rails pluginRails plugin compatible with i18n APIcompatible with i18n API adds model translations to ActiveRecordadds model translations to ActiveRecord And much more!And much more!
Globalize2 Continued..
Download from here:Download from here: http://github.com/joshmh/globalize2/downloadshttp://github.com/joshmh/globalize2/downloads Extract and place in vendor/plugins directoryExtract and place in vendor/plugins directory Why download as its a plugin?Why download as its a plugin?
Downloaded version contains all :)Downloaded version contains all :)
Globalize2 Continued..
For model translationFor model translation
class Post < ActiveRecord::Baseclass Post < ActiveRecord::Base
translates :title, :texttranslates :title, :text
#all validations except uniqueness#all validations except uniqueness
endend
#in migration file#in migration file
Post.create_translation_table! :title => :string, :text => :textPost.create_translation_table! :title => :string, :text => :text
For uniqueness validationsFor uniqueness validationshttp://codesnippets.joyent.com/posts/show/1960http://codesnippets.joyent.com/posts/show/1960
Globalize2 Continued..
We can retrieve all locale in same objectWe can retrieve all locale in same object
@post = Post.first(:include => :globalize_translations)@post = Post.first(:include => :globalize_translations)
I18n.locale = :enI18n.locale = :en
@post.title # => Globalize2 [email protected] # => Globalize2 rocks!
I18n.locale = :heI18n.locale = :he
@post.title # => @post.title # => שולט שולט22גלובאלייזגלובאלייז !!
We can save/update in same wayWe can save/update in same way
@post = Post.new@post = Post.new
I18n.locale = :enI18n.locale = :en
@post.title ='Globalize2Rocks '@post.title ='Globalize2Rocks '
I18n.locale = :heI18n.locale = :he
@post.title # => @post.title # => שולט שולט22גלובאלייזגלובאלייז !!
Globalize2 Continued..
For retrieve dataFor retrieve data
Post.all(:include => :globalize_translations)Post.all(:include => :globalize_translations)
For ordering with locale fieldFor ordering with locale field
Post.all(:include => :globalize_translations, :order=> Post.all(:include => :globalize_translations, :order=> post_translations.title)post_translations.title)
IF we need to display default locale we can use fallbacksIF we need to display default locale we can use fallbacks
I18n.fallbacks.map :ar => :en #in any initializerI18n.fallbacks.map :ar => :en #in any initializer
IF we need not to display default locale we can filter IF we need not to display default locale we can filter the query with the query with
:conditions => ['locale = ?',I18n.locale] :conditions => ['locale = ?',I18n.locale]
this will reduce 50% hit on db rowsthis will reduce 50% hit on db rows
Questions??Questions??