-
Notifications
You must be signed in to change notification settings - Fork 479
/
sync-codeorg-in
executable file
·123 lines (106 loc) · 4.46 KB
/
sync-codeorg-in
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
#!/usr/bin/env ruby
# Pulls in all strings that need to be translated. Pulls source
# files from blockly-core, apps, pegasus, and dashboard
# as well as instructions for levelbuilder supported levels and
# collects them to the single source folder i18n/locales/source.
require File.expand_path('../../../pegasus/src/env', __FILE__)
require 'fileutils'
require 'json'
require 'yaml'
require 'tempfile'
require_relative 'i18n_script_utils'
def pull_in_files_for_translation
localize_level_content
run_bash_script "bin/i18n-codeorg/in.sh"
end
def copy_to_yml(label, data, allow_full_length=false)
args = allow_full_length ? {line_width: -1} : {}
File.open("dashboard/config/locales/#{label}.en.yml", "w+") do |f|
data = ({ "en" => { "data" => { label => data }}}).to_yaml(**args)
f.write(data)
end
end
def reformat_quotes
filenames = [
"instructions",
"markdown_instructions",
'failure_message_overrides',
]
filenames.each do |filename|
temp_file = Tempfile.new("temp#{filename}.yml")
File.open("dashboard/config/locales/#{filename}.en.yml", "r") do |f|
f.each_line { |line| temp_file.puts line.gsub("'\"", '"').gsub("\"'", '"').gsub("''", "'")}
end
temp_file.close
FileUtils.mv(temp_file.path, "dashboard/config/locales/#{filename}.en.yml")
end
end
# Pull in various fields for levelbuilder levels from .level files and
# save them to [field_name].en.yml files to be translated. Fields included:
# instructions
# markdown instructions
# failure message override
# authored hints
# callouts
def localize_level_content
level_instructions = Hash.new
level_markdown_instructions = Hash.new
level_failure_message_overrides = Hash.new
level_authored_hints = Hash.new
level_callouts = Hash.new
instruction_pattern = /^\s*"instructions": (".*?"),?\n$/
markdown_instruction_pattern = /^\s*"markdown_instructions": (".*?"),?\n$/
failure_message_override_pattern = /^\s*"failure_message_override": (".*?"),?\n$/
authored_hint_pattern = /^\s*"authored_hints": "(.*?)",?\n$/
callout_pattern = /^\s*"callout_json": "(.*?)",?\n$/
Dir.glob("dashboard/config/scripts/levels/*.level").sort.each do |file|
level = File.basename(file, ".*") + "_instruction"
markdown_level = File.basename(file, ".*") + "_markdown_instruction"
failure_message_override_level = File.basename(file, ".*") + "_failure_message_override"
authored_hint_level = File.basename(file, ".*") + "_authored_hint"
callout_level = File.basename(file, ".*") + "_callout"
File.open(file) do |f|
f.each_line do |line|
# Instructions
instruction_match = line.match instruction_pattern
if instruction_match
level_instructions[level] = instruction_match.captures.first
end
# Markdown Instructions
markdown_instruction_match = line.match markdown_instruction_pattern
if markdown_instruction_match
level_markdown_instructions[markdown_level] = markdown_instruction_match.captures.first
end
# Failure message overrides
failure_message_override_match = line.match failure_message_override_pattern
if failure_message_override_match
level_failure_message_overrides[failure_message_override_level] = failure_message_override_match.captures.first
end
# Authored Hints
authored_hint_match = line.match authored_hint_pattern
if authored_hint_match
hint_json = JSON.load(%Q("#{authored_hint_match.captures.first}"))
level_authored_hints[authored_hint_level] = JSON.parse(hint_json).reduce({}) do |memo, hint|
memo[hint['hint_id']] = hint['hint_markdown'] unless hint['hint_id'].empty?
memo
end
end
# Callouts
callout_match = line.match callout_pattern
next unless callout_match
callout_json = JSON.load(%Q("#{callout_match.captures.first}"))
level_callouts[callout_level] = JSON.parse(callout_json).reduce({}) do |memo, callout|
memo[callout['localization_key']] = callout['callout_text'] unless callout['localization_key'].empty?
memo
end
end
end
end
copy_to_yml("instructions", level_instructions)
copy_to_yml("markdown_instructions", level_markdown_instructions)
copy_to_yml("failure_message_overrides", level_failure_message_overrides)
copy_to_yml("authored_hints", level_authored_hints, true)
copy_to_yml("callouts", level_callouts, true)
reformat_quotes
end
pull_in_files_for_translation