Module: Tolk::Sync::ClassMethods
- Defined in:
- lib/tolk/sync.rb
Instance Method Summary collapse
- #flat_hash(data, prefix = '', result = {}) ⇒ Object
- #load_translations ⇒ Object
- #read_primary_locale_file ⇒ Object
- #sync! ⇒ Object
Instance Method Details
#flat_hash(data, prefix = '', result = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/tolk/sync.rb', line 45 def flat_hash(data, prefix = '', result = {}) data.each do |key, value| current_prefix = prefix.present? ? "#{prefix}.#{key}" : key if !value.is_a?(Hash) || Tolk::Locale.pluralization_data?(value) result[current_prefix] = value.respond_to?(:stringify_keys) ? value.stringify_keys : value else flat_hash(value, current_prefix, result) end end result.stringify_keys end |
#load_translations ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/tolk/sync.rb', line 12 def load_translations if Tolk.config.exclude_gems_token # bypass default init_translations I18n.backend.reload! if I18n.backend.initialized? I18n.backend.instance_variable_set(:@initialized, true) translations_files = Dir[Rails.root.join('config', 'locales', "*.{rb,yml}")] if Tolk.config.block_xxx_en_yml_locale_files locale_block_filter = Proc.new { |l| ['.', '..'].include?(l) || !l.ends_with?('.yml') || l.split("/").last.match(/(.*\.){2,}/) # reject files of type xxx.en.yml } translations_files = translations_files.reject(&locale_block_filter) end I18n.backend.load_translations(translations_files) else I18n.backend.send :init_translations unless I18n.backend.initialized? # force load end translations = flat_hash(I18n.backend.send(:translations)[primary_locale.name.to_sym]) filter_out_i18n_keys(translations.merge(read_primary_locale_file)) end |
#read_primary_locale_file ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/tolk/sync.rb', line 36 def read_primary_locale_file primary_file = "#{self.locales_config_path}/#{self.primary_locale_name}.yml" if File.exist?(primary_file) flat_hash(Tolk::YAML.load_file(primary_file)[self.primary_locale_name]) else {} end end |
#sync! ⇒ Object
8 9 10 |
# File 'lib/tolk/sync.rb', line 8 def sync! sync_phrases(load_translations) end |