Class: Translatomatic::ResourceFile::Base Abstract

Inherits:
Object
  • Object
show all
Includes:
Flattenation, PathUtils, 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

Methods included from PathUtils

#detect_path_locale, #modify_path_locale

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



50
51
52
53
54
55
56
57
# File 'lib/translatomatic/resource_file/base.rb', line 50

def initialize(path = nil, options = {})
  raise 'expected options hash' if options && !options.is_a?(Hash)
  raise t('file.unsupported', file: path) unless self.class.enabled?
  initialize_attributes(path, options)
  update_locale
  init
  try_load
end

Instance Attribute Details

#localeLocale

Returns The locale of the contents of this resource file.

Returns:

  • (Locale)

    The locale of the contents of this resource file



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

def locale
  @locale
end

#optionsHash<Symbol,Object] Options used in the constructor (readonly)

Returns Hash<Symbol,Object] Options used in the constructor.

Returns:

  • (Hash<Symbol,Object] Options used in the constructor)

    Hash<Symbol,Object] Options used in the constructor



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

def options
  @options
end

#pathPathname

Returns The path to this resource file.

Returns:

  • (Pathname)

    The path to this resource file



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

def path
  @path
end

Class Method Details

.enabled?boolean

Returns True if this resource format is enabled.

Returns:

  • (boolean)

    True if this resource format is enabled



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

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



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

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

.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



32
33
34
# File 'lib/translatomatic/resource_file/base.rb', line 32

def self.key_value?
  false
end

.preferred_locale_separatorString

Returns Preferred character to use to separate the locale from the file basename.

Returns:

  • (String)

    Preferred character to use to separate the locale from the file basename



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

def self.preferred_locale_separator
  '.'
end

.supports_variable_interpolation?boolean

Returns true if this resource file supports variable interpolation.

Returns:

  • (boolean)

    true if this resource file supports variable interpolation



38
39
40
# File 'lib/translatomatic/resource_file/base.rb', line 38

def self.supports_variable_interpolation?
  false
end

Instance Method Details

#create_variable(name) ⇒ String

Create an interpolated variable string.

Parameters:

  • name (String)

    The variable name

Returns:

  • (String)

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



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

def create_variable(name)
  return nil unless self.class.supports_variable_interpolation?
  raise 'create_variable(name) must be implemented by subclass'
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



99
100
101
# File 'lib/translatomatic/resource_file/base.rb', line 99

def get(key)
  @properties[key]
end

#get_context(key) ⇒ Array<String>

Get context of a property

Parameters:

  • key (String)

    The name of the property

Returns:

  • (Array<String>)

    The property context, may be nil



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

def get_context(key)
  .get_context(key)
end

#locale_path(target_locale) ⇒ Pathname

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

Parameters:

  • target_locale (String)

    The target locale

Returns:

  • (Pathname)

    The path of this resource file modified for the given locale



76
77
78
79
# File 'lib/translatomatic/resource_file/base.rb', line 76

def locale_path(target_locale)
  modify_path_locale(path, target_locale,
                     self.class.preferred_locale_separator)
end

#propertiesHash<String,String>

Returns key -> value properties.

Returns:

  • (Hash<String,String>)

    key -> value properties



92
93
94
# File 'lib/translatomatic/resource_file/base.rb', line 92

def properties
  @properties.dup
end

#properties=(properties) ⇒ Object

Set all properties

Parameters:

  • properties (Hash<String,String>)

    New properties



83
84
85
86
87
88
89
# File 'lib/translatomatic/resource_file/base.rb', line 83

def properties=(properties)
  # use set rather that set @properties directly as subclasses
  # override set()
  properties.each do |key, value|
    set(key, value)
  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



63
64
65
# File 'lib/translatomatic/resource_file/base.rb', line 63

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

#sentencesArray<Text>

Returns All property values split into sentences.

Returns:

  • (Array<Text>)

    All property values split into sentences



124
125
126
127
128
129
130
131
# File 'lib/translatomatic/resource_file/base.rb', line 124

def sentences
  sentences = []
  properties.each_value do |value|
    string = build_text(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



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

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

#to_sString

Returns String representation of this file.

Returns:

  • (String)

    String representation of this file



119
120
121
# File 'lib/translatomatic/resource_file/base.rb', line 119

def to_s
  relative_path(path).to_s
end

#typeSymbol

Returns The type of this resource file, e.g. “:properties”.

Returns:

  • (Symbol)

    The type of this resource file, e.g. “:properties”



68
69
70
# File 'lib/translatomatic/resource_file/base.rb', line 68

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

#variable_regexRegexp

Returns A regexp used to match interpolated variables.

Returns:

  • (Regexp)

    A regexp used to match interpolated variables



143
144
145
146
# File 'lib/translatomatic/resource_file/base.rb', line 143

def variable_regex
  return nil unless self.class.supports_variable_interpolation?
  raise 'variable_regex must be implemented by subclass'
end