Class: Haml::I18n::Extractor::YamlTool
- Inherits:
-
Object
- Object
- Haml::I18n::Extractor::YamlTool
- Defined in:
- lib/haml-i18n-extractor/yaml_tool.rb
Instance Attribute Summary collapse
-
#locale_hash ⇒ Object
Returns the value of attribute locale_hash.
-
#locales_dir ⇒ Object
Returns the value of attribute locales_dir.
Instance Method Summary collapse
-
#initialize(locales_dir = nil) ⇒ YamlTool
constructor
this requires passing an absolute path.
- #locale_file ⇒ Object
- #write_file(filename = nil) ⇒ Object
-
#yaml_hash ⇒ Object
{:en => {:view_name => {:key_name => :string_name } } }.
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_hash ⇒ Object
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_dir ⇒ Object
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_file ⇒ Object
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_hash ⇒ Object
{: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 |