Class: Translatomatic::ResourceFile::YAML

Inherits:
Base
  • Object
show all
Defined in:
lib/translatomatic/resource_file/yaml.rb

Instance Attribute Summary

Attributes inherited from Base

#locale, #path, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#format, #get, #sentences, #to_s, #valid?

Methods included from Util

#locale, #log, #string

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.

Parameters:

  • path (String)

    Path to the file

  • locale (String) (defaults to: nil)

    Locale of the file contents



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

.extensionsArray<String>

Returns File extensions supported by this resource file.

Returns:

  • (Array<String>)

    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

Parameters:

  • locale (String)

    for the path

Returns:

  • (Pathname)

    The path of this resource file modified for the 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.

Parameters:

  • target (Pathname) (defaults to: path)

    The destination path

  • options (Hash<Symbol, Object>) (defaults to: {})

    Output format options



44
45
46
47
48
# File 'lib/translatomatic/resource_file/yaml.rb', line 44

def save(target = path, options = {})
  out = @data.to_yaml
  out.sub!(/^---\n/m, '')
  target.write(out)
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



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