Module | ArkanisDevelopment::SimpleLocalization::LocalizedModelsByLangFile |
In: |
lib/features/localized_models_by_lang_file.rb
|
This method adds the localized_model_name and the human_attribute_name to the ActiveRecord::Base class. The original human_attribute_name is still available as human_attribute_name_without_localization.
localized_model_name returns the localized model name from the language file. If no localized name is available nil is returned.
The new human_attribute_name looks for the localized name of the attribute. If the language file does not contain a matching entry the requrest will be redirected to the original human_attribute_name method.
Note: since we are extending ActiveRecord::Base it‘s possible to call both methods directly on the base class (the scaffold method does this indirectly on the human_attribute_name method using Column#human_name). In this case we simply don‘t know which table or model we belong to and therefore we can‘t access the localized data. To prevent error messages in this situation ("undefined method `abstract_class?’ for Object:Class" because Base#table_name doesn‘t work here) localized_model_name returns nil and human_attribute_name delegates the request to it‘s former non localized version (which doesn‘t need to know the table name because it simply asks the Inflector).
This drawback of the scaffold method is fixed by the localized_column_human_name extension.
# File lib/features/localized_models_by_lang_file.rb, line 65 65: def self.included(base) 66: class << base 67: 68: def localized_model_name 69: return nil if self == ActiveRecord::Base 70: Language.entry :models, self.to_s.underscore.to_sym, :name 71: rescue EntryNotFound 72: nil 73: end 74: 75: alias_method :human_attribute_name_without_localization, :human_attribute_name 76: 77: def human_attribute_name(attribute_key_name) 78: attribute_key_name = attribute_key_name.to_s 79: return human_attribute_name_without_localization(attribute_key_name) if self == ActiveRecord::Base 80: Language.entry!(:models, self.to_s.underscore.to_sym, :attributes, attribute_key_name) 81: rescue EntryNotFound 82: human_attribute_name_without_localization(attribute_key_name) 83: end 84: 85: end 86: end