Skip to content

Instantly share code, notes, and snippets.

@romul
Created June 5, 2009 12:00
Show Gist options
  • Select an option

  • Save romul/124258 to your computer and use it in GitHub Desktop.

Select an option

Save romul/124258 to your computer and use it in GitHub Desktop.
module MultilingualModule
def self.define_ml_methods_for(attrs)
languages = APP_CONFIG[:languages].split(/\|/)
attrs.each do |method|
define_method('ml_' + method) do |*args|
yml = YAML.load(send("#{method}_ml").to_s)
hash = yml ? yml : {}
languages.each do |lang|
hash[lang.to_sym] = "" unless hash[lang.to_sym]
end
hash
end
define_method(method) do |*args|
ml_values = send "ml_#{method}"
ml_values[I18n.locale.to_sym]
end
end
attrs.each do |method|
languages.each do |lang|
define_method("#{method}_#{lang}") do |*args|
ml_values = send "ml_#{method}"
ml_values[lang.to_sym]
end
define_method("#{method}_#{lang}=") do |value, *args|
ml_values = send "ml_#{method}"
ml_values[lang.to_sym] = value
send "#{method}_ml=", ml_values.to_yaml
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment