Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

[PATCH, TESTED] Allow to specify locale as variable

#9

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.

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

State: open
Milestone: none
Assigned to: Josh H Josh H

Activity

  1. Josh H
    Josh H
    • State changed from new to open

    Looks good to me. Any objections?

    February 17th, 2009 @ 08:13 PM

  2. Sven Fuchs
  3. Sébastien Grosjean - ZenCocoon
  4. tim
    tim

    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

    February 16th, 2010 @ 06:07 PM

  5. Sébastien Grosjean - ZenCocoon
  6. Sébastien Grosjean - ZenCocoon

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.