Class: Twine::Formatters::Tizen
Constant Summary
collapse
- FORMAT_NAME =
'tizen'
- EXTENSION =
'.xml'
- DEFAULT_FILE_NAME =
'strings.xml'
- LANG_CODES =
Hash[
'eng-GB' => 'en',
'rus-RU' => 'ru',
'fra-FR' => 'fr',
'deu-DE' => 'de',
'spa-ES' => 'es',
'ita-IT' => 'it',
'ces-CZ' => 'cs',
'pol-PL' => 'pl',
'por-PT' => 'pt',
'ukr-UA' => 'uk'
]
- DEFAULT_LANG_CODES =
Hash[
]
Placeholders::PLACEHOLDER_FLAGS_WIDTH_PRECISION_LENGTH, Placeholders::PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH
Instance Attribute Summary
Attributes inherited from Abstract
#options, #strings
Class Method Summary
collapse
Instance Method Summary
collapse
#convert_placeholders_from_android_to_twine, #convert_placeholders_from_twine_to_android
Methods inherited from Abstract
#escape_quotes, #format_file, #format_key_value, #format_row, #format_section, #initialize, #output_path_for_language, #row_pattern, #set_comment_for_key, #set_translation_for_key, #write_file
Class Method Details
.can_handle_directory?(path) ⇒ Boolean
28
29
30
|
# File 'lib/twine/formatters/tizen.rb', line 28
def self.can_handle_directory?(path)
Dir.entries(path).any? { |item| /^values.*$/.match(item) }
end
|
Instance Method Details
#default_file_name ⇒ Object
32
33
34
|
# File 'lib/twine/formatters/tizen.rb', line 32
def default_file_name
return DEFAULT_FILE_NAME
end
|
#determine_language_given_path(path) ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/twine/formatters/tizen.rb', line 60
def determine_language_given_path(path)
path_arr = path.split(File::SEPARATOR)
path_arr.each do |segment|
match = /^(.*-.*)\.xml$/.match(segment)
if match
lang = match[1]
lang = LANG_CODES.fetch(lang, nil)
return lang
end
end
return
end
|
132
133
134
|
# File 'lib/twine/formatters/tizen.rb', line 132
def (row, lang)
"\t<!-- #{row..gsub('--', '—')} -->\n" if row.
end
|
116
117
118
|
# File 'lib/twine/formatters/tizen.rb', line 116
def (lang)
"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Tizen Strings File -->\n<!-- Generated by Twine #{Twine::VERSION} -->\n<!-- Language: #{lang} -->"
end
|
140
141
142
|
# File 'lib/twine/formatters/tizen.rb', line 140
def format_key(key)
key.upcase
end
|
128
129
130
|
# File 'lib/twine/formatters/tizen.rb', line 128
def (section)
"\t<!-- SECTION: #{section.name} -->"
end
|
120
121
122
123
124
125
126
|
# File 'lib/twine/formatters/tizen.rb', line 120
def format_sections(strings, lang)
result = '<string_table Bversion="2.0.0.201311071819" Dversion="20120315">'
result += super + "\n"
result += '</string_table>'
end
|
144
145
146
147
148
149
150
151
152
153
154
155
|
# File 'lib/twine/formatters/tizen.rb', line 144
def format_value(value)
value = escape_quotes(value)
value.gsub!("'", "\\\\'")
value = CGI.escapeHTML(value)
value = convert_placeholders_from_twine_to_android(value)
value.gsub(/\A *| *\z/) { |spaces| '\u0020' * spaces.length }
end
|
#key_value_pattern ⇒ Object
136
137
138
|
# File 'lib/twine/formatters/tizen.rb', line 136
def key_value_pattern
"\t<text id=\"IDS_%{key}\">%{value}</text>"
end
|
#read_file(path, lang) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/twine/formatters/tizen.rb', line 73
def read_file(path, lang)
resources_regex = /<resources(?:[^>]*)>(.*)<\/resources>/m
key_regex = /<string name="(\w+)">/
= /<!-- (.*) -->/
value_regex = /<string name="\w+">(.*)<\/string>/
key = nil
value = nil
= nil
File.open(path, 'r:UTF-8') do |f|
content_match = resources_regex.match(f.read)
if content_match
for line in content_match[1].split(/\r?\n/)
key_match = key_regex.match(line)
if key_match
key = key_match[1]
value_match = value_regex.match(line)
if value_match
value = value_match[1]
value = CGI.unescapeHTML(value)
value.gsub!('\\\'', '\'')
value.gsub!('\\"', '"')
value = convert_placeholders_from_android_to_twine(value)
value.gsub!(/(\\u0020)*|(\\u0020)*\z/) { |spaces| ' ' * (spaces.length / 6) }
else
value = ""
end
set_translation_for_key(key, lang, value)
if and .length > 0 and !.start_with?("SECTION:")
(key, )
end
= nil
end
= .match(line)
if
= [1]
end
end
end
end
end
|
#write_all_files(path) ⇒ Object
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/twine/formatters/tizen.rb', line 36
def write_all_files(path)
if !File.directory?(path)
raise Twine::Error.new("Directory does not exist: #{path}")
end
langs_written = []
Dir.foreach(path) do |item|
if item == "." or item == ".."
next
end
item = File.join(path, item)
if !File.directory?(item)
lang = determine_language_given_path(item)
if lang
write_file(item, lang)
langs_written << lang
end
end
end
if langs_written.empty?
raise Twine::Error.new("Failed to genertate any files: No languages found at #{path}")
end
end
|