Class: I18nAccessors::Methods
- Inherits:
-
Module
- Object
- Module
- I18nAccessors::Methods
- Defined in:
- lib/i18n_accessors/methods.rb
Overview
Defines methods for a set of locales to access translated attributes in those locales directly with a method call, using a suffix including the locale:
article.title_pt_br
If no locales are passed as an option to the initializer, I18n.available_locales
will be used by default.
Instance Method Summary collapse
-
#initialize(*attributes, locales: I18n.available_locales) ⇒ Methods
constructor
A new instance of Methods.
Constructor Details
#initialize(*attributes, locales: I18n.available_locales) ⇒ Methods
Returns a new instance of Methods.
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/i18n_accessors/methods.rb', line 32 def initialize(*attributes, locales: I18n.available_locales) attributes.each do |attribute| locales.each do |locale| normalized_locale = I18nAccessors.normalize_locale(locale) define_method "#{attribute}_#{normalized_locale}" do |*args| I18nAccessors.i18n_class.with_locale(locale) { send(attribute, *args) } end define_method "#{attribute}_#{normalized_locale}?" do |*args| I18nAccessors.i18n_class.with_locale(locale) { send("#{attribute}?", *args) } end define_method "#{attribute}_#{normalized_locale}=" do |value, *args| I18nAccessors.i18n_class.with_locale(locale) { send("#{attribute}=", value, *args) } end end end end |