Module: I18n::Processes::Configuration

Included in:
BaseProcess
Defined in:
lib/i18n/processes/configuration.rb

Overview

rubocop:disable Metrics/ModuleLength

Constant Summary collapse

DEFAULTS =
{
  base_locale:     'en',
  internal_locale: 'en',
  search:          ::I18n::Processes::UsedKeys::SEARCH_DEFAULTS,
  data:            ::I18n::Processes::Data::DATA_DEFAULTS
}.freeze
CONFIG_FILES =
%w[
  config/i18n-processes.yml config/i18n-processes.yml.erb
  i18n-processes.yml i18n-processes.yml.erb
].freeze
IGNORE_TYPES =
[nil, :missing, :unused, :eq_base].freeze

Instance Method Summary collapse

Instance Method Details

#base_localeString

Returns default i18n locale.

Returns:

  • (String)

    default i18n locale



71
72
73
# File 'lib/i18n/processes/configuration.rb', line 71

def base_locale
  @config_sections[:base_locale] ||= (config[:base_locale] || DEFAULTS[:base_locale]).to_s
end

#configHash{String => String,Hash,Array}

i18n-processes config (defaults + config/i18n-processes.yml)

Returns:

  • (Hash{String => String,Hash,Array})


13
14
15
# File 'lib/i18n/processes/configuration.rb', line 13

def config
  @config || (self.config = {})
end

#config=(conf) ⇒ Object



39
40
41
42
# File 'lib/i18n/processes/configuration.rb', line 39

def config=(conf)
  @config = file_config.deep_merge(conf)
  @config_sections = {}
end

#config_for_inspectObject



110
111
112
113
114
115
116
# File 'lib/i18n/processes/configuration.rb', line 110

def config_for_inspect
  to_hash_from_indifferent(config_sections.reject { |_k, v| v.blank? }).tap do |sections|
    sections.each_value do |section|
      section.merge! section.delete('config') if section.is_a?(Hash) && section.key?('config')
    end
  end
end

#config_sectionsObject

evaluated configuration (as the app sees it)



96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/i18n/processes/configuration.rb', line 96

def config_sections
  # init all sections
  base_locale
  internal_locale
  locales
  data_config
  @config_sections[:search] ||= search_config
  translation_config
  IGNORE_TYPES.each do |ignore_type|
    ignore_config ignore_type
  end
  @config_sections
end

#data_configObject

data config

@return [Hash<adapter: String, options: Hash>]


46
47
48
49
50
51
52
53
# File 'lib/i18n/processes/configuration.rb', line 46

def data_config
  @config_sections[:data] ||= begin
    {
      adapter: data.class.name,
      config:  data.config
    }
  end
end

#file_configObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/i18n/processes/configuration.rb', line 22

def file_config
  file   = CONFIG_FILES.detect { |f| File.exist?(f) }
  # rubocop:disable Security/Eval
  config = file && YAML.load(eval(Erubi::Engine.new(File.read(file, encoding: 'UTF-8')).src))
  # rubocop:enable Security/Eval
  if config.present?
    config.with_indifferent_access.tap do |c|
      if c[:relative_roots]
        warn_deprecated 'Please move relative_roots under search in config/i18n-processes.yml.'
        c[:search][:relative_roots] = c.delete(:relative_roots)
      end
    end
  else
    {}.with_indifferent_access
  end
end

#ignore_config(type = nil) ⇒ Object



89
90
91
92
# File 'lib/i18n/processes/configuration.rb', line 89

def ignore_config(type = nil)
  key = type ? "ignore_#{type}" : 'ignore'
  @config_sections[key] ||= config[key]
end

#internal_localeObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/i18n/processes/configuration.rb', line 75

def internal_locale
  @config_sections[:internal_locale] ||= begin
    internal_locale = (config[:internal_locale] || DEFAULTS[:internal_locale]).to_s
    valid_locales = Dir[File.join(I18n::Processes.gem_path, 'config', 'locales', '*.yml')]
                    .map { |f| File.basename(f, '.yml') }
    unless valid_locales.first.include?(internal_locale)
      log_warn "invalid internal_locale #{internal_locale.inspect}. "\
               "Available internal locales: #{valid_locales * ', '}."
      internal_locale = DEFAULTS[:internal_locale].to_s
    end
    internal_locale
  end
end

#localesArray<String>

Returns all available locales, base_locale is always first.

Returns:

  • (Array<String>)

    all available locales, base_locale is always first



66
67
68
# File 'lib/i18n/processes/configuration.rb', line 66

def locales
  @config_sections[:locales] ||= data.locales
end

#translation_configHash{String => String,Hash,Array}

translation config

Returns:

  • (Hash{String => String,Hash,Array})


57
58
59
60
61
62
63
# File 'lib/i18n/processes/configuration.rb', line 57

def translation_config
  @config_sections[:translation] ||= begin
    conf = (config[:translation] || {}).with_indifferent_access
    conf[:api_key] ||= ENV['GOOGLE_TRANSLATE_API_KEY'] if ENV.key?('GOOGLE_TRANSLATE_API_KEY')
    conf
  end
end