Class: ZLocalize::TranslationEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/zlocalize/translation_file.rb

Overview

:nodoc: all

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ TranslationEntry

Returns a new instance of TranslationEntry.



50
51
52
53
54
55
56
57
58
# File 'lib/zlocalize/translation_file.rb', line 50

def initialize(opts = {})
  # opts = HashWithIndifferentAccess.new(opts)
  @plural      = opts['plural']
  @source      = opts['source']
  @translation = opts['translation']
  @references  = opts['references']
  @id          = opts['id']
  @ignore      = opts['ignore']
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



47
48
49
# File 'lib/zlocalize/translation_file.rb', line 47

def id
  @id
end

#ignoreObject

Returns the value of attribute ignore.



48
49
50
# File 'lib/zlocalize/translation_file.rb', line 48

def ignore
  @ignore
end

#pluralObject

Returns the value of attribute plural.



43
44
45
# File 'lib/zlocalize/translation_file.rb', line 43

def plural
  @plural
end

#referencesObject

Returns the value of attribute references.



46
47
48
# File 'lib/zlocalize/translation_file.rb', line 46

def references
  @references
end

#sourceObject

Returns the value of attribute source.



44
45
46
# File 'lib/zlocalize/translation_file.rb', line 44

def source
  @source
end

#translationObject

Returns the value of attribute translation.



45
46
47
# File 'lib/zlocalize/translation_file.rb', line 45

def translation
  @translation
end

Instance Method Details

#add_reference(ref) ⇒ Object



60
61
62
# File 'lib/zlocalize/translation_file.rb', line 60

def add_reference(ref)
  @references << ref unless @references.include?(ref)
end

#data_to_yaml(data) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/zlocalize/translation_file.rb', line 84

def data_to_yaml(data)
  if data.nil?
    nil
  elsif data.is_a?(Array)
    "\n" +  data.map { |el| "    - \"#{ZLocalize.escape_ruby_string(el)}\"" }.join("\n")
  else
    "\"#{ZLocalize.escape_ruby_string(data)}\""
  end
end

#synchronize_references(entry) ⇒ Object



64
65
66
67
68
69
# File 'lib/zlocalize/translation_file.rb', line 64

def synchronize_references(entry)
  # first, remove references that are not present for other entry
  @references.delete_if { |ref| !entry.references.include?(ref) }
  # add all references from other entry (duplicates are avoided)
  entry.references.each { |ref| add_reference(ref) }
end

#to_yamlObject



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/zlocalize/translation_file.rb', line 71

def to_yaml
  # write YAML ourselves, to allow UTF-8 strings as they are
  out = "entry_#{sprintf('%0.6d',id.to_i)}:\n" +
        "  id: #{@id}\n" +
        "  ignore: #{@ignore ? 'true' : 'false'}\n" +
        "  plural: #{@plural ? 'true' : 'false'}\n" +
        "  references:\n" +
           @references.map { |r| "    - #{r}" }.join("\n") + "\n" +
        "  source: #{data_to_yaml(@source)}\n" +
        "  translation: #{data_to_yaml(@translation)}\n"
  out.force_encoding("UTF-8")
end