Class: Traductor::LocaleFile

Inherits:
Object
  • Object
show all
Defined in:
lib/traductor/locale_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ LocaleFile

Returns a new instance of LocaleFile.



10
11
12
13
14
15
# File 'lib/traductor/locale_file.rb', line 10

def initialize(path)
  @path = path
  @format = detect_format(path)
  @data = parse
  @locale = extract_locale
end

Instance Attribute Details

#data ⇒ Object (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/traductor/locale_file.rb', line 8

def data
  @data
end

#format ⇒ Object (readonly)

Returns the value of attribute format.



8
9
10
# File 'lib/traductor/locale_file.rb', line 8

def format
  @format
end

#locale ⇒ Object (readonly)

Returns the value of attribute locale.



8
9
10
# File 'lib/traductor/locale_file.rb', line 8

def locale
  @locale
end

#path ⇒ Object (readonly)

Returns the value of attribute path.



8
9
10
# File 'lib/traductor/locale_file.rb', line 8

def path
  @path
end

Instance Method Details

#content ⇒ Object

Returns the content hash without the top-level locale key. For YAML: => {"hello" => "Hello"} -> => "Hello" For JSON: returns as-is if no single top-level locale key



20
21
22
23
24
25
26
# File 'lib/traductor/locale_file.rb', line 20

def content
  if data.is_a?(Hash) && data.keys.length == 1
    data.values.first
  else
    data
  end
end

#write(data, output_path) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/traductor/locale_file.rb', line 28

def write(data, output_path)
  dir = File.dirname(output_path)
  FileUtils.mkdir_p(dir) unless File.directory?(dir)

  case detect_format(output_path)
  when :yaml
    File.write(output_path, data.to_yaml)
  when :json
    File.write(output_path, JSON.pretty_generate(data) + "\n")
  end
end