Class: LostInTranslations::Translator::Base
- Inherits:
-
Object
- Object
- LostInTranslations::Translator::Base
- Defined in:
- lib/lost_in_translations/translator/base.rb
Class Method Summary collapse
- .assign_translation(object, field, value, locale) ⇒ Object
- .check_forcing_locale(object, locale) ⇒ Object
- .translate(object, field, original_locale) ⇒ Object
- .translation_data(object) ⇒ Object
- .translation_data_field(object) ⇒ Object
- .try_get_translations(data, locale) ⇒ Object
Class Method Details
.assign_translation(object, field, value, locale) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/lost_in_translations/translator/base.rb', line 14 def self.assign_translation(object, field, value, locale) data = translation_data(object) translations = try_get_translations(data, locale) translations ||= data[locale.to_sym] = {} translations[field.to_sym] = value end |
.check_forcing_locale(object, locale) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/lost_in_translations/translator/base.rb', line 48 def self.check_forcing_locale(object, locale) force_locale_field = object.class.force_locale_field if object.respond_to?(force_locale_field) force_locale = object.send(force_locale_field) return force_locale unless force_locale.to_s.length < 2 end locale end |
.translate(object, field, original_locale) ⇒ Object
4 5 6 7 8 9 10 11 12 |
# File 'lib/lost_in_translations/translator/base.rb', line 4 def self.translate(object, field, original_locale) locale = check_forcing_locale(object, original_locale) data = translation_data(object) translations = try_get_translations(data, locale) || {} translations[field.to_sym] || translations[field.to_s] end |
.translation_data(object) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/lost_in_translations/translator/base.rb', line 24 def self.translation_data(object) translation_data_field = translation_data_field(object) data = object.send(translation_data_field) if data.nil? && object.respond_to?("#{translation_data_field}=") data = object.send("#{translation_data_field}=", {}) end data end |
.translation_data_field(object) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/lost_in_translations/translator/base.rb', line 36 def self.translation_data_field(object) translation_data_field = object.class.translation_data_field unless object.respond_to?(translation_data_field) raise \ NotImplementedError, "#{object.class.name} does not implement .#{translation_data_field}" end translation_data_field end |
.try_get_translations(data, locale) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/lost_in_translations/translator/base.rb', line 60 def self.try_get_translations(data, locale) short_locale = locale.to_s.split('-')[0] data[locale.to_sym] || data[locale.to_s] || data[short_locale.to_sym] || data[short_locale.to_s] end |