SlideShare ist ein Scribd-Unternehmen logo
1 von 58
The Ruby on Rails I18n Core API PRESENTED BY -Neeraj Kumar
Introduction ,[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],[object Object]
Setup  for RoR App ,[object Object],[object Object],[object Object]
[object Object],[object Object],[object Object],en:  hello: "Hello world"
[object Object],[object Object],[object Object]
before_filter  :set_locale def   set_locale # if params[:locale] is nil then I18n.default_locale will be used I18n .locale = params[ :locale ] end - URL :  http://example.com/books?locale=pt  - will load Portuguese localization.
[object Object],[object Object],[object Object]
# app/controllers/application_controller.rb def  default_url_options (options = {}) logger.debug  “default_url_options is passed options:  #{options.inspect}” { :locale  =>  I18n .locale} end - Every helper method dependent on url_for  automatically include the locale in the query string .
# config/routes.rb map.resources   :books ,   :path_prefix   =>   '/:locale' # =>  www.example.com/nl/books map.root   '/:locale' ,   :controller   =>  “dashboard”
- Drawback of default_url_options    implementation pass the :id option, e.g. link_to    'show', book_url(:id => book)
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
before_filter  :set_locale def   set_locale I18n .locale = extract_locale_from_uri end
def   extract_locale_from_tld parsed_locale =  request.host.split( '.' ).last I18n .available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil end - parsed_locale = request.host.split('.').first to set the locale from subdomain.
[object Object],[object Object],[object Object],[object Object],[object Object]
def   set_locale logger.debug =  “* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}” I18n .locale = extract_locale_from_accept_language_header end
def   extract_locale_from_accept_language_header request.env[ 'HTTP_ACCEPT_LANGUAGE' ].scan( /^[a-z]{2}/ ).first end - Using GeoIP (or similar) Database – GeoIP Lite Country - User Profile
en: activerecord: models: user: foo admin: bar attributes: user:  login: “Handle” # => will translate User attribute “login” as “Handle” ,[object Object]
en: activerecord: errors: messages: blank: “can not has nothing” # => #<User id:nil, etc> # => u.valid? # => false # => u.errors.on(:name) # => “can not has nothing” ,[object Object]
[object Object],[object Object],[object Object],[object Object]
en: activerecord: errors: messages: already_registered: “u already is {{model}}” # => u.errors.on(:email) # => “u already is foo” ,[object Object],[object Object]
[object Object],en: activerecord: errors: template: header: one: “1 error prohibted this {{model}} from being saved” other: “{{count}} errors prohibted this {{model}} from being saved” body: “There were problems with the following fields:”
Anatomy of Gem i18n i18n backend core_ext helpers locales backend exceptions gettext helpers locale i18n version locale gettext fallbacks tag
active_record Interpolation compiler Interpolation compiler Interpolation compiler Interpolation compiler Interpolation compiler Interpolation compiler Interpolation compiler fast gettext helpers Interpolation compiler links metadata simple active_record base cache cascader chain cldr fallbacks pluralization missing translation store_procs backend
i18n.rb   ,[object Object],def   default_locale(locale) @@default_locale  = locale.to_sym  rescue   nil end ,[object Object]
i18n.rb   ,[object Object],[object Object],def   backend @@backend  ||=  Backend::Simple .new end ,[object Object]
simple.rb   ,[object Object],module   I18n module   Backend class   Simple include  Base end end end
i18n.rb   def   available_locales @@available_locales  ||= backend.available_locales end
I18n.rb   ,[object Object],[object Object],[object Object],[object Object]
I18n.rb   ,[object Object],[object Object],[object Object],[object Object]
I18n.rb   def   default_exception_handler (exception, locale, key, options) return  exception.message if  MissingTranslationData  === exception raise  exception end
I18n.rb   ,[object Object],[object Object],[object Object],[object Object]
I18n.rb   ,[object Object],def   config Thread .current[ :i18n_config ] ||=  I18n::Config .new end ,[object Object]
I18n.rb   ,[object Object],[object Object],[object Object]
I18n.rb   ,[object Object],[object Object],I18n .t  :invalid ,  :scope  => [ :active_record ,  :error_messages ]  # => I18n.translation :invalid :active_record.error_messages.invalid
I18n.rb   ,[object Object],I18n. t  :foo ,  :bar  =>   'baz' #=> 'foo baz' I18n. t  :foo ,  :count  =>   1 #=> 'foo' I18n. t  :foo ,  :count  =>   0 #=> 'foos' I18n. t  :foo ,  :count  =>   2 #=> 'foos' ,[object Object],[object Object],I18n. t  :foo ,  :default  =>   'bar'
I18n.rb   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
I18n.rb   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
base.rb   ,[object Object],def   load_translations (*filenames) filenames.each { |filename| load_file(filename) } end
base.rb   ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
base.rb   def   store_translations (locale, data, options = {}) merge_translations(locale, data, options) end
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],base.rb
base.rb   def   translate (locale, key, options = {}) raise   InvalidLocale .new(locale)  unless  locale return  key.map { |k| translate(locale, k, options) }  if  key.is_a?( Array ) if  options.empty? entry = resolve(locale, key, lookup(locale, key), options) raise ( I18n::MissingTranslationData .new(locale, key, options))  if  entry.nil?
else count, scope, default = options.values_at( :count ,  :scope ,  :default ) values = options.reject { |name, value|  RESERVED_KEYS .include?(name) } entry = lookup(locale, key, scope, options) entry = entry.nil? && default ? default(locale, key, default, options) : resolve(locale, key, entry, options) base.rb
raise ( I18n::MissingTranslationData .new(locale, key, options))  if  entry.nil? e ntry = pluralize(locale, entry, count)  if  count entry = interpolate(locale, entry, values)  if  values end entry end base.rb
base.rb   ,[object Object],[object Object],[object Object]
base.rb   ,[object Object],case  match when   '%a'  then   I18n . t(: &quot;date.abbr_day_names” ,   :locale  => locale,  :format  => format)[object.wday] when   '%A'   then   I18n .t(: &quot;date.day_names” ,  :locale  => locale,  :format  => format)[object.wday] when   '%b'   then   I18n .t(: &quot;date.abbr_month_names&quot; ,  :locale  => locale,  :format  => format)[object.mon]
    when   '%B'   then  I18n.t(: &quot;date.month_names&quot; ,  :locale  => locale,  :format  => format)[object.mon] when   '%p'  then I18n.t(: &quot;time.#{object.hour < 12 ? :am : :pm}&quot; ,  :locale  => locale,  :format  => format)  if  object.respond_to?  :hour end base.rb
Customization ,[object Object],[object Object],[object Object],[object Object]
MissingTranslationData  # no translation was found for the requested key InvalidLocale  # the locale set to I18n.locale is invalid (e.g. nil) ,[object Object]
- customization – e.g. the default exception handling does not allow to catch missing translation during automated test easily .
[object Object],[object Object],[object Object],I18n .backend =  Globalize :: Backend :: Static .new
module  I18n def  just_raise_that_exception (*args) raise  args.first end end I18n .exception_handler   =   :just_raise_that_exception
References ,[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]
[object Object]
[object Object]

Weitere ähnliche Inhalte

Was ist angesagt?

Something About Dynamic Linking
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic LinkingWang Hsiangkai
 
Workshop Assembler
Workshop AssemblerWorkshop Assembler
Workshop AssemblerTuhin_Das
 
Loader and Its types
Loader and Its typesLoader and Its types
Loader and Its typesParth Dodiya
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Bilal Amjad
 
File handling With Solve Programs
File handling With Solve ProgramsFile handling With Solve Programs
File handling With Solve ProgramsRohan Gajre
 
LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723Iftach Ian Amit
 
Loaders ( system programming )
Loaders ( system programming ) Loaders ( system programming )
Loaders ( system programming ) Adarsh Patel
 
Apache Thrift : One Stop Solution for Cross Language Communication
Apache Thrift : One Stop Solution for Cross Language CommunicationApache Thrift : One Stop Solution for Cross Language Communication
Apache Thrift : One Stop Solution for Cross Language CommunicationPiyush Goel
 
Apache Thrift, a brief introduction
Apache Thrift, a brief introductionApache Thrift, a brief introduction
Apache Thrift, a brief introductionRandy Abernethy
 
A hands-on introduction to the ELF Object file format
A hands-on introduction to the ELF Object file formatA hands-on introduction to the ELF Object file format
A hands-on introduction to the ELF Object file formatrety61
 
Linker and loader upload
Linker and loader   uploadLinker and loader   upload
Linker and loader uploadBin Yang
 
Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?Eelco Visser
 
Collaborative Peer-to-Peer Information Sharing
Collaborative Peer-to-Peer Information SharingCollaborative Peer-to-Peer Information Sharing
Collaborative Peer-to-Peer Information Sharingelliando dias
 
Loaders and Linkers
Loaders and LinkersLoaders and Linkers
Loaders and Linkerskunj desai
 

Was ist angesagt? (20)

Something About Dynamic Linking
Something About Dynamic LinkingSomething About Dynamic Linking
Something About Dynamic Linking
 
Workshop Assembler
Workshop AssemblerWorkshop Assembler
Workshop Assembler
 
Loader and Its types
Loader and Its typesLoader and Its types
Loader and Its types
 
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
Assembly Language Programming By Ytha Yu, Charles Marut Chap 8 (The Stack and...
 
File handling With Solve Programs
File handling With Solve ProgramsFile handling With Solve Programs
File handling With Solve Programs
 
LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723LD_PRELOAD Exploitation - DC9723
LD_PRELOAD Exploitation - DC9723
 
Loaders ( system programming )
Loaders ( system programming ) Loaders ( system programming )
Loaders ( system programming )
 
Pearl
PearlPearl
Pearl
 
Erlocator
ErlocatorErlocator
Erlocator
 
C++ basics
C++ basicsC++ basics
C++ basics
 
Apache Thrift : One Stop Solution for Cross Language Communication
Apache Thrift : One Stop Solution for Cross Language CommunicationApache Thrift : One Stop Solution for Cross Language Communication
Apache Thrift : One Stop Solution for Cross Language Communication
 
Apache Thrift, a brief introduction
Apache Thrift, a brief introductionApache Thrift, a brief introduction
Apache Thrift, a brief introduction
 
A hands-on introduction to the ELF Object file format
A hands-on introduction to the ELF Object file formatA hands-on introduction to the ELF Object file format
A hands-on introduction to the ELF Object file format
 
Linker and loader upload
Linker and loader   uploadLinker and loader   upload
Linker and loader upload
 
Perl_Part1
Perl_Part1Perl_Part1
Perl_Part1
 
Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?Compiler Construction | Lecture 1 | What is a compiler?
Compiler Construction | Lecture 1 | What is a compiler?
 
Facebook thrift
Facebook thriftFacebook thrift
Facebook thrift
 
loaders and linkers
 loaders and linkers loaders and linkers
loaders and linkers
 
Collaborative Peer-to-Peer Information Sharing
Collaborative Peer-to-Peer Information SharingCollaborative Peer-to-Peer Information Sharing
Collaborative Peer-to-Peer Information Sharing
 
Loaders and Linkers
Loaders and LinkersLoaders and Linkers
Loaders and Linkers
 

Ähnlich wie The Ruby On Rails I18n Core Api

Ruby i18n - internationalization for ruby
Ruby i18n - internationalization for rubyRuby i18n - internationalization for ruby
Ruby i18n - internationalization for rubyLingoHub
 
Internet Technology and its Applications
Internet Technology and its ApplicationsInternet Technology and its Applications
Internet Technology and its Applicationsamichoksi
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpointwebhostingguy
 
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj KumarThe ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj KumarThoughtWorks
 
Zend framework 06 - zend config, pdf, i18n, l10n, sessions
Zend framework 06 - zend config, pdf, i18n, l10n, sessionsZend framework 06 - zend config, pdf, i18n, l10n, sessions
Zend framework 06 - zend config, pdf, i18n, l10n, sessionsTricode (part of Dept)
 
Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#Robert Pickering
 
Drupal site translation and translation testing
Drupal site translation and translation testingDrupal site translation and translation testing
Drupal site translation and translation testingjames_andres
 
Adventurous Merb
Adventurous MerbAdventurous Merb
Adventurous MerbMatt Todd
 
Extending Zend Framework
Extending Zend FrameworkExtending Zend Framework
Extending Zend FrameworkPHPBelgium
 
Advanced Internationalization with Rails
Advanced Internationalization with RailsAdvanced Internationalization with Rails
Advanced Internationalization with RailsClinton Dreisbach
 
Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009Bastian Feder
 

Ähnlich wie The Ruby On Rails I18n Core Api (20)

Ruby i18n - internationalization for ruby
Ruby i18n - internationalization for rubyRuby i18n - internationalization for ruby
Ruby i18n - internationalization for ruby
 
Internet Technology and its Applications
Internet Technology and its ApplicationsInternet Technology and its Applications
Internet Technology and its Applications
 
Php
PhpPhp
Php
 
course slides -- powerpoint
course slides -- powerpointcourse slides -- powerpoint
course slides -- powerpoint
 
Phpwebdevelping
PhpwebdevelpingPhpwebdevelping
Phpwebdevelping
 
Capistrano Overview
Capistrano OverviewCapistrano Overview
Capistrano Overview
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Capistrano
CapistranoCapistrano
Capistrano
 
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj KumarThe ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
 
Localization in Rails
Localization in RailsLocalization in Rails
Localization in Rails
 
Zend framework 06 - zend config, pdf, i18n, l10n, sessions
Zend framework 06 - zend config, pdf, i18n, l10n, sessionsZend framework 06 - zend config, pdf, i18n, l10n, sessions
Zend framework 06 - zend config, pdf, i18n, l10n, sessions
 
Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#Combinators, DSLs, HTML and F#
Combinators, DSLs, HTML and F#
 
Drupal site translation and translation testing
Drupal site translation and translation testingDrupal site translation and translation testing
Drupal site translation and translation testing
 
Adventurous Merb
Adventurous MerbAdventurous Merb
Adventurous Merb
 
Php
PhpPhp
Php
 
Extending Zend Framework
Extending Zend FrameworkExtending Zend Framework
Extending Zend Framework
 
Ext 0523
Ext 0523Ext 0523
Ext 0523
 
Advanced Internationalization with Rails
Advanced Internationalization with RailsAdvanced Internationalization with Rails
Advanced Internationalization with Rails
 
Red5 - PHUG Workshops
Red5 - PHUG WorkshopsRed5 - PHUG Workshops
Red5 - PHUG Workshops
 
Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009Eclipse Pdt2.0 26.05.2009
Eclipse Pdt2.0 26.05.2009
 

The Ruby On Rails I18n Core Api

  • 1. The Ruby on Rails I18n Core API PRESENTED BY -Neeraj Kumar
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9. before_filter :set_locale def set_locale # if params[:locale] is nil then I18n.default_locale will be used I18n .locale = params[ :locale ] end - URL : http://example.com/books?locale=pt - will load Portuguese localization.
  • 10.
  • 11. # app/controllers/application_controller.rb def default_url_options (options = {}) logger.debug “default_url_options is passed options: #{options.inspect}” { :locale => I18n .locale} end - Every helper method dependent on url_for automatically include the locale in the query string .
  • 12. # config/routes.rb map.resources :books , :path_prefix => '/:locale' # => www.example.com/nl/books map.root '/:locale' , :controller => “dashboard”
  • 13. - Drawback of default_url_options implementation pass the :id option, e.g. link_to 'show', book_url(:id => book)
  • 14.
  • 15. before_filter :set_locale def set_locale I18n .locale = extract_locale_from_uri end
  • 16. def extract_locale_from_tld parsed_locale = request.host.split( '.' ).last I18n .available_locales.include?(parsed_locale.to_sym) ? parsed_locale : nil end - parsed_locale = request.host.split('.').first to set the locale from subdomain.
  • 17.
  • 18. def set_locale logger.debug = “* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}” I18n .locale = extract_locale_from_accept_language_header end
  • 19. def extract_locale_from_accept_language_header request.env[ 'HTTP_ACCEPT_LANGUAGE' ].scan( /^[a-z]{2}/ ).first end - Using GeoIP (or similar) Database – GeoIP Lite Country - User Profile
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25. Anatomy of Gem i18n i18n backend core_ext helpers locales backend exceptions gettext helpers locale i18n version locale gettext fallbacks tag
  • 26. active_record Interpolation compiler Interpolation compiler Interpolation compiler Interpolation compiler Interpolation compiler Interpolation compiler Interpolation compiler fast gettext helpers Interpolation compiler links metadata simple active_record base cache cascader chain cldr fallbacks pluralization missing translation store_procs backend
  • 27.
  • 28.
  • 29.
  • 30. i18n.rb def available_locales @@available_locales ||= backend.available_locales end
  • 31.
  • 32.
  • 33. I18n.rb def default_exception_handler (exception, locale, key, options) return exception.message if MissingTranslationData === exception raise exception end
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43. base.rb def store_translations (locale, data, options = {}) merge_translations(locale, data, options) end
  • 44.
  • 45. base.rb def translate (locale, key, options = {}) raise InvalidLocale .new(locale) unless locale return key.map { |k| translate(locale, k, options) } if key.is_a?( Array ) if options.empty? entry = resolve(locale, key, lookup(locale, key), options) raise ( I18n::MissingTranslationData .new(locale, key, options)) if entry.nil?
  • 46. else count, scope, default = options.values_at( :count , :scope , :default ) values = options.reject { |name, value| RESERVED_KEYS .include?(name) } entry = lookup(locale, key, scope, options) entry = entry.nil? && default ? default(locale, key, default, options) : resolve(locale, key, entry, options) base.rb
  • 47. raise ( I18n::MissingTranslationData .new(locale, key, options)) if entry.nil? e ntry = pluralize(locale, entry, count) if count entry = interpolate(locale, entry, values) if values end entry end base.rb
  • 48.
  • 49.
  • 50. when '%B' then I18n.t(: &quot;date.month_names&quot; , :locale => locale, :format => format)[object.mon] when '%p' then I18n.t(: &quot;time.#{object.hour < 12 ? :am : :pm}&quot; , :locale => locale, :format => format) if object.respond_to? :hour end base.rb
  • 51.
  • 52.
  • 53. - customization – e.g. the default exception handling does not allow to catch missing translation during automated test easily .
  • 54.
  • 55. module I18n def just_raise_that_exception (*args) raise args.first end end I18n .exception_handler = :just_raise_that_exception
  • 56.
  • 57.
  • 58.

Hinweis der Redaktion

  1. Need to understand.