Class: Translatomatic::ResourceFile::Base Abstract
- Inherits:
-
Object
- Object
- Translatomatic::ResourceFile::Base
- Includes:
- Flattenation, PathUtils, Util
- Defined in:
- lib/translatomatic/resource_file/base.rb
Overview
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
-
#locale ⇒ Locale
The locale of the contents of this resource file.
-
#options ⇒ Hash<Symbol,Object] Options used in the constructor
readonly
Hash<Symbol,Object] Options used in the constructor.
-
#path ⇒ Pathname
The path to this resource file.
Class Method Summary collapse
-
.enabled? ⇒ boolean
True if this resource format is enabled.
-
.extensions ⇒ Array<String>
File extensions supported by this resource file.
-
.key_value? ⇒ boolean
True if the file format consists of keys and values.
-
.preferred_locale_separator ⇒ String
Preferred character to use to separate the locale from the file basename.
-
.supports_variable_interpolation? ⇒ boolean
True if this resource file supports variable interpolation.
Instance Method Summary collapse
-
#create_variable(name) ⇒ String
Create an interpolated variable string.
-
#get(key) ⇒ String
Get the value of a property.
-
#get_context(key) ⇒ Array<String>
Get context of a property.
-
#initialize(path = nil, options = {}) ⇒ Translatomatic::ResourceFile::Base
constructor
Create a new resource file or load an existing file.
-
#locale_path(target_locale) ⇒ Pathname
Create a path for the current resource file with a given locale.
-
#properties ⇒ Hash<String,String>
Key -> value properties.
-
#properties=(properties) ⇒ Object
Set all properties.
-
#save(target = path, options = {}) ⇒ void
Save the resource file.
-
#sentences ⇒ Array<Text>
All property values split into sentences.
-
#set(key, value) ⇒ String
Set a property.
-
#to_s ⇒ String
String representation of this file.
-
#type ⇒ Symbol
The type of this resource file, e.g.
-
#variable_regex ⇒ Regexp
A regexp used to match interpolated variables.
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.
50 51 52 53 54 55 56 57 |
# File 'lib/translatomatic/resource_file/base.rb', line 50 def initialize(path = nil, = {}) raise 'expected options hash' if && !.is_a?(Hash) raise t('file.unsupported', file: path) unless self.class.enabled? initialize_attributes(path, ) update_locale init try_load end |
Instance Attribute Details
#locale ⇒ Locale
Returns 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 |
#options ⇒ Hash<Symbol,Object] Options used in the constructor (readonly)
Returns Hash<Symbol,Object] Options used in the constructor.
7 8 9 |
# File 'lib/translatomatic/resource_file/base.rb', line 7 def end |
#path ⇒ Pathname
Returns 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.
16 17 18 |
# File 'lib/translatomatic/resource_file/base.rb', line 16 def self.enabled? true end |
.extensions ⇒ Array<String>
Returns 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.
32 33 34 |
# File 'lib/translatomatic/resource_file/base.rb', line 32 def self.key_value? false end |
.preferred_locale_separator ⇒ String
Returns 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.
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.
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
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
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
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 |
#properties ⇒ Hash<String,String>
Returns 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
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.
63 64 65 |
# File 'lib/translatomatic/resource_file/base.rb', line 63 def save(target = path, = {}) raise 'save(path) must be implemented by subclass' end |
#sentences ⇒ Array<Text>
Returns 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
114 115 116 |
# File 'lib/translatomatic/resource_file/base.rb', line 114 def set(key, value) @properties[key] = value end |
#to_s ⇒ String
Returns 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 |
#type ⇒ Symbol
Returns 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_regex ⇒ Regexp
Returns 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 |