Class: Babelyoda::LocalizationKey
- Inherits:
-
Object
- Object
- Babelyoda::LocalizationKey
show all
- Includes:
- Regexp
- Defined in:
- lib/babelyoda/tanker.rb,
lib/babelyoda/strings.rb,
lib/babelyoda/localization_key.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
Returns a new instance of LocalizationKey.
9
10
11
12
13
14
15
16
|
# File 'lib/babelyoda/localization_key.rb', line 9
def initialize(id, context)
@id = id
@context = context
@values = {}
@plural = plural_id?(id)
@plural_key = plural? ? (id) : nil
@id = depluralize_key(@id) if plural?
end
|
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
6
7
8
|
# File 'lib/babelyoda/localization_key.rb', line 6
def context
@context
end
|
#id ⇒ Object
Returns the value of attribute id.
5
6
7
|
# File 'lib/babelyoda/localization_key.rb', line 5
def id
@id
end
|
#values ⇒ Object
Returns the value of attribute values.
7
8
9
|
# File 'lib/babelyoda/localization_key.rb', line 7
def values
@values
end
|
Class Method Details
.parse_xml(node) ⇒ Object
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/babelyoda/tanker.rb', line 31
def self.parse_xml(node)
context = node.css('context').first
context &&= context.text
result = self.new(node[:id], context)
node.css('value').each do |value_node|
value = Babelyoda::LocalizationValue.parse_xml(value_node)
result << value if value
end
result
end
|
Instance Method Details
#<<(localization_value) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/babelyoda/localization_key.rb', line 24
def <<(localization_value)
raise "Can't merge a plural value into a non-plural key" if !plural? && localization_value.plural?
lang = localization_value.language.to_sym
value = localization_value.dup
if plural?
value.pluralize!(@plural_key)
if @values[lang]
@values[lang].merge!(value, { :preserve => true })
else
@values[lang] = value
end
else
@values[lang] = value
end
self
end
|
#drop_empty! ⇒ Object
70
71
72
73
74
|
# File 'lib/babelyoda/localization_key.rb', line 70
def drop_empty!
@values.delete_if do |id, value|
value.text.empty?
end
end
|
#empty? ⇒ Boolean
76
77
78
|
# File 'lib/babelyoda/localization_key.rb', line 76
def empty?
@values.empty?
end
|
#ensure_languages!(languages = []) ⇒ Object
80
81
82
83
84
85
86
|
# File 'lib/babelyoda/localization_key.rb', line 80
def ensure_languages!(languages = [])
languages.each do |language|
unless self.values[language]
self << Babelyoda::LocalizationValue.new(language, '')
end
end
end
|
#merge!(localization_key, options = {}) ⇒ Object
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/babelyoda/localization_key.rb', line 41
def merge!(localization_key, options = {})
updated = false
context_changed = false
new_context = localization_key.context
if @context != new_context && new_context != nil && new_context.length > 0
@context = localization_key.context
updated = context_changed = true
end
localization_key.values.each_value do |value|
if @values.has_key?(value.language.to_sym)
updated = true if @values[value.language.to_sym].merge!(value, options)
else
@values[value.language.to_sym] = value.dup
updated = true
end
end
if context_changed
@values.each_value do |value|
value.status = :requires_translation
end
end
return updated
end
|
#plural? ⇒ Boolean
22
|
# File 'lib/babelyoda/localization_key.rb', line 22
def plural? ; @plural ; end
|
#to_s ⇒ Object
18
19
20
|
# File 'lib/babelyoda/localization_key.rb', line 18
def to_s
"\"#{@id}\" [#{@values.keys.map{|k| ":#{k.to_s}"}.join(', ')}] // #{@context}"
end
|
#to_strings(io, language) ⇒ Object
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
# File 'lib/babelyoda/strings.rb', line 21
def to_strings(io, language)
return if self.values[language].nil?
io << "/* #{self.context} */\n" if self.context
if plural?
values[language].text.keys.sort.each do |plural_key|
if values[language].text[plural_key] != nil && values[language].text[plural_key].length > 0
io << "\"#{pluralize_key(id, plural_key).escape_double_quotes}\" = \"#{values[language].text[plural_key].escape_double_quotes}\";\n"
end
end
else
io << "\"#{id.escape_double_quotes}\" = \"#{values[language].text.escape_double_quotes}\";\n"
end
io << "\n"
end
|
#to_xml(xml, language = nil) ⇒ Object
42
43
44
45
46
47
48
49
50
|
# File 'lib/babelyoda/tanker.rb', line 42
def to_xml(xml, language = nil)
xml.key(:id => self.id, :is_plural => (plural? ? 'True' : 'False')) do |key|
xml << "<context>#{self.context}</context>"
self.values.each_value do |value|
next if language && (value.language.to_s != language.to_s)
value.to_xml(xml)
end
end
end
|