-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtransifex.rb
More file actions
88 lines (74 loc) · 2.69 KB
/
transifex.rb
File metadata and controls
88 lines (74 loc) · 2.69 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
=begin
This script fetches the translation files from the transifex service and puts them
into the locales directory.
You will need maintainer access on our transifex project to run this script.
Get your api token from here: https://www.transifex.com/user/settings/api/
and set it in your ENV as TRANSIFEX_API_KEY
e.g.
`export TRANSIFEX_API_KEY=<api key>`
in your shell.
=end
require 'net/http'
require 'json'
require 'tempfile'
require 'fileutils'
HTTP_USER = 'api'
HTTP_PASSWORD = ENV.fetch('TRANSIFEX_API_KEY')
def http(url)
uri = URI(url)
request = Net::HTTP::Get.new(uri)
request.basic_auth HTTP_USER, HTTP_PASSWORD
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = (uri.scheme == "https")
http.request(request)
end
def list_languages()
url = 'https://www.transifex.com/api/2/project/security-without-borders-website/languages/'
response = http(url)
JSON.parse(response.body)
end
def list_resources()
url = 'https://www.transifex.com/api/2/project/security-without-borders-website/resources/'
response = http(url)
JSON.parse(response.body)
end
def get_language_info(language_code)
url = 'https://www.transifex.com/api/2/project/security-without-borders-website/resource/enyml/stats/%s/' % language_code
response = http(url)
JSON.parse(response.body)
end
def download_translation_file(language_code)
tmpfile = Tempfile.new(language_code + '.yml')
url = 'https://www.transifex.com/api/2/project/security-without-borders-website/resource/enyml/translation/%s/?file'% language_code
response = http(url)
tmpfile.write(response.body)
tmpfile.rewind
tmpfile
end
# debug stuff
# puts list_resources
# main
languages = list_languages()
changes = 0
for language in languages
# puts language
info = get_language_info(language["language_code"])
# puts "? %s is %s complete, %s reviewed" % language["language_code"], info["completed"], info["reviewed"]
if info["completed"] != "100%"
# require that everything is reviewed.
puts "- Skipping %s. Translation is not complete." % language["language_code"]
break
end
tmpfile = download_translation_file language["language_code"]
# puts tmpfile.path
shorter_code = language["language_code"] # hoping here that transifex does not inject a naughty string
original_file = 'locales/%s.yml' % shorter_code
if not (File::exists? original_file and FileUtils.compare_file tmpfile, original_file)
changes += 1
puts "+ Translation for %s has been changed." % language["language_code"]
FileUtils.mv tmpfile.path, original_file
end
tmpfile.close
tmpfile.unlink
end
puts "%i locales updated." % changes