Class: MultiFormalI18nTenancy::Backend
- Inherits:
-
I18n::Backend::Simple
- Object
- I18n::Backend::Simple
- MultiFormalI18nTenancy::Backend
- Defined in:
- lib/multi_formal_i18n_tenancy/backend.rb
Constant Summary collapse
- FORMAL_FILENAME_PATTERN =
/_formal\.[a-zA-Z]+$/
- FORMAL_LOCALE_PATTERN =
/_formal$/
- TENANT_FILENAME_PATTERN =
/tenants/
- TENANT_LOCALE_PATTERN =
/tenants$/
Instance Attribute Summary collapse
-
#filenames ⇒ Object
Returns the value of attribute filenames.
Instance Method Summary collapse
-
#load_translations(*filenames) ⇒ Object
Accepts a list of paths to translation files.
-
#store_translations(locale, data, options = {}) ⇒ Object
Stores translations for the given locale in memory.
Instance Attribute Details
#filenames ⇒ Object
Returns the value of attribute filenames.
8 9 10 |
# File 'lib/multi_formal_i18n_tenancy/backend.rb', line 8 def filenames @filenames end |
Instance Method Details
#load_translations(*filenames) ⇒ Object
Accepts a list of paths to translation files. Loads translations from plain Ruby (*.rb) or YAML files (*.yml). See #load_rb and #load_yml for details.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/multi_formal_i18n_tenancy/backend.rb', line 13 def load_translations(*filenames) filenames = I18n.load_path if filenames.empty? @filenames = filenames.flatten [ # locale file groups order (ancestor chain) # # a) de > de_formal > your_enterprise_name_de > your_enterprise_name_de_formal # b) de > your_enterprise_name_de # de ->(f) { !f.match(FORMAL_FILENAME_PATTERN) && !f.match(TENANT_FILENAME_PATTERN) }, # de > your_enterprise_name_de ->(f) { !f.match(FORMAL_FILENAME_PATTERN) && f.match(TENANT_FILENAME_PATTERN) }, # de > de_formal ->(f) { f.match(FORMAL_FILENAME_PATTERN) && !f.match(TENANT_FILENAME_PATTERN) }, # de > de_formal > your_enterprise_name_de > your_enterprise_name_de_formal ->(f) { f.match(FORMAL_FILENAME_PATTERN) && f.match(TENANT_FILENAME_PATTERN) } ].each do |filename_filter| filenames.flatten.select{|f| filename_filter.call(f) }.each { |filename| load_file(filename) } end @filenames = [] # free memory end |
#store_translations(locale, data, options = {}) ⇒ Object
Stores translations for the given locale in memory. This uses a deep merge for the translations hash, so existing translations will be overwritten by new ones only at the deepest level of the hash.
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/multi_formal_i18n_tenancy/backend.rb', line 43 def store_translations(locale, data, = {}) locale = locale.to_sym # the has_key check assures that the inheritance will be performed for the first locale file if (locale.to_s.match(FORMAL_LOCALE_PATTERN) || tenant_from_locale?(locale)) && !translations.has_key?(locale) # inherit keys from base locale file base_locale = locale.to_s tenant = tenant_from_locale?(locale) if tenant && locale.to_s.match(FORMAL_LOCALE_PATTERN) # de > de_formal base_locale = locale.to_s.gsub(/^#{tenant}_/, '') translations[locale] = (translations[base_locale.to_sym] || {}).clone # de_formal > your_enterprise_name_de base_locale = locale.to_s.gsub(FORMAL_LOCALE_PATTERN, '') base_translations = (translations[base_locale.to_sym] || {}).clone.deep_symbolize_keys # deep_symbolize_keys? translations[locale].deep_merge!(base_translations) elsif tenant base_locale.gsub!(/^#{tenant}_/, '') else base_locale.gsub!(FORMAL_LOCALE_PATTERN, '') end translations[locale] = (translations[base_locale.to_sym] || {}).clone else translations[locale] ||= {} end data = data.deep_symbolize_keys translations[locale].deep_merge!(data) end |