Class: Haml::I18n::Extractor::YamlWriter

Inherits:
Object
  • Object
show all
Includes:
Helpers::StringHelpers
Defined in:
lib/haml-i18n-extractor/extraction/yaml_writer.rb

Constant Summary

Constants included from Helpers::StringHelpers

Helpers::StringHelpers::LIMIT_KEY_NAME, Helpers::StringHelpers::NOT_ALLOWED_IN_KEYNAME

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::StringHelpers

#change_one_interpolation, #html_comment?, #interpolated?, #link_to?, #normalize_interpolation, #normalized_name

Constructor Details

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

Returns a new instance of YamlWriter.



15
16
17
18
19
20
21
22
# File 'lib/haml-i18n-extractor/extraction/yaml_writer.rb', line 15

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_writer.rb', line 11

def i18n_scope
  @i18n_scope
end

#info_for_yamlObject

Returns the value of attribute info_for_yaml.



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

def info_for_yaml
  @info_for_yaml
end

#yaml_fileObject

Returns the value of attribute yaml_file.



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

def yaml_file
  @yaml_file
end

Instance Method Details

#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/extraction/yaml_writer.rb', line 38

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

converts the blob of info passed into it into i18n yaml like {:en => {:view_name => {:key_name => :string_name } } }



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/haml-i18n-extractor/extraction/yaml_writer.rb', line 26

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