Class: Haml::I18n::Extractor::YamlTool

Inherits:
Object
  • Object
show all
Defined in:
lib/haml-i18n-extractor/yaml_tool.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locales_dir = nil) ⇒ YamlTool

this requires passing an absolute path. meaning run from a given rails root… ok? change?



16
17
18
19
20
21
# File 'lib/haml-i18n-extractor/yaml_tool.rb', line 16

def initialize(locales_dir = nil)
  @locales_dir = locales_dir ? locales_dir : "./config/locales/"
  if ! File.exists?(@locales_dir)
    FileUtils.mkdir_p(@locales_dir)
  end
end

Instance Attribute Details

#locale_hashObject

Returns the value of attribute locale_hash.



11
12
13
# File 'lib/haml-i18n-extractor/yaml_tool.rb', line 11

def locale_hash
  @locale_hash
end

#locales_dirObject

Returns the value of attribute locales_dir.



11
12
13
# File 'lib/haml-i18n-extractor/yaml_tool.rb', line 11

def locales_dir
  @locales_dir
end

Instance Method Details

#locale_fileObject



34
35
36
# File 'lib/haml-i18n-extractor/yaml_tool.rb', line 34

def locale_file
  File.join(@locales_dir, "#{i18n_scope}.yml")
end

#write_file(filename = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/haml-i18n-extractor/yaml_tool.rb', line 38

def write_file(filename = nil)
  pth = filename.nil? ? locale_file : filename
  if File.exist?(pth)
    str = File.read(pth)
    if str.empty?
      existing_yaml_hash = {}
    else
      existing_yaml_hash = YAML.load(str)
    end
  else
    existing_yaml_hash = {}
  end
  final_yaml_hash = existing_yaml_hash.deep_merge!(yaml_hash)
  f = File.open(pth, "w+")
  f.puts final_yaml_hash.to_yaml
  f.flush
end

#yaml_hashObject

{:en => {:view_name => {:key_name => :string_name } } }



24
25
26
27
28
29
30
31
32
# File 'lib/haml-i18n-extractor/yaml_tool.rb', line 24

def yaml_hash
  yml = HashWithIndifferentAccess.recursive_init
  @locale_hash.map do |line_no, info|
    unless info[:keyname].nil?
      yml[i18n_scope][standardized_viewname(info[:path])][standarized_keyname(info[:keyname])] = info[:replaced_text]
    end
  end
  yml = hashify(yml)
end