-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransifex
111 lines (95 loc) · 2.9 KB
/
transifex
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
#!/usr/bin/env ruby
require 'open3'
if `which tx`.strip.empty?
puts "", "The Transifex client needs to be installed to use this script."
puts "Instructions are here: http://docs.transifex.com/developer/client/setup"
puts "", "On Mac:", ""
puts " curl -O https://raw.github.com/pypa/pip/master/contrib/get-pip.py"
puts " sudo python get-pip.py"
puts " sudo pip install transifex-client", ""
exit 1
end
YML_FILE_COMMENTS = <<END
# encoding: utf-8
#
# Never edit this file. It will be overwritten when translations are pulled from Transifex.
#
END
ALL_LOCALES = Dir.glob( File.expand_path("../../config/locales/client.*.yml", __FILE__) ).map {|x| x.split('.')[-2]}.sort
LOCALE_MAPPINGS = [['zh-CN', 'zh_CN'], ['zh-TW', 'zh_TW']]
YML_DIRS = ['config/locales']
def push
puts "Pulling new translations...", ""
command = "tx push -s -t #{ARGV.include?('force') ? '-f' : ''}"
Open3.popen2e(command) do |stdin, stdout_err, wait_thr|
while line = stdout_err.gets
puts line
end
end
puts ""
unless $?.success?
puts "Something failed. Check the output above.", ""
exit $?.exitstatus
end
end
def pull
puts "Pulling new translations...", ""
command = "tx pull -a #{ARGV.include?('force') ? '-f' : ''}"
Open3.popen2e(command) do |stdin, stdout_err, wait_thr|
while line = stdout_err.gets
puts line
end
end
puts ""
unless $?.success?
puts "Something failed. Check the output above.", ""
exit $?.exitstatus
end
# Change root element in yml files for some languages because Transifex uses a different
# locale code.
LOCALE_MAPPINGS.each do |ours, theirs|
['client', 'server'].each do |base|
YML_DIRS.each do |dir|
contents = []
file_name = File.expand_path("../../#{dir}/#{base}.#{ours}.yml", __FILE__)
found = false
next unless File.exists?(file_name)
File.open(file_name, 'r') do |file|
file.each_line do |line|
if found or line.strip != "#{theirs}:"
contents << line
else
contents << "#{ours}:"
found = true
end
end
end
File.open(file_name, 'w+') do |f|
f.puts(YML_FILE_COMMENTS, '') unless contents[0][0] == '#'
f.puts contents
end
end
end
end
# Add comments to the top of files
(ALL_LOCALES - ['en']).each do |locale|
['client', 'server'].each do |base|
YML_DIRS.each do |dir|
file_name = File.expand_path("../../#{dir}/#{base}.#{locale}.yml", __FILE__)
next unless File.exists?(file_name)
contents = File.readlines(file_name)
File.open(file_name, 'w+') do |f|
f.puts(YML_FILE_COMMENTS, '') unless contents[0][0] == '#'
f.puts contents
end
end
end
end
end
if ARGV.include?('push')
push
elsif ARGV.include?('pull')
pull
else
puts "./bin/transifex [push | pull] [force]"
end