Module: Tolk::Sync::ClassMethods

Defined in:
lib/tolk/sync.rb

Instance Method Summary collapse

Instance Method Details

#flat_hash(data, prefix = '', result = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tolk/sync.rb', line 23

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_translationsObject



12
13
14
15
16
# File 'lib/tolk/sync.rb', line 12

def load_translations
  I18n.available_locales # force load
  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_fileObject



18
19
20
21
# File 'lib/tolk/sync.rb', line 18

def read_primary_locale_file
  primary_file = "#{self.locales_config_path}/#{self.primary_locale_name}.yml"
  File.exists?(primary_file) ? flat_hash(YAML::load(IO.read(primary_file))[self.primary_locale_name]) : {}
end

#sync!Object



8
9
10
# File 'lib/tolk/sync.rb', line 8

def sync!
  sync_phrases(load_translations)
end