Class: Twine::Formatters::JQuery
Constant Summary
collapse
- FORMAT_NAME =
'jquery'
- EXTENSION =
'.json'
- DEFAULT_FILE_NAME =
'localize.json'
Instance Attribute Summary
Attributes inherited from Abstract
#options, #strings
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Abstract
#androidify_substitutions, #initialize, #iosify_substitutions, #set_comment_for_key, #set_translation_for_key, #write_all_files
Class Method Details
.can_handle_directory?(path) ⇒ Boolean
8
9
10
|
# File 'lib/twine/formatters/jquery.rb', line 8
def self.can_handle_directory?(path)
Dir.entries(path).any? { |item| /^.+\.json$/.match(item) }
end
|
Instance Method Details
#default_file_name ⇒ Object
12
13
14
|
# File 'lib/twine/formatters/jquery.rb', line 12
def default_file_name
return DEFAULT_FILE_NAME
end
|
#determine_language_given_path(path) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/twine/formatters/jquery.rb', line 16
def determine_language_given_path(path)
path_arr = path.split(File::SEPARATOR)
path_arr.each do |segment|
match = /^((.+)-)?([^-]+)\.json$/.match(segment)
if match
return match[3]
end
end
return
end
|
#read_file(path, lang) ⇒ Object
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/twine/formatters/jquery.rb', line 28
def read_file(path, lang)
begin
require "json"
rescue LoadError
raise Twine::Error.new "You must run 'gem install json' in order to read or write jquery-localize files."
end
open(path) do |io|
json = JSON.load(io)
json.each do |key, value|
set_translation_for_key(key, lang, value)
end
end
end
|
#write_file(path, lang) ⇒ Object
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
# File 'lib/twine/formatters/jquery.rb', line 43
def write_file(path, lang)
begin
require "json"
rescue LoadError
raise Twine::Error.new "You must run 'gem install json' in order to read or write jquery-localize files."
end
default_lang = @strings.language_codes[0]
encoding = @options[:output_encoding] || 'UTF-8'
File.open(path, "w:#{encoding}") do |f|
f.puts "/**\n * JQuery Language File\n * Generated by Twine\n * Language: #{lang}\n */"
f.puts "{"
@strings.sections.each_with_index do |section, si|
printed_section = false
section.rows.each_with_index do |row, ri|
if row.matches_tags?(@options[:tags], @options[:untagged])
if !printed_section
f.puts ''
if section.name && section.name.length > 0
f.puts "/* #{section.name} */"
end
printed_section = true
end
key = row.key
key = key.gsub('"', '\\\\"')
value = row.translated_string_for_lang(lang, default_lang)
value = value.gsub('"', '\\\\"')
= row.
if
= .gsub('*/', '* /')
end
f.print "\"#{key}\":\"#{value}\","
if && .length > 0
f.print " /* #{} */\n"
else
f.print "\n"
end
end
end
end
f.seek(-2, IO::SEEK_CUR)
f.puts "\n}"
end
end
|