Class: Twine::Formatters::Django
Instance Attribute Summary
Attributes inherited from Abstract
#options, #strings
Instance Method Summary
collapse
Methods inherited from Abstract
#escape_quotes, #format_key_value, #format_section, #format_sections, #initialize, #output_path_for_language, #set_comment_for_key, #set_translation_for_key, #should_include_row
Instance Method Details
#can_handle_directory?(path) ⇒ Boolean
12
13
14
|
# File 'lib/twine/formatters/django.rb', line 12
def can_handle_directory?(path)
Dir.entries(path).any? { |item| /^.+\.po$/.match(item) }
end
|
#default_file_name ⇒ Object
16
17
18
|
# File 'lib/twine/formatters/django.rb', line 16
def default_file_name
return 'strings.po'
end
|
#determine_language_given_path(path) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/twine/formatters/django.rb', line 20
def determine_language_given_path(path)
path_arr = path.split(File::SEPARATOR)
path_arr.each do |segment|
match = /(..)\.po$/.match(segment)
if match
return match[1]
end
end
return
end
|
#extension ⇒ Object
8
9
10
|
# File 'lib/twine/formatters/django.rb', line 8
def extension
'.po'
end
|
84
85
86
87
|
# File 'lib/twine/formatters/django.rb', line 84
def format_base_translation(row)
base_translation = row.translations[@default_lang]
"# base translation: \"#{base_translation}\"\n" if base_translation
end
|
94
95
96
|
# File 'lib/twine/formatters/django.rb', line 94
def (row, lang)
"#. #{escape_quotes(row.)}\n" if row.
end
|
65
66
67
68
69
70
|
# File 'lib/twine/formatters/django.rb', line 65
def format_file(lang)
@default_lang = @strings.language_codes[0]
result = super
@default_lang = nil
result
end
|
72
73
74
|
# File 'lib/twine/formatters/django.rb', line 72
def (lang)
"##\n # Django Strings File\n # Generated by Twine #{Twine::VERSION}\n # Language: #{lang}\n"
end
|
98
99
100
|
# File 'lib/twine/formatters/django.rb', line 98
def format_key(key)
escape_quotes(key)
end
|
4
5
6
|
# File 'lib/twine/formatters/django.rb', line 4
def format_name
'django'
end
|
80
81
82
|
# File 'lib/twine/formatters/django.rb', line 80
def format_row(row, lang)
[(row, lang), format_base_translation(row), format_key_value(row, lang)].compact.join
end
|
76
77
78
|
# File 'lib/twine/formatters/django.rb', line 76
def (section)
"#--------- #{section.name} ---------#\n"
end
|
102
103
104
|
# File 'lib/twine/formatters/django.rb', line 102
def format_value(value)
escape_quotes(value)
end
|
#key_value_pattern ⇒ Object
89
90
91
92
|
# File 'lib/twine/formatters/django.rb', line 89
def key_value_pattern
"msgid \"%{key}\"\n" +
"msgstr \"%{value}\"\n"
end
|
#read(io, lang) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/twine/formatters/django.rb', line 32
def read(io, lang)
= /#\. *"?(.*)"?$/
key_regex = /msgid *"(.*)"$/
value_regex = /msgstr *"(.*)"$/m
= nil
while line = io.gets
= .match(line)
if
= [1]
end
key_match = key_regex.match(line)
if key_match
key = key_match[1].gsub('\\"', '"')
end
value_match = value_regex.match(line)
if value_match
value = value_match[1].gsub(/"\n"/, '').gsub('\\"', '"')
end
if key and key.length > 0 and value and value.length > 0
set_translation_for_key(key, lang, value)
if and .length > 0 and !.start_with?("--------- ")
(key, )
end
key = nil
value = nil
= nil
end
end
end
|