Class: TranslationService
- Inherits:
-
Object
- Object
- TranslationService
- Defined in:
- app/services/translation_service.rb
Instance Method Summary collapse
- #flatten_hash(hash, parent_key = '', result = {}) ⇒ Object
- #load_translations(locale = I18n.locale) ⇒ Object
- #retrieve_translation(key) ⇒ Object
- #translations(locale = I18n.locale) ⇒ Object
- #translations_params ⇒ Object
Instance Method Details
#flatten_hash(hash, parent_key = '', result = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'app/services/translation_service.rb', line 19 def flatten_hash(hash, parent_key = '', result = {}) hash.each do |key, value| new_key = parent_key.empty? ? key.to_s : "#{parent_key}.#{key}" if value.is_a?(Hash) flatten_hash(value, new_key, result) else result[new_key] = value end end result end |
#load_translations(locale = I18n.locale) ⇒ Object
15 16 17 |
# File 'app/services/translation_service.rb', line 15 def load_translations(locale = I18n.locale) @translations ||= I18n.backend.send(:translations)[locale] end |
#retrieve_translation(key) ⇒ Object
10 11 12 13 |
# File 'app/services/translation_service.rb', line 10 def retrieve_translation(key) translations = load_translations translations.dig(*key.split('.').map(&:to_sym)) end |
#translations(locale = I18n.locale) ⇒ Object
2 3 4 5 6 7 8 |
# File 'app/services/translation_service.rb', line 2 def translations(locale = I18n.locale) translations = load_translations(locale) { translations: flatten_hash(translations), locale: I18n.locale } end |
#translations_params ⇒ Object
31 32 33 34 35 36 37 38 |
# File 'app/services/translation_service.rb', line 31 def translations_params available_locales = I18n.available_locales if available_locales.include?(params[:locale]&.to_sym) { locale: params[:locale] } else { locale: :fr } end end |