Your SlideShare is downloading. ×
0
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
The ruby on rails i18n core api-Neeraj Kumar
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

The ruby on rails i18n core api-Neeraj Kumar

2,533

Published on

Across the world, natural or regional languages differ in many ways, (e.g. in pluralization rules). Therefore, Internationalization became a complex problem and it is hard to provide tools for …

Across the world, natural or regional languages differ in many ways, (e.g. in pluralization rules). Therefore, Internationalization became a complex problem and it is hard to provide tools for solving all problems at once. Sven Fuchs focused to provide an extensible framework and easy to use gem that is Ruby I18n (internationalization) gem.

The Ruby I18n gem is mainly designed for translating your application to a single custom language other than English or for providing multi-language support for your ruby on rails application. The pivotal point of the new I18n api in Rails is the I18n module which is provided as a gem and shipped with Rails (starting from Rails 2.2) in ActiveSupport’s vendor directory.

Therefore, during my presentation I will try to go over some of the advanced optional features and architecture of I18n gem. Besides, I will also try to cover begin with I18n gem, setup, benefits, the work flow, what's in? and what's not? Etc.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,533
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
3
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. before_filter :set_locale def set_locale # if params[:locale] is nil then I18n.default_locale will be used I18n.locale = params[:locale] end
  • 2. # app/controllers/application_controller.rb def default_url_options(options = {}) logger.debug “default_url_options is passed options: #{options.inspect}n” {:locale => I18n.locale} end
  • 3. # config/routes.rb map.resources :books, :path_prefix => '/:locale' # => www.example.com/nl/books map.root '/:locale', :controller => “dashboard”
  • 4. before_filter :set_locale def set_locale I18n.locale = extract_locale_from_uri end
  • 5. def extract_locale_from_tld parsed_locale = request.host.split('.').last I18n.available_locales.include? (parsed_locale.to_sym) ? parsed_locale : nil end
  • 6. def set_locale logger.debug = “* Accept-Language: #{request.env['HTTP_ACCEPT_LANGUAGE']}” I18n.locale = extract_locale_from_accept_language_header end
  • 7. def extract_locale_from_accept_language_header request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z] {2}/).first end
  • 8. en: activerecord: models: user: foo admin: bar attributes: user: login: “Handle” # => will translate User attribute “login” as “Handle”
  • 9. en: activerecord: errors: messages: blank: “can not has nottin” # => u = User.new # => #<User id:nil, etc> # => u.valid? # => false # => u.errors.on(:name) # => “can not has nottin”
  • 10. en: activerecord: errors: messages: already_registered: “u already is {{model}}” # => u.errors.on(:email) # => “u already is foo”
  • 11. 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:”
  • 12. i18n i18n i18n backend backend core_ext helpers locales exceptions gettext gettext helpers tag fallbacks locale version
  • 13. backend Interpolation fast simple active_record active_record compiler Interpolation gettext base compiler missing Interpolation helpers cache compiler store_procs Interpolation Interpolation cascader compiler compiler Interpolation links translation chain compiler Interpolation metadata cldr compiler Interpolation pluralization fallbacks compiler
  • 14. def default_locale(locale) @@default_locale = locale.to_sym rescue nil end
  • 15. def backend @@backend ||= Backend::Simple.new end
  • 16. module I18n module Backend class Simple include Base end end end
  • 17. def available_locales @@available_locales ||= backend.available_locales end
  • 18. def default_exception_handler(exception, locale, key, options) return exception.message if MissingTranslationData === exception raise exception end
  • 19. def config Thread.current[:i18n_config] ||= I18n::Config.new end
  • 20. I18n.t :invalid, :scope => [:active_record, :error_messages] # => I18n.translation :invalid :active_record.error_messa ges.invalid
  • 21. 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' I18n.t :foo, :default => 'bar'
  • 22. def translate(&args) options = args.pop if args.last.is_a?(Hash) key = args.shift locale = options && options.delete(:locale) || config.locale raises = options && options.delete(:raise) config.backend.translate(locale, key, options || {}) rescue I18n::ArgumentError => exception raise exception if raises handle_exception(exception, locale, key, options) end alias :t :translate
  • 23. def localize(object, options = {}) locale = options.delete(:locale) || config.locale format = options.delete(:format) || :default config.backend.localize(locale, object, format, options) end alias :l :localize
  • 24. def load_translations(*filenames) filenames.each { |filename| load_file(filename) } end
  • 25. def load_file(filename) type = File.extname(filename).tr('.', '').downcase raise UnknownFileType.new(type, filename) unless respond_to?(:”load_#(type)”) data = send(:”load_#(type)”, filename) data.each { |locale, d| merge_translation(locale, d) } end
  • 26. def store_translations(locale, data, options = {}) merge_translations(locale, data, options) end
  • 27. def merge_translations(locale, data, options = {}) locale = locale.to_sym translations[locale] ||= {} separator = options[:separator] || I18n.default_separator data = unwind_keys(data, separator) data = deep_symbolized_keys(data) merger = proc do |key, v1, v2| Hash === v1 && Hash === v2 ? v1.merge(v2, &merger) : (v2 || v1) end translations[locale].merge!(data, &merger) end
  • 28. 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?
  • 29. 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)
  • 30. raise(I18n::MissingTranslationData.new(locale, key, options)) if entry.nil? entry = pluralize(locale, entry, count) if count entry = interpolate(locale, entry, values) if values end entry end
  • 31. case match when '%a' then I18n.t(:"date.abbr_day_names”, :locale => locale, :format => format)[object.wday] when '%A' then I18n.t(:"date.day_names”, :locale => locale, :format => format)[object.wday] when '%b' then I18n.t(:"date.abbr_month_names", :locale => locale, :format => format)[object.mon]
  • 32. when '%B' then I18n.t(:"date.month_names", :locale => locale, :format => format)[object.mon] when '%p' then I18n.t(:"time.#{object.hour < 12 ? :am : :pm}", :locale => locale, :format => format) if object.respond_to? :hour end
  • 33. MissingTranslationData # no translation was found for the requested key InvalidLocale # the locale set to I18n.locale is invalid (e.g. nil)
  • 34. I18n.backend = Globalize::Backend::Static.new
  • 35. module I18n def just_raise_that_exception(*args) raise args.first end end I18n.exception_handler = :just_raise_that_exception

×