Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Localization in Rails <ul><li>Localization in Rails </li></ul><ul><li>Muntasim Ahmed </li></ul><ul><li>[email_address] </l...
Localization? <ul><li>Language localization </li></ul><ul><li>Internationalization and localization </li></ul>
Localization in Rails <ul><li>Localization(i10n) </li></ul><ul><li>Localization Simplified </li></ul><ul><li>Localization ...
Rails Internationalization (I18n) <ul><li>http://guides.rubyonrails.org/i18n.html </li></ul><ul><li>Gem with rails 2.2+ </...
I18n Continued.. <ul><li>Step 1 </li></ul><ul><ul><li>#in application controller </li></ul></ul><ul><ul><li>before_filter ...
I18n Continued.. <ul><li>Step 3 </li></ul><ul><li># app/controllers/home_controller.rb  </li></ul><ul><li>class HomeContro...
I18n Continued..
I18n Continued.. <ul><li># config/locales/en.yml  </li></ul><ul><li>en:  </li></ul><ul><li>hello_world: 'Hello World'  </l...
 
Now..  <ul><li>Globalize2 </li></ul><ul><ul><li>Rails plugin </li></ul></ul><ul><ul><li>compatible with i18n API </li></ul...
Globalize2 Continued.. <ul><li>Download from here: </li></ul><ul><ul><li>http://github.com/joshmh/globalize2/downloads </l...
Globalize2 Continued.. <ul><li>For model translation </li></ul><ul><li>class Post < ActiveRecord::Base </li></ul><ul><li>t...
Globalize2 Continued.. <ul><li>We can retrieve all locale in same object </li></ul><ul><li>@post = Post.first(:include => ...
Globalize2 Continued.. <ul><li>For retrieve data </li></ul><ul><li>Post.all(:include => :globalize_translations) </li></ul...
<ul><li>Questions?? </li></ul>
Upcoming SlideShare
Loading in …5
×

Localization in Rails

2,757 views

Published on

Localization in Rails

Published in: Technology, Business
  • Be the first to comment

  • Be the first to like this

Localization in Rails

  1. 1. Localization in Rails <ul><li>Localization in Rails </li></ul><ul><li>Muntasim Ahmed </li></ul><ul><li>[email_address] </li></ul><ul><li>4 January 2010 </li></ul>
  2. 2. Localization? <ul><li>Language localization </li></ul><ul><li>Internationalization and localization </li></ul>
  3. 3. Localization in Rails <ul><li>Localization(i10n) </li></ul><ul><li>Localization Simplified </li></ul><ul><li>Localization Enhanced </li></ul><ul><li>Gibberish </li></ul><ul><li>Gettext </li></ul><ul><li>I18n </li></ul><ul><li>Globalize </li></ul><ul><li>Globalize2 </li></ul>
  4. 4. Rails Internationalization (I18n) <ul><li>http://guides.rubyonrails.org/i18n.html </li></ul><ul><li>Gem with rails 2.2+ </li></ul><ul><li>Available as I18n throughout the application </li></ul>
  5. 5. I18n Continued.. <ul><li>Step 1 </li></ul><ul><ul><li>#in application controller </li></ul></ul><ul><ul><li>before_filter :set_locale </li></ul></ul><ul><ul><li>def set_locale </li></ul></ul><ul><ul><li>I18n.locale = params[:locale] </li></ul></ul><ul><ul><li>end </li></ul></ul><ul><li>Step 2 </li></ul><ul><li>in config/locales directory we should have translation files for all locales(like en.yml,ar.yml, bn.yml ) </li></ul>
  6. 6. I18n Continued.. <ul><li>Step 3 </li></ul><ul><li># app/controllers/home_controller.rb </li></ul><ul><li>class HomeController < ApplicationController </li></ul><ul><li>def index </li></ul><ul><li>flash[:notice] = t(:hello_flash) </li></ul><ul><li>end </li></ul><ul><li>end </li></ul><ul><li># app/views/home/index.html.erb </li></ul><ul><li><h1><%=t :hello_world %></h1> </li></ul><ul><li><p><%= flash[:notice] %></p> </li></ul>
  7. 7. I18n Continued..
  8. 8. I18n Continued.. <ul><li># config/locales/en.yml </li></ul><ul><li>en: </li></ul><ul><li>hello_world: 'Hello World' </li></ul><ul><li>hello_flash: 'Hello Flash' </li></ul><ul><li># config/locales/ar.yml </li></ul><ul><li>ar: </li></ul><ul><li>hello_world: '########' </li></ul><ul><li>hello_flash: '#:):*)(*&' </li></ul>
  9. 10. Now.. <ul><li>Globalize2 </li></ul><ul><ul><li>Rails plugin </li></ul></ul><ul><ul><li>compatible with i18n API </li></ul></ul><ul><ul><li>adds model translations to ActiveRecord </li></ul></ul><ul><ul><li>And much more! </li></ul></ul>
  10. 11. Globalize2 Continued.. <ul><li>Download from here: </li></ul><ul><ul><li>http://github.com/joshmh/globalize2/downloads </li></ul></ul><ul><ul><li>Extract and place in vendor/plugins directory </li></ul></ul><ul><ul><li>Why download as its a plugin? </li></ul></ul><ul><ul><ul><li>Downloaded version contains all :) </li></ul></ul></ul>
  11. 12. Globalize2 Continued.. <ul><li>For model translation </li></ul><ul><li>class Post < ActiveRecord::Base </li></ul><ul><li>translates :title, :text </li></ul><ul><li>#all validations except uniqueness </li></ul><ul><li>end </li></ul><ul><li>#in migration file </li></ul><ul><li>Post.create_translation_table! :title => :string, :text => :text </li></ul><ul><li>For uniqueness validations </li></ul><ul><li>http://codesnippets.joyent.com/posts/show/1960 </li></ul>
  12. 13. Globalize2 Continued.. <ul><li>We can retrieve all locale in same object </li></ul><ul><li>@post = Post.first(:include => :globalize_translations) </li></ul><ul><li>I18n.locale = :en </li></ul><ul><li>@post.title # => Globalize2 rocks! </li></ul><ul><li>I18n.locale = :he </li></ul><ul><li>@post.title # => גלובאלייז 2 שולט ! </li></ul><ul><li>We can save/update in same way </li></ul><ul><li>@post = Post.new </li></ul><ul><li>I18n.locale = :en </li></ul><ul><li>@post.title ='Globalize2Rocks ' </li></ul><ul><li>I18n.locale = :he </li></ul><ul><li>@post.title # => גלובאלייז 2 שולט ! </li></ul><ul><li>@post.save </li></ul>
  13. 14. Globalize2 Continued.. <ul><li>For retrieve data </li></ul><ul><li>Post.all(:include => :globalize_translations) </li></ul><ul><li>For ordering with locale field </li></ul><ul><li>Post.all(:include => :globalize_translations, :order=> post_translations.title) </li></ul><ul><li>IF we need to display default locale we can use fallbacks </li></ul><ul><li>I18n.fallbacks.map :ar => :en #in any initializer </li></ul><ul><li>IF we need not to display default locale we can filter the query with </li></ul><ul><li>:conditions => ['locale = ?',I18n.locale] </li></ul><ul><li>this will reduce 50% hit on db rows </li></ul>
  14. 15. <ul><li>Questions?? </li></ul>

×