Class: Translatomatic::ResourceFile::Properties

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

Overview

Properties resource file

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?

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.

Parameters:

  • path (String)

    Path to the file

  • locale (String) (defaults to: nil)

    Locale of the file contents



14
15
16
17
18
# File 'lib/translatomatic/resource_file/properties.rb', line 14

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

Class Method Details

.extensionsArray<String>

Returns File extensions supported by this resource file.

Returns:

  • (Array<String>)

    File extensions supported by this resource file



9
10
11
# File 'lib/translatomatic/resource_file/properties.rb', line 9

def self.extensions
  %w{properties}
end

Instance Method Details

#create_variable(name) ⇒ String

Create an interpolated variable string.

Returns:

  • (String)

    A string representing the interpolated variable, or nil if this resource file doesn’t support variable interpolation.



40
41
42
# File 'lib/translatomatic/resource_file/properties.rb', line 40

def create_variable(name)
  return "{#{name}}"
end

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

This method returns an undefined value.

Save the resource file.

Parameters:

  • target (Pathname) (defaults to: path)

    The destination path

  • options (Hash<Symbol, Object>) (defaults to: {})

    Output format options



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

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") if value  # convert newlines to \n
    out += "#{key} = #{value}\n"
  end
  # escape unicode characters
  out = Translatomatic::EscapedUnicode.escape(out)
  target.write(out)
end

#supports_variable_interpolation?boolean

Returns true if this resource file supports variable interpolation.

Returns:

  • (boolean)

    true if this resource file supports variable interpolation



35
36
37
# File 'lib/translatomatic/resource_file/properties.rb', line 35

def supports_variable_interpolation?
  true
end

#variable_regexRegexp

Returns A regexp used to match interpolated variables.

Returns:

  • (Regexp)

    A regexp used to match interpolated variables



45
46
47
# File 'lib/translatomatic/resource_file/properties.rb', line 45

def variable_regex
  /\{.*?\}/
end