Class: Translatomatic::ResourceFile::PO

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

Overview

Property list resource file

Instance Attribute Summary

Attributes inherited from Base

#locale, #options, #path

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#create_variable, enabled?, #get, #get_context, #initialize, #locale_path, preferred_locale_separator, #properties, #properties=, #sentences, supports_variable_interpolation?, #to_s, #type, #variable_regex

Methods included from PathUtils

#detect_path_locale, #modify_path_locale

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/po.rb', line 9

def self.extensions
  %w[po pot]
end

.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/po.rb', line 14

def self.key_value?
  true
end

Instance Method Details

#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/po.rb', line 44

def save(target = path, options = {})
  return unless @po
  add_created_by unless options[:no_created_by]
  target.write(@po.to_s)
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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/translatomatic/resource_file/po.rb', line 19

def set(key, value)
  super(key, value)

  if @pomap.include?(key)
    po_property = @pomap[key]
    entry = po_property.entry
    if entry.plural?
      msgstr = entry.msgstr || []
      msgstr[po_property.msgstr_index] = value
      entry.msgstr = msgstr
    else
      entry.msgstr = value
    end
  else
    # new key, create po entry
    @po << {
      msgid: key,
      msgstr: value
    }
    entry = @po.entries[-1]
    add_entry(entry, :msgid, 0)
  end
end