Repository navigation
Expand file tree
/
Copy pathimport_language
More file actions
executable file
·148 lines (131 loc) · 4.07 KB
/
Copy pathimport_language
File metadata and controls
executable file
·148 lines (131 loc) · 4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env ruby
# encoding: UTF-8
require 'rubygems'
require 'bundler/setup'
require 'net/http'
require 'yaml'
lang = ARGV[0]
if lang.to_s.empty?
puts "Usage #{$0} <language|all>"
exit
end
if lang == "all"
puts "Updating all languages"
for l in Dir["locales/*.po"].map{|f| f.match(/locales\/(.*)\.po/)[1]}.sort
system("ruby #{$0} #{l}")
end
end
entities = nil
if ARGV.delete("--encode")
require "htmlentities"
entities = HTMLEntities.new
end
source_url = "https://translate.apachefriends.org/download/#{lang}/apachefriends/template.po"
source_filename = "./locales/#{lang}.po"
destination_filename = "./locales/#{lang}.yml"
# Update PO FILE
response = nil
begin
source_url = response['location'] if response
uri = URI.parse(source_url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
response = http.get(uri.request_uri)
end while response.is_a?(Net::HTTPRedirection)
unless response.is_a?(Net::HTTPSuccess)
puts "Error fetching PO file from #{source_url} #{response}"
exit 1
end
source = response.body.force_encoding("UTF-8")
source.gsub!(/\r\n/, "\n") # dos2unix
# fix urdu placeholders and Apache/Mysql/PHP etc translations
if lang == "ur"
source.gsub!('٪', '%')
source.gsub!(/{([^}]*)}\s*%/, '%{\1}')
source.gsub!(/% "/, '%"')
source.gsub!(/\\\s*اپاچی\s*/, '\\apache')
source.gsub!('اپاچی', 'Apache')
source.gsub!('ایس کیو ایل', 'MySQL')
source.gsub!('پی ایچ پی', 'PHP')
source.gsub!('پرل', 'Perl')
source.gsub!('ونڈوز', 'Windows')
source.gsub!('وسٹا', 'Vista')
source.gsub!('میک', 'Mac')
source.gsub!(/xampp\s*کی\s*\\/, 'xampp\\')
source.gsub!(/xampp([^\.\/\\])/, 'XAMPP\1')
source.gsub!(/C\\/, 'C:\\\\')
source.gsub!(/([CD]):\s*\\/, '\1:\\')
end
# fix windows FAQ
source.gsub!(/\\ ?(\\ ?){1,}([^"])/, '\\\\\2')
File.write(source_filename, source)
puts "Updated #{source_filename}"
# Transform to YAML format
puts "Skip #{destination_filename} generation"
=begin
result = Hash.new({})
StringIO.new(source).slice_before(/#:/).each do |term|
key = term[0][/^#:\s*(.*)$\n/,1]
msgid = ""
msgstr = ""
term[1..-1].each do |line|
next if line =~ /^\s*#[^:]/
if line[0..5] != "msgstr" && msgstr.empty?
msgid += line
else
msgstr += line
end
end
msgid = msgid.split("\n").map{|l| l.strip.gsub(/^msgid\s*/, '').gsub(/(^"|"$)/, '')}.join
msgstr = msgstr.split("\n").map{|l| l.strip.gsub(/^msgstr\s*/, '').gsub(/(^"|"$)/, '')}.join
next unless key && msgid
# encode if required (and msgstr does not contain HTML)
msgstr = entities.encode(msgstr, :decimal) if entities && !msgstr[/<\/?.*>/]
#puts msgstr
if key =~ /\.[0-9]*$/
k = key.split('.')[1..-2].join('.')
result[k] = (result[k].is_a?(Array) ? result[k] : [result[k]]) + [msgstr]
else
k = key.split('.')[1..-1].join('.')
result[k] = msgstr
end
end
scoped = Hash.new
scoped[lang] = {}
result.each_pair do |key,value|
h = scoped[lang]
keys = key.split(".")
keys[0..-2].each do |k|
h[k] ||= {}
h = h[k]
end
h[keys.last] = value
end
File.open(destination_filename, 'w') { |f| f.puts scoped.to_yaml }
puts "Updated #{destination_filename} based on #{source_filename}"
=end
# Add flag if it is missing
country = lang[-2..-1].downcase
country = 'pk' if country == 'ur'
unless File.exist?("source/images/flags/#{lang}.png")
require 'tempfile'
require 'zip'
puts "Adding source/images/flags/#{lang}.png"
response = Net::HTTP.get_response(URI("http://www.famfamfam.com/lab/icons/flags/famfamfam_flag_icons.zip"))
unless response.is_a?(Net::HTTPSuccess)
puts "Error fetching #{country}.png flag from http://www.famfamfam.com/lab/icons"
end
begin
tempfile = Tempfile.new("flags.zip")
tempfile.binmode
tempfile.write(response.body)
File.open("source/images/flags/#{lang}.png", "wb") do |f|
Zip::File.open(tempfile) do |archive|
f.write(archive.read("png/#{country}.png"))
end
end
rescue Exception => e
puts "Error fetching #{country}.png flag from http://www.famfamfam.com/lab/icons #{e.to_s}"
exit
end
end