Class: I18nAdminUtils::Backend::YmlManager

Inherits:
Object
  • Object
show all
Defined in:
lib/i18n_admin_utils/backend/yml_manager.rb

Class Method Summary collapse

Class Method Details

.create_file_if_not_exists(filename) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/i18n_admin_utils/backend/yml_manager.rb', line 31

def self.create_file_if_not_exists(filename)
  dir = File.dirname(filename)

  unless File.directory?(dir)
    FileUtils.mkdir_p(dir)
  end
  unless File.exists?(filename)
    File.new(filename, 'w')
    I18n.load_path += Dir[Rails.root.join(dir, '*.{rb,yml}').to_s] #Reindex files
  end
end

.parse_yml(locale) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/i18n_admin_utils/backend/yml_manager.rb', line 18

def self.parse_yml(locale)
  filename = "#{I18nAdminUtils::Config.yml_file}.#{locale}.yml"
  create_file_if_not_exists(filename)
  yml = YAML.load(File.read(filename))
  yml = {} unless yml
  yml
end

.save(yml, locale) ⇒ Object



26
27
28
29
# File 'lib/i18n_admin_utils/backend/yml_manager.rb', line 26

def self.save(yml, locale)
  filename = "#{I18nAdminUtils::Config.yml_file}.#{locale}.yml"
  File.open(filename, 'w') { |f| f.write yml.to_yaml }
end

.save_translation(locale, key, translation) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/i18n_admin_utils/backend/yml_manager.rb', line 4

def self.save_translation(locale, key, translation)
  yml = parse_yml(locale)
  yml[locale] ||= {}
  hash = yml
  last = locale
  key.split('.').each do |segment|
    hash[last] ||= {}
    hash = hash[last]
    last = segment
  end
  hash[last] = translation
  save(yml, locale)
end