#9 open
Sébastien Grosjean - ZenCocoon

[PATCH, TESTED] Allow to specify locale as variable

Reported by Sébastien Grosjean - ZenCocoon | February 17th, 2009 @ 08:01 PM

Problematic:

Really long and dirty to ask a specific translation of a attribute. Always have to change the locale back and forth.

Exemple: When having a website in English listing attributes with French names.

Solution:

This tested patch allow to pass the locale as variable of the attribute so it fetch the locale you asked for. That makes it much easier and prettier for occasional translation request.

Comments and changes to this ticket

  • Josh H

    Josh H February 17th, 2009 @ 08:13 PM

    • State changed from “new” to “open”

    Looks good to me. Any objections?

  • Sven Fuchs
  • tim

    tim February 16th, 2010 @ 06:07 PM

    we added a little hack to define seperate methods for each localized attribute.

    for example: i have a post object, the content attribute is translated into german and english..

    class Post
     translates :content
    end
    

    generates

    Post.new.content_de and Post.new.content_en.
    

    We use this to added a form with this 2 attributes to add translations to the post content. This just works seemless with the way how rails works with forms and attributes...

    our code

    module Ours
      module Globalize

    def translates(*attr)
      attr.each do |name|
        I18n.available_locales.each do |locale|
          define_method "#{name}_#{locale}" do
            #globalize.fetch locale, name
            translations.select{|t| t.locale == locale}.first.try(name)
          end
          define_method "#{name}_#{locale}=" do |value|
            value = value.blank? ? nil : value
            globalize.write(locale, name, value)
            #self[name] = value
          end
        end
      end
      super
    end
    
    
    
    
    end end
    class Post extend Ours::Globalize translates :content end

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

The second incarnation of Globalize for Rails, which will let your Rails app speak any language on Earth, and beyond.

People watching this ticket

Attachments

Pages