Class: MultiFormalI18nTenancy::Backend

Inherits:
I18n::Backend::Simple
  • Object
show all
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

Instance Method Summary collapse

Instance Attribute Details

#filenamesObject

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

#available_locale(options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/multi_formal_i18n_tenancy/backend.rb', line 21

def available_locale(options = {})
  options.assert_valid_keys(:base_locale, :formal, :tenant) if options.respond_to? :assert_valid_keys
  
  base_locale = options[:base_locale] || I18n.default_locale
  formal = options[:formal] || false
  tenant = (options[:tenant] || '').parameterize.gsub('-', '_')
   
  deepest_available_locale = nil
  
  # take last / deepest available locale of possible combinations
  variant_index = -1
  
  [     
    [
      (formal && tenant), 
      [tenant, base_locale, 'formal']
    ],
    [
      formal && tenant && available_locales.include?([tenant, base_locale].join('_').to_sym) &&
      available_locales.include?([base_locale, 'formal'].join('_').to_sym), 
      [tenant, base_locale, 'formal']
    ],    
    [tenant, [tenant, base_locale]],
    [formal, [base_locale, 'formal']],
    [true, [base_locale]], 
  ].each do |variant|
    variant_index += 1
    
    next unless variant[0]

    if available_locales.include?(variant[1].join('_').to_sym) || (variant_index == 1 && [tenant, base_locale, 'formal'] == variant[1])
      deepest_available_locale = variant[1].join('_').to_sym
    end
          
    break if deepest_available_locale
  end
  
  deepest_available_locale || I18n.default_locale
end

#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
# File 'lib/multi_formal_i18n_tenancy/backend.rb', line 13

def load_translations(*filenames)
  filenames = I18n.load_path if filenames.empty?
  
  @filenames = filenames.flatten
  
  @filenames.each { |filename| load_file(filename) }
end