I18n in Rails2.2

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    2 Favorites

    I18n in Rails2.2 - Presentation Transcript

    1. I18n feature in Rails2.2 Overview of recent Rails support for localization
    2. Who am I? • Renaud Morvan (http://twitter.com/neleanth) • 3 years of professional Rails Dev at http:// feedback20.com • App localized in 6 languages • Used Ruby-Gettext-Package then switched to custom implementation
    3. Some History • No built-in support for I18n before Rails 2.2 • Tons of plugin available: http:// wiki.rubyonrails.org/rails/pages/ internationalizationcomparison • Lots of monkey patching needed => huge risk of breakage
    4. late ‘07: Turning Point • Sep ‘07: Change in Rails patch policy delegating more power to the community • Major I18n plugin dev regroup around a common patch: Get rid of monkey patch + common API • Then lots of debate and no consensus • Early ‘08: patch stalled
    5. Eventually... • Jul ’07 Sven Fuchs (Globalize plugin dev) get back to work, rewrite the patch • Rails edge is patched and I18n gem is bundled with it. • Released in Rails2.2
    6. What is I18n Gem? • A Common Api: module I18n # Sets the current locale pseudo-globally (threadsafe) def locale=(locale); end # Translates, pluralizes and interpolates a given key def translate(key, options = {}); end alias :t :translate # Localizes time, dates and numbers to local formatting. def localize(object, options = {}); end alias :l :localize ... end
    7. What is I18n Gem? • A backend implementing it (you can use your own) class I18n::Backend:: Simple def load_translations(*filenames); end def store_translations(locale, data); end def translate(locale, key, options = {}); end def localize(locale, object, format = :default); end end
    8. I18n.t basics 1/2 • Key: I18n.t 'date.formats.short' I18n.t :'date.formats.short' • Scope: I18n.t 'date.formats.short' I18n.t 'formats.short', :scope => 'date' I18n.t 'short', :scope => 'date.formats' I18n.t 'short', :scope => ['date','formats'] • Pluralization: I18n.t :foo, :count => 0 # => 'Foos' I18n.t :foo, :count => 1 # => 'Foo' I18n.t :foo, :count => 2 # => 'Foos' • Interpolation: given :foo => \"foo {{bar}}\" I18n.t :foo, :bar => 'toto' # => 'foo toto'
    9. I18n.t Basics 2/2 • Missing translation: I18n.t :missing # raise I18n::MissingTranslationData • Default (can be translation if :sym): I18n.t :missing, :default => 'default' # => 'default' • Bulk: I18n.t :missing, :default => [:missing_too, 'default'] I18n.t [:'baz.foo', :'baz.bar'] • AllinOne: I18n.t ['attr1.blank', :'attr2.blank'], :count => 2, :scope => [:activerecord, :attributes], :default => [:'model.blank'], :attribute_name => 'attr1'# yuk !
    10. I18n.l • Time, TimeZone, DateTime I18n.l Time.zone.now, :format => 'long' # => \"dimanche 30 novembre 2008 11:29:49 UTC\" • Date I18n.l Date.today, :format => 'long' # => \"30 novembre 2008\"
    11. I18n API • Simple and efficient for I18n in model code • Minimal impact on Rails core code • Provide I18n for any Rails plugin ! • Not that simple to implement backend: those features are MANDATORY for rails • One method to Translate them all! => difficult to parse, optimize
    12. I18n::SimpleBackend • Implement the API and the features • Storage YML (nested hash) or ruby • Yet a bit slow: Benchmark.realtime { 10000.times do I18n.t [:date, :format, :short]; end; } # => 1.50350284576416 Benchmark.realtime {10000.times do _('date.formats.short') end; } # => 0.023993968963623
    13. I18n::YourBackend ? • You have to implement at least the exact same features and behavior for Rails I18n • All extra features won’t be supported in other backend and should be implemented externaly • Will make your 118n/L10n plugin work with all rails app and plugin • No more monkey patching !
    14. I18n in Rails • Currently (Rails2.2): • AR Validation errors • Form errors • Float Number / Currency (output only) • Time/Date Format (on demand with I18n.l) • Time/Date related helpers
    15. I18n in Rails • Rails in only bundled with :en locale but since Rails2.2rc2 it comes with a config/ locale folder • To bootstrap localization of you own rails app go to http://github.com/svenfuchs/rails- i18n/tree/master • Would need a reference page where all keys used in rails are displayed, currently you have
    16. I18n in Rails: niceties • Time of custom AR validation message in class is over: class User < ActiveRecord::Base validates_presence_of :name end # will look at the different keys in this order 'activerecord.errors.messages.models.user.attributes.name. blank' 'activerecord.errors.messages.models.user.blank' 'activerecord.errors.messages.blank' It makes it possible to handle special case per langage and not in models
    17. I18n in Rails: views • In views you have the “t” helper: <%= t \"my.message.here\" %> <%= t \"my.plural.message.here\", :count => 2 %> ... Its just an alias to I18n.t
    18. Resources • The I18n gem: http://github.com/svenfuchs/ i18n/tree • Rails translation for SimpleBackend: http:// github.com/svenfuchs/rails-i18n/tree/master • Example rails App: http://github.com/ clemens/i18n_demo_app/tree/master • Live: http://i18n-demo.phusion.nl/
    19. Thank you • Big big big thanks to Sven Fuchs and all the I18n team (matt, ...) • Any question ?

    + Renaud MorvanRenaud Morvan, 2 years ago

    custom

    1924 views, 2 favs, 0 embeds more stats

    English slide introducing the i18n gem in Rails2.2

    More info about this presentation

    © All Rights Reserved

    • Total Views 1924
      • 1924 on SlideShare
      • 0 from embeds
    • Comments 0
    • Favorites 2
    • Downloads 11
    Most viewed embeds

    more

    All embeds

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories