-
Notifications
You must be signed in to change notification settings - Fork 33
/
import_language.rb
157 lines (135 loc) · 5.11 KB
/
import_language.rb
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
148
149
150
151
152
153
154
155
156
157
# Run as `ruby import_language.rb es path/to/site.es.json`
require 'json'
require 'fileutils'
require 'ruby-lokalise-api'
require 'open-uri'
require 'zip'
require 'metadown'
require 'set'
require 'yaml'
require_relative '_plugins/common'
def language_dir(lang)
"_sections/*/#{lang}"
end
LOKALISE_TOKEN = ARGV[0]
SINGLE_LANG = ARGV[1]
LOKALISE_PROJECT_ID = "423383895e6b8c4b081a89.98184174"
SOURCE_LANG = "en"
SOURCE_DIR = language_dir(SOURCE_LANG)
SECTIONS_TO_FILES = {}
Dir["#{SOURCE_DIR}/**/*.md"].sort!.each { |filename|
SECTIONS_TO_FILES[key_from_filename(filename)] = filename
}
TOP_LEVEL_PAGES = {}
Dir["*-en.md"].sort!.each { |filename|
TOP_LEVEL_PAGES[key_from_top_level_file(filename)] = filename
}
STRINGS = YAML.load(File.open("_data/#{SOURCE_LANG}/strings.yml", 'r:UTF-8') { |f| f.read })
CONFIG_YML = File.open("_config.yml", 'r:UTF-8') { |f| f.read }
SUPPORTED_LANGS = YAML.load(CONFIG_YML)['languages']
NEW_LANGS = []
def should_have_sci_review_notice(lang, section, translated_file, no_review_keys: [])
return false if translated_file !~ /\/(act_and_prepare|COVID-19)\//
return false if !no_review_keys.include? section
# Exclude /00-blah.md headers
return false if File.basename(translated_file) =~ /^00-/
return true
end
def generate_content(lang, translations, no_review_keys: [])
FileUtils.rm_r(Dir[language_dir(lang)], force: true)
SECTIONS_TO_FILES.each do |section, source_file|
translated_file = source_file.sub("/en/", "/#{lang}/")
translated_dir = File.dirname(translated_file)
FileUtils.mkdir_p(translated_dir)
File.open(translated_file, "w:UTF-8") { |file|
content = translations[section]
if should_have_sci_review_notice(lang, section, translated_file, no_review_keys: no_review_keys)
puts "Adding scientific-review disclaimer to #{lang} #{section}"
# Find the last header line and insert after it.
lines = content.lines
last_hdr = lines.rindex{|e| e =~ /^#/}
if last_hdr == nil
puts "Missing header lines in #{translated_file}"
last_hdr = 0
else
last_hdr = last_hdr + 1
end
lines.insert(last_hdr, "\n{% include pending-sci-review.html %}\n")
content = lines.join("")
end
file.puts content
}
end
TOP_LEVEL_PAGES.each do |page, source_file|
source_content = File.open(source_file, 'r:UTF-8') { |f| f.read }
metadata = Metadown.render(source_content).metadata
metadata["lang"] = lang
metadata["title"] = translations["#{page}-title"] if translations["#{page}-title"]
translated_file = source_file.sub(/-en.md$/, "-#{lang}.md")
File.open(translated_file, "w:UTF-8") { |file|
file.puts metadata.to_yaml
file.puts "---\n"
file.puts metadata["translate_content"] == false ? content_without_frontmatter(source_content) : translations[page]
}
end
translated_strings_dir = "_data/#{lang}"
translated_strings = Hash[STRINGS.map { |key, value| [key, translations[key]] }]
FileUtils.mkdir_p(translated_strings_dir)
File.open("#{translated_strings_dir}/strings.yml", 'w:UTF-8') {|f| f.puts translated_strings.to_yaml }
NEW_LANGS << lang unless SUPPORTED_LANGS.include? lang
end
def fetch_json_from_lokalise(lang: nil, filter_data: ['translated'])
client = Lokalise.client LOKALISE_TOKEN
params = {
format: "json",
filter_filename: ["pasted.json"],
replace_breaks: false,
placeholder_format: :icu,
filter_data: filter_data
}
if lang != nil
params['filter_langs'] = [lang]
end
resp = client.download_files(LOKALISE_PROJECT_ID, params)
result = {}
Zip::File.open_buffer(open(resp["bundle_url"])) { |zip|
zip.each do |entry|
next unless entry.name.end_with?("pasted.json")
file_lang = entry.name.split("/")[0]
result[file_lang] = JSON.parse(entry.get_input_stream.read)
end
}
result
end
def keys_without_reviews(everything, reviewed)
everything.keys.to_set - reviewed.keys.to_set
end
puts "Fetching translations from Lokalise"
all_translations = fetch_json_from_lokalise(lang: SINGLE_LANG)
all_reviews = Hash.new
begin
all_reviews = fetch_json_from_lokalise(lang: SINGLE_LANG, filter_data: ['reviewed'])
rescue Lokalise::Error::NotAcceptable => e
puts "No reviewed keys found for given language"
end
puts "Translations fetched: #{all_translations.keys}"
puts "Writing translation files"
all_translations.each {|lang, json|
File.open("_translations/#{lang}.json", "w:UTF-8") { |f| f.write(JSON.pretty_generate(json)) }
}
all_translations.each {|lang, translations|
reviews = all_reviews[lang]
reviews = Hash.new if reviews.nil?
not_reviewed = keys_without_reviews(translations, reviews)
puts "Generating content files for language #{lang}"
generate_content(lang, translations, no_review_keys: not_reviewed)
}
unless NEW_LANGS.empty?
puts "Languages to add: #{NEW_LANGS}"
languages_regex = /^languages: (\[.*\])$/
if CONFIG_YML =~ languages_regex
new_languages_line = "languages: #{(SUPPORTED_LANGS + NEW_LANGS).to_s}"
new_config_yml = CONFIG_YML.sub languages_regex, new_languages_line
File.open('_config.yml', 'w:UTF-8') { |f| f.puts new_config_yml }
end
end