Class: Translatomatic::ResourceFile::YAML
- Defined in:
- lib/translatomatic/resource_file/yaml.rb
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
-
#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.
Methods inherited from Base
#format, #get, #sentences, #to_s, #valid?
Methods included from Util
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.
12 13 14 15 16 17 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 12 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.
7 8 9 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 7 def self.extensions %w{yml yaml} end |
Instance Method Details
#locale_path(locale) ⇒ Pathname
Note:
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
22 23 24 25 26 27 28 29 30 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 22 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.
44 45 46 47 48 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 44 def save(target = path, = {}) out = @data.to_yaml out.sub!(/^---\n/m, '') target.write(out) end |
#set(key, value) ⇒ String
Set a property
33 34 35 36 37 38 39 40 41 |
# File 'lib/translatomatic/resource_file/yaml.rb', line 33 def set(key, value) super(key, value) hash = @data path = key.split(/\./) last_key = path.pop path.each { |i| hash = (hash[i] ||= {}) } hash[last_key] = value end |