Class: Translatomatic::ResourceFile::YAML
- Defined in:
- lib/translatomatic/resource_file/yaml.rb
Overview
YAML resource file
Instance Attribute Summary
Attributes inherited from Base
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.
-
#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.
-
#set(key, value) ⇒ String
Set a property.
-
#supports_variable_interpolation? ⇒ boolean
True if this resource file supports variable interpolation.
-
#variable_regex ⇒ Regexp
A regexp used to match interpolated variables.
Methods inherited from Base
#format, #get, #sentences, #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.
14 15 16 17 18 19 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 14 def initialize(path, locale = nil) super(path, locale) @valid = true @data = {} @properties = @path.exist? ? read : {} end |
Class Method Details
.extensions ⇒ Array<String>
Returns File extensions supported by this resource file.
9 10 11 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 9 def self.extensions %w{yml yaml} end |
Instance Method Details
#create_variable(name) ⇒ String
Create an interpolated variable string.
63 64 65 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 63 def create_variable(name) return "%{#{name}}" end |
#locale_path(locale) ⇒ Pathname
localization files in rails use the following file name convention: config/locales/en.yml.
Create a path for the current resource file with a given locale
24 25 26 27 28 29 30 31 32 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 24 def locale_path(locale) if path.to_s.match(/config\/locales\/[-\w]+.yml$/) # rails style filename = locale.to_s + path.extname path.dirname + filename else super(locale) end end |
#save(target = path, options = {}) ⇒ void
This method returns an undefined value.
Save the resource file.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 46 def save(target = path, = {}) if @data data = @data data = data.transform_keys { locale.language } if ruby_i18n? out = data.to_yaml out.sub!(/^---\n/m, '') out = comment(created_by) + "\n" + out unless [:no_created_by] target.write(out) end end |
#set(key, value) ⇒ String
Set a property
35 36 37 38 39 40 41 42 43 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 35 def set(key, value) super(key, value) hash = @data path = key.to_s.split(/\./) last_key = path.pop path.each { |i| hash = (hash[i] ||= {}) } hash[last_key] = value end |
#supports_variable_interpolation? ⇒ boolean
Returns true if this resource file supports variable interpolation.
58 59 60 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 58 def supports_variable_interpolation? true end |
#variable_regex ⇒ Regexp
Returns A regexp used to match interpolated variables.
68 69 70 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 68 def variable_regex /\%\s*\{.*?\}/ end |