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.



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

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.



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

def id
  @id
end

#ignoreObject

Returns the value of attribute ignore.



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

def ignore
  @ignore
end

#pluralObject

Returns the value of attribute plural.



41
42
43
# File 'lib/zlocalize/translation_file.rb', line 41

def plural
  @plural
end

#referencesObject

Returns the value of attribute references.



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

def references
  @references
end

#sourceObject

Returns the value of attribute source.



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

def source
  @source
end

#translationObject

Returns the value of attribute translation.



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

def translation
  @translation
end

Instance Method Details

#add_reference(ref) ⇒ Object



58
59
60
# File 'lib/zlocalize/translation_file.rb', line 58

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

#data_to_yaml(data) ⇒ Object



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

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



62
63
64
65
66
67
# File 'lib/zlocalize/translation_file.rb', line 62

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



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

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