Class: Translatomatic::ResourceFile::Base Abstract
- Inherits:
-
Object
- Object
- Translatomatic::ResourceFile::Base
- Includes:
- 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
Instance Attribute Summary collapse
-
#locale ⇒ Object
Returns the value of attribute locale.
-
#path ⇒ Object
Returns the value of attribute path.
-
#properties ⇒ Hash<String,String>
Key -> value properties.
Class Method Summary collapse
-
.extensions ⇒ Array<String>
File extensions supported by this resource file.
Instance Method Summary collapse
-
#create_variable(name) ⇒ String
Create an interpolated variable string.
-
#format ⇒ String
The format of this resource file, e.g.
-
#get(key) ⇒ String
Get the value of a property.
-
#initialize(path, locale = nil) ⇒ Translatomatic::ResourceFile::Base
constructor
Create a new resource file.
-
#locale_path(locale) ⇒ Pathname
Create a path for the current resource file with a given locale.
-
#save(target = path, options = {}) ⇒ void
Save the resource file.
-
#sentences ⇒ Array<String>
All property values split into sentences.
-
#set(key, value) ⇒ String
Set a property.
-
#supports_variable_interpolation? ⇒ boolean
True if this resource file supports variable interpolation.
-
#to_s ⇒ String
String representation of this file.
-
#valid? ⇒ Boolean
Test if the current resource file is valid.
-
#variable_regex ⇒ Regexp
A regexp used to match interpolated variables.
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.
22 23 24 25 26 27 28 |
# File 'lib/translatomatic/resource_file/base.rb', line 22 def initialize(path, locale = nil) @path = path.kind_of?(Pathname) ? path : Pathname.new(path) @locale = Translatomatic::Locale.parse(locale || detect_locale || Translatomatic::Locale.default) raise t("resource.unknown_locale") unless @locale && @locale.language @valid = false @properties = {} end |
Instance Attribute Details
#locale ⇒ Object
Returns the value of attribute locale.
5 6 7 |
# File 'lib/translatomatic/resource_file/base.rb', line 5 def locale @locale end |
#path ⇒ Object
Returns the value of attribute path.
6 7 8 |
# File 'lib/translatomatic/resource_file/base.rb', line 6 def path @path end |
Class Method Details
.extensions ⇒ Array<String>
Returns File extensions supported by this resource file.
12 13 14 |
# File 'lib/translatomatic/resource_file/base.rb', line 12 def self.extensions raise "extensions must be implemented by subclass" end |
Instance Method Details
#create_variable(name) ⇒ String
Create an interpolated variable string.
118 119 120 121 |
# File 'lib/translatomatic/resource_file/base.rb', line 118 def create_variable(name) return nil unless supports_variable_interpolation? raise "create_variable(name) must be implemented by subclass" end |
#format ⇒ String
Returns The format of this resource file, e.g. “Properties”.
31 32 33 |
# File 'lib/translatomatic/resource_file/base.rb', line 31 def format self.class.name.demodulize.downcase.to_sym end |
#get(key) ⇒ String
Get the value of a property
69 70 71 |
# File 'lib/translatomatic/resource_file/base.rb', line 69 def get(key) @properties[key] end |
#locale_path(locale) ⇒ Pathname
Create a path for the current resource file with a given locale
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/translatomatic/resource_file/base.rb', line 38 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.
91 92 93 |
# File 'lib/translatomatic/resource_file/base.rb', line 91 def save(target = path, = {}) raise "save(path) must be implemented by subclass" end |
#sentences ⇒ Array<String>
Returns All property values split into sentences.
101 102 103 104 105 106 107 108 |
# File 'lib/translatomatic/resource_file/base.rb', line 101 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
77 78 79 |
# File 'lib/translatomatic/resource_file/base.rb', line 77 def set(key, value) @properties[key] = value end |
#supports_variable_interpolation? ⇒ boolean
Returns true if this resource file supports variable interpolation.
111 112 113 |
# File 'lib/translatomatic/resource_file/base.rb', line 111 def supports_variable_interpolation? false end |
#to_s ⇒ String
Returns String representation of this file.
96 97 98 |
# File 'lib/translatomatic/resource_file/base.rb', line 96 def to_s path.basename.to_s end |
#valid? ⇒ Boolean
Test if the current resource file is valid
83 84 85 |
# File 'lib/translatomatic/resource_file/base.rb', line 83 def valid? @valid end |
#variable_regex ⇒ Regexp
Returns A regexp used to match interpolated variables.
124 125 126 127 |
# File 'lib/translatomatic/resource_file/base.rb', line 124 def variable_regex return nil unless supports_variable_interpolation? raise "variable_regex must be implemented by subclass" end |