Class: Translatomatic::ResourceFile::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/translatomatic/resource_file/base.rb

Overview

This class is abstract.

Subclasses implement different types of resource files

Base class for resource file implementations

Direct Known Subclasses

CSV, PO, Properties, Subtitle, Text, XCodeStrings, XML, YAML

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, options = {}) ⇒ Translatomatic::ResourceFile::Base

Create a new resource file or load an existing file. If options is unspecified, attempts to determine the locale of the file automatically, and if that fails, uses the default locale. Raises an exception if errors were encountered loading the file.

Parameters:

  • path (String) (defaults to: nil)

    Path to the file

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

    Options



39
40
41
42
43
44
45
46
47
48
# File 'lib/translatomatic/resource_file/base.rb', line 39

def initialize(path = nil, options = {})
  raise "expected options hash" if options && !options.kind_of?(Hash)
  raise t("file.unsupported", file: path) unless self.class.enabled?
  @options = options || {}
  @properties = {}
  @path = path.nil? || path.kind_of?(Pathname) ? path : Pathname.new(path)
  update_locale
  init
  load if @path && @path.exist?
end

Instance Attribute Details

#localeObject

Returns the value of attribute locale.



5
6
7
# File 'lib/translatomatic/resource_file/base.rb', line 5

def locale
  @locale
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'lib/translatomatic/resource_file/base.rb', line 6

def path
  @path
end

#propertiesHash<String,String>

Returns key -> value properties.

Returns:



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

def properties
  @properties
end

Class Method Details

.enabled?boolean

Returns True if this resource format is enabled.

Returns:

  • (boolean)

    True if this resource format is enabled



12
13
14
# File 'lib/translatomatic/resource_file/base.rb', line 12

def self.enabled?
  true
end

.extensionsArray<String>

Returns File extensions supported by this resource file.

Returns:

  • (Array<String>)

    File extensions supported by this resource file



17
18
19
# File 'lib/translatomatic/resource_file/base.rb', line 17

def self.extensions
  raise "extensions must be implemented by subclass"
end

.is_key_value?boolean

Returns True if the file format consists of keys and values.

Returns:

  • (boolean)

    True if the file format consists of keys and values



22
23
24
# File 'lib/translatomatic/resource_file/base.rb', line 22

def self.is_key_value?
  false
end

.supports_variable_interpolation?boolean

Returns true if this resource file supports variable interpolation.

Returns:

  • (boolean)

    true if this resource file supports variable interpolation



27
28
29
# File 'lib/translatomatic/resource_file/base.rb', line 27

def self.supports_variable_interpolation?
  false
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.



131
132
133
134
# File 'lib/translatomatic/resource_file/base.rb', line 131

def create_variable(name)
  return nil unless supports_variable_interpolation?
  raise "create_variable(name) must be implemented by subclass"
end

#formatString

Returns The format of this resource file, e.g. “Properties”.

Returns:

  • (String)

    The format of this resource file, e.g. “Properties”



59
60
61
# File 'lib/translatomatic/resource_file/base.rb', line 59

def format
  self.class.name.demodulize.downcase.to_sym
end

#get(key) ⇒ String

Get the value of a property

Parameters:

  • key (String)

    The name of the property

Returns:

  • (String)

    The value of the property



97
98
99
# File 'lib/translatomatic/resource_file/base.rb', line 97

def get(key)
  @properties[key]
end

#locale_path(locale) ⇒ Pathname

Create a path for the current resource file with a given locale

Parameters:

  • locale (String)

    The target locale

Returns:

  • (Pathname)

    The path of this resource file modified for the given locale



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/translatomatic/resource_file/base.rb', line 66

def locale_path(locale)
  basename = path.sub_ext('').basename.to_s

  extlist = extension_list
  if extlist.length >= 2 && loc_idx = find_locale(extlist)
    # extension(s) contains locale, replace it
    extlist[loc_idx] = locale.to_s
  elsif valid_locale?(basename)
    # basename is a locale name, replace it
    path.dirname + (locale.to_s + path.extname)
  else
    # remove any underscore and trailing text from basename
    deunderscored = basename.sub(/_.*?$/, '')
    # add _locale.ext
    filename = deunderscored + "_" + locale.to_s + path.extname
    path.dirname + filename
  end
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



54
55
56
# File 'lib/translatomatic/resource_file/base.rb', line 54

def save(target = path, options = {})
  raise "save(path) must be implemented by subclass"
end

#sentencesArray<String>

Returns All property values split into sentences.

Returns:

  • (Array<String>)

    All property values split into sentences



119
120
121
122
123
124
125
126
# File 'lib/translatomatic/resource_file/base.rb', line 119

def sentences
  sentences = []
  properties.values.each do |value|
    string = Translatomatic::String.new(value, locale)
    sentences += string.sentences
  end
  sentences
end

#set(key, value) ⇒ String

Set a property

Parameters:

  • key (String)

    The name of the property

  • value (String)

    The new value of the property

Returns:

  • (String)

    The new value of the property



105
106
107
# File 'lib/translatomatic/resource_file/base.rb', line 105

def set(key, value)
  @properties[key] = value
end

#to_sString

Returns String representation of this file.

Returns:

  • (String)

    String representation of this file



110
111
112
# File 'lib/translatomatic/resource_file/base.rb', line 110

def to_s
  path.basename.to_s
end

#typeObject



114
115
116
# File 'lib/translatomatic/resource_file/base.rb', line 114

def type
  self.class.name.demodulize
end

#variable_regexRegexp

Returns A regexp used to match interpolated variables.

Returns:

  • (Regexp)

    A regexp used to match interpolated variables



137
138
139
140
# File 'lib/translatomatic/resource_file/base.rb', line 137

def variable_regex
  return nil unless supports_variable_interpolation?
  raise "variable_regex must be implemented by subclass"
end