Class: Translatomatic::ResourceFile::YAML

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

Overview

YAML resource file

Instance Attribute Summary

Attributes inherited from Base

#locale, #path, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

enabled?, #format, #get, #initialize, #sentences, #to_s, #type

Constructor Details

This class inherits a constructor from Translatomatic::ResourceFile::Base

Class Method Details

.extensionsArray<String>

Returns File extensions supported by this resource file.

Returns:

  • (Array<String>)

    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

.is_key_value?boolean

Returns True if the file format consists of keys and values.

Returns:

  • (boolean)

    True if the file format consists of keys and values



14
15
16
# File 'lib/translatomatic/resource_file/yaml.rb', line 14

def self.is_key_value?
  true
end

.supports_variable_interpolation?boolean

Returns true if this resource file supports variable interpolation.

Returns:

  • (boolean)

    true if this resource file supports variable interpolation



19
20
21
# File 'lib/translatomatic/resource_file/yaml.rb', line 19

def self.supports_variable_interpolation?
  true
end

Instance Method Details

#create_variable(name) ⇒ String

Create an interpolated variable string.

Returns:

  • (String)

    A string representing the interpolated variable, or nil if this resource file doesn’t support variable interpolation.



60
61
62
# File 'lib/translatomatic/resource_file/yaml.rb', line 60

def create_variable(name)
  return "%{#{name}}"
end

#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)

    The target locale

Returns:

  • (Pathname)

    The path of this resource file modified for the given locale



26
27
28
29
30
31
32
33
34
# File 'lib/translatomatic/resource_file/yaml.rb', line 26

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



48
49
50
51
52
53
54
55
56
57
# File 'lib/translatomatic/resource_file/yaml.rb', line 48

def save(target = path, options = {})
  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 options[:no_created_by]
    target.write(out)
  end
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



37
38
39
40
41
42
43
44
45
# File 'lib/translatomatic/resource_file/yaml.rb', line 37

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

#variable_regexRegexp

Returns A regexp used to match interpolated variables.

Returns:

  • (Regexp)

    A regexp used to match interpolated variables



65
66
67
# File 'lib/translatomatic/resource_file/yaml.rb', line 65

def variable_regex
  /\%\s*\{.*?\}/
end