[PATCH, TESTED] Allow to specify locale as variable
-
Sven Fuchs
looks cool :)
-
Sébastien Grosjean - ZenCocoon
do you like a push request Josh ?
-
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 endgenerates
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 -
Sébastien Grosjean - ZenCocoon
- No changes were found…
-
Sébastien Grosjean - ZenCocoon
not a big fan of this hack personally.
I've started a different path a while ago, you can find more at #6 [PENDING, PATCH, TESTED] Allow multiple translations to be set at once or even #10 [Feature] Validation and form to work nicer with locales
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.
