Class: Translatomatic::ResourceFile::Properties

Inherits:
Base
  • Object
show all
Defined in:
lib/translatomatic/resource_file/properties.rb

Instance Attribute Summary

Attributes inherited from Base

#locale, #path, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#format, #get, #locale_path, #sentences, #set, #to_s, #valid?

Methods included from Util

#locale, #log, #string

Constructor Details

#initialize(path, locale = nil) ⇒ Translatomatic::ResourceFile::Base

Create a new resource file. If locale is unspecified, attempts to determine the locale of the file automatically, and if that fails, uses the default locale.



12
13
14
15
16
# File 'lib/translatomatic/resource_file/properties.rb', line 12

def initialize(path, locale = nil)
  super(path, locale)
  @valid = true
  @properties = @path.exist? ? read(@path) : {}
end

Class Method Details

.extensionsArray<String>



7
8
9
# File 'lib/translatomatic/resource_file/properties.rb', line 7

def self.extensions
  %w{properties}
end

Instance Method Details

#save(target = path, options = {}) ⇒ void

This method returns an undefined value.

Save the resource file.



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/translatomatic/resource_file/properties.rb', line 19

def save(target = path, options = {})
  out = ""
  out += add_created_by unless options[:no_created_by]
  properties.each do |key, value|
    # TODO: maintain original line ending format?

    value = value.gsub("\n", "\\n")  # convert newlines to \n

    out += "#{key} = #{value}\n"
  end
  # escape unicode characters

  out = Translatomatic::EscapedUnicode.escape(out)
  target.write(out)
end