Class: Babelyoda::LocalizationValue

Inherits:
Object
  • Object
show all
Includes:
Regexp
Defined in:
lib/babelyoda/tanker.rb,
lib/babelyoda/localization_value.rb

Constant Summary

Constants included from Regexp

Regexp::PLURALIZED_ID, Regexp::PLURAL_ID

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Regexp

#depluralize_key, #depluralize_value, #extract_plural_key, #plural_id?, #plural_match, #pluralize_key

Constructor Details

#initialize(language, text, status = :requires_translation) ⇒ LocalizationValue

Returns a new instance of LocalizationValue.



9
10
11
12
# File 'lib/babelyoda/localization_value.rb', line 9

def initialize(language, text, status = :requires_translation)
  @language, @text, @status = language.to_sym, text, status.to_sym
  pluralize! if plural_id?(@text)
end

Instance Attribute Details

#languageObject

Returns the value of attribute language.



5
6
7
# File 'lib/babelyoda/localization_value.rb', line 5

def language
  @language
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/babelyoda/localization_value.rb', line 6

def status
  @status
end

#textObject

Returns the value of attribute text.



7
8
9
# File 'lib/babelyoda/localization_value.rb', line 7

def text
  @text
end

Class Method Details

.parse_xml(node) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/babelyoda/tanker.rb', line 54

def self.parse_xml(node)
  if node.css('plural').first
    plural = node.css('plural').first

    plural_key = plural.css('one').first
    value_one = self.new(node[:language], plural_key.text, node[:status])
    value_one.pluralize!(:one)

    plural_key = plural.css('some').first
    value_some = self.new(node[:language], plural_key.text, node[:status])
    value_some.pluralize!(:some)

    plural_key = plural.css('many').first
    value_many = self.new(node[:language], plural_key.text, node[:status])
    value_many.pluralize!(:many)

    plural_key = plural.css('none').first
    value_none = self.new(node[:language], plural_key.text, node[:status])
    value_none.pluralize!(:none)

    value_one.merge!(value_some)
    value_one.merge!(value_many)
    value_one.merge!(value_none)
    
    value_one.text.keys.each do |k|
      value_one.text[k] = nil if value_one.text[k] == ''
    end
    
    value_one
  elsif node.text.length > 0
    self.new(node[:language], node.text, node[:status])
  end
end

Instance Method Details

#merge!(other_value, options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/babelyoda/localization_value.rb', line 30

def merge!(other_value, options = {})
  updated = false
  options = resolve_options options

  unless @language.to_sym == other_value.language.to_sym
    raise "Can't merge values in different languages: #{@language.to_sym} and #{other_value.language.to_sym}"
  end

  raise "Can't merge a plural and a non-plural value!" unless plural? == other_value.plural?

  if plural?
    [:one, :some, :many, :none].each do |plural_type|
      key_updated = merge_plural_type!(plural_type, other_value.text[plural_type], options)
      updated ||= key_updated
    end
  else
    if mergeable?(options)
      if @text != other_value.text && !other_value.nil?
        @text = other_value.text
        updated = true
      end
    end
  end
  return updated
end

#plural?Boolean

Returns:

  • (Boolean)


28
# File 'lib/babelyoda/localization_value.rb', line 28

def plural? ; text.kind_of? Hash ; end

#pluralize!(plural_key = :one) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/babelyoda/localization_value.rb', line 14

def pluralize!(plural_key = :one)
  return if plural?
  new_text = { :one => nil, :some => nil, :many => nil, :none => nil }

  if plural_id?(text)
    m = plural_match(text)
    new_text[m[2].to_sym] = depluralize_value(@text)
  else
    new_text[plural_key] = text
  end

  @text = new_text
end

#to_xml(xml) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/babelyoda/tanker.rb', line 88

def to_xml(xml)
  unless plural?
    xml.value(self.text, :language => self.language, :status => self.status)
  else
    xml.value(:language => self.language, :status => self.status) do |value|
      value.plural do |plural|
        plural.one text[:one] || ''
        plural.some text[:some] || ''
        plural.many text[:many] || ''
        plural.none text[:none] || ''
      end
    end
  end
end