Class: I18nize::Loader
- Inherits:
-
Object
- Object
- I18nize::Loader
- Defined in:
- lib/i18nize/loader.rb
Overview
Loader for i18nize
Class Method Summary collapse
- .load_file(file_path, expected_locale) ⇒ Object
- .load_flat_keys(locale: "en", base_path: "config/locales") ⇒ Object
- .load_per_file(locale: "en", base_path: "config/locales") ⇒ Object
Class Method Details
.load_file(file_path, expected_locale) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/i18nize/loader.rb', line 25 def self.load_file(file_path, expected_locale) raw = YAML.load_file(file_path) # Some files may contain multiple top-level locales, we only care about the expected one raw = raw.transform_keys(&:to_s) return {} unless raw.key?(expected_locale.to_s) flatten_hash(raw[expected_locale.to_s]).transform_keys { |k| "#{expected_locale}.#{k}" } rescue Psych::SyntaxError => e warn "[i18nize] Skipping file with YAML error: #{file_path} (#{e.message})" {} end |
.load_flat_keys(locale: "en", base_path: "config/locales") ⇒ Object
21 22 23 |
# File 'lib/i18nize/loader.rb', line 21 def self.load_flat_keys(locale: "en", base_path: "config/locales") load_per_file(locale: locale, base_path: base_path).values.reduce({}) { |acc, h| acc.merge(h) } end |
.load_per_file(locale: "en", base_path: "config/locales") ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/i18nize/loader.rb', line 8 def self.load_per_file(locale: "en", base_path: "config/locales") files = Dir.glob(File.join(base_path, "**", "*.yml")).select do |f| filename = File.basename(f) filename =~ /(^|[\._])#{Regexp.escape(locale)}\.yml$/ end files.each_with_object({}) do |file_path, result| data = load_file(file_path, locale) relative_path = file_path.start_with?(base_path) ? file_path.sub(/^#{Regexp.escape(base_path)}\//, "") : file_path result[relative_path] = data unless data.empty? end end |