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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(i18n_scope = nil, yaml_file = nil) ⇒ YamlTool

Returns a new instance of YamlTool.



13
14
15
16
17
18
19
20
# File 'lib/haml-i18n-extractor/extraction/yaml_tool.rb', line 13

def initialize(i18n_scope = nil, yaml_file = nil)
  @i18n_scope = i18n_scope && i18n_scope.to_sym || :en
  @yaml_file = yaml_file || "./config/locales/#{@i18n_scope}.yml"
  locales_dir = Pathname.new(@yaml_file).dirname
  if ! File.exists?(locales_dir)
    FileUtils.mkdir_p(locales_dir)
  end
end

Instance Attribute Details

#i18n_scopeObject

Returns the value of attribute i18n_scope.



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

def i18n_scope
  @i18n_scope
end

#locale_hashObject

Returns the value of attribute locale_hash.



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

def locale_hash
  @locale_hash
end

#yaml_fileObject

Returns the value of attribute yaml_file.



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

def yaml_file
  @yaml_file
end

Instance Method Details

#write_file(filename = nil) ⇒ Object



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

def write_file(filename = nil)
  pth = filename.nil? ? @yaml_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 } } }



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

def yaml_hash
  yml = Hash.new
  @locale_hash.map do |line_no, info|
    unless info[:keyname].nil?
      keyspace = [@i18n_scope,standardized_viewnames(info[:path]), standarized_keyname(info[:keyname]),
                  info[:replaced_text]].flatten
      yml.deep_merge!(nested_hash({},keyspace))
    end
  end
  yml = hashify(yml)
end