neeto-translate-cli 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +25 -0
- data/lib/neeto_translate_cli/cli.rb +1 -0
- data/lib/neeto_translate_cli/translator.rb +22 -0
- data/lib/neeto_translate_cli/version.rb +1 -1
- data/test/fixtures/glossary.yml +4 -0
- data/test/neeto_translate_cli/cli_test.rb +15 -1
- data/test/neeto_translate_cli/translator_test.rb +168 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2092a3c08b7cb6d52526218c211f8f3a13fa3a0c69c4c378e1e2b8d3ef39b9b5
|
|
4
|
+
data.tar.gz: 0c9f7ec5827a257d696bab24e85f99013fb4562108504f93d85ac0196e376bb3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 23cf2df659446c19cd152dd0d3abff1958669d5f8d2b9620e1a3700382af94caabf0c3862576317887c4d6ffb9b2cfabe43abb84b6a0574aa62e557fda144c25
|
|
7
|
+
data.tar.gz: 0b7da39e77e610a366fdca6ab08bfa83e47b8f099d5747bf30fc78d8084edabc0b863ca44a77617f22cd6479539bd9606c21ea9adf191c6fbb9d4bba47927bf7
|
data/README.md
CHANGED
|
@@ -51,6 +51,7 @@ neeto-translate-cli [OPTIONS]
|
|
|
51
51
|
- `--backend PATH` - Path to backend locales (default: `config/locales`)
|
|
52
52
|
- `--defaults LANGUAGES` - Comma-separated list of languages to translate (default: `en,fr,de,es`)
|
|
53
53
|
- `--repo NAME` - GitHub repository name (e.g., `neetozone/neeto-cal-web`)
|
|
54
|
+
- `--glossary PATH` - Path to a YAML file with fixed translations of English terms for each language (see [Glossary](#glossary))
|
|
54
55
|
|
|
55
56
|
### Examples
|
|
56
57
|
|
|
@@ -76,6 +77,29 @@ neeto-translate-cli \
|
|
|
76
77
|
--repo "neetozone/neeto-cal-web"
|
|
77
78
|
```
|
|
78
79
|
|
|
80
|
+
**With a glossary:**
|
|
81
|
+
|
|
82
|
+
```bash
|
|
83
|
+
neeto-translate-cli --glossary config/translation_glossary.yml
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Glossary
|
|
87
|
+
|
|
88
|
+
A glossary makes the translator always use a given word for an English term in a language. It is a YAML file keyed by language code, mapping each English term to its translation:
|
|
89
|
+
|
|
90
|
+
```yaml
|
|
91
|
+
nl:
|
|
92
|
+
meeting: afspraak
|
|
93
|
+
meetings: afspraken
|
|
94
|
+
scheduling link: planningslink
|
|
95
|
+
video meeting: videovergadering
|
|
96
|
+
zoom meeting: Zoom-vergadering
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
The terms are added to the translation prompt for that language only. They also apply inside longer phrases, so "meeting location" becomes "locatie van de afspraak" in Dutch. When several terms match, the longest one wins, so list a more specific term to keep a different word in that context: above, "Join the video meeting" uses "videovergadering", not "afspraak".
|
|
100
|
+
|
|
101
|
+
Language codes, terms and translations must all be non-empty text. Quote words YAML reads as booleans or numbers, such as the Norwegian code `"no"`.
|
|
102
|
+
|
|
79
103
|
### Environment Variables
|
|
80
104
|
|
|
81
105
|
The following environment variables can be configured:
|
|
@@ -94,6 +118,7 @@ The following environment variables can be configured:
|
|
|
94
118
|
- Missing translation keys for each language
|
|
95
119
|
- Keys removed from the English files, for each language
|
|
96
120
|
- Updated keys that need re-translation
|
|
121
|
+
- Glossary terms, when `--glossary` is given
|
|
97
122
|
- Repository metadata
|
|
98
123
|
|
|
99
124
|
## Development
|
|
@@ -45,6 +45,7 @@ module NeetoTranslateCli
|
|
|
45
45
|
options[:repo] = path
|
|
46
46
|
end
|
|
47
47
|
opts.on("--branch NAME", "Base branch to which the PR should be created") { |branch| options[:branch] = branch}
|
|
48
|
+
opts.on("--glossary PATH", "Path to a YAML file of per-language term translations") { |path| options[:glossary] = path }
|
|
48
49
|
|
|
49
50
|
# This arg is explicitly added to handle translations in neeto-website and bigbinary-website.
|
|
50
51
|
# Usage: --strategy next-i18next
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require "yaml"
|
|
4
|
+
|
|
3
5
|
require_relative "api"
|
|
4
6
|
require_relative "payload_builder"
|
|
5
7
|
require_relative "next_i18next/payload_builder"
|
|
@@ -28,9 +30,29 @@ module NeetoTranslateCli
|
|
|
28
30
|
if payload_builder.no_new_changes?(payload)
|
|
29
31
|
puts "Nothing to see here. You are all good."
|
|
30
32
|
else
|
|
33
|
+
payload[:glossary] = glossary if options[:glossary]
|
|
31
34
|
response = api.translate! payload
|
|
32
35
|
puts response.body
|
|
33
36
|
end
|
|
34
37
|
end
|
|
38
|
+
|
|
39
|
+
def glossary
|
|
40
|
+
data = YAML.load_file(options[:glossary]) || {}
|
|
41
|
+
unless data.is_a?(Hash) && data.all? { |language, terms| language.is_a?(String) && valid_glossary_terms?(terms) }
|
|
42
|
+
abort_invalid_glossary("expected language codes mapping English terms to translations, all as non-empty text")
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
data
|
|
46
|
+
rescue Errno::ENOENT, Psych::SyntaxError => error
|
|
47
|
+
abort_invalid_glossary(error.message)
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def valid_glossary_terms?(terms)
|
|
51
|
+
terms.is_a?(Hash) && terms.all? { |pair| pair.all? { |text| text.is_a?(String) && !text.strip.empty? } }
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def abort_invalid_glossary(reason)
|
|
55
|
+
abort "Invalid glossary file #{options[:glossary]}: #{reason}"
|
|
56
|
+
end
|
|
35
57
|
end
|
|
36
58
|
end
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
1
|
+
require_relative "../test_helper"
|
|
2
2
|
|
|
3
3
|
class NeetoTranslateCli::CLITest < Minitest::Test
|
|
4
4
|
def setup
|
|
@@ -65,6 +65,20 @@ class NeetoTranslateCli::CLITest < Minitest::Test
|
|
|
65
65
|
assert_equal "neetozone/test-repo", options[:repo]
|
|
66
66
|
end
|
|
67
67
|
|
|
68
|
+
def test_start_with_glossary_path
|
|
69
|
+
cli = NeetoTranslateCli::CLI.new([])
|
|
70
|
+
options = cli.send(:parse_options, ["--glossary", "config/translation_glossary.yml"])
|
|
71
|
+
|
|
72
|
+
assert_equal "config/translation_glossary.yml", options[:glossary]
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def test_start_without_glossary_path
|
|
76
|
+
cli = NeetoTranslateCli::CLI.new([])
|
|
77
|
+
options = cli.send(:parse_options, [])
|
|
78
|
+
|
|
79
|
+
assert_nil options[:glossary]
|
|
80
|
+
end
|
|
81
|
+
|
|
68
82
|
def test_default_languages_list_is_well_formed
|
|
69
83
|
languages = ::NeetoTranslateCli::CLI::DEFAULT_LANGUAGES.split(",")
|
|
70
84
|
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
require "
|
|
1
|
+
require "tempfile"
|
|
2
|
+
require_relative "../test_helper"
|
|
2
3
|
|
|
3
4
|
class NeetoTranslateCli::TranslatorTest < Minitest::Test
|
|
4
5
|
def setup
|
|
@@ -105,4 +106,170 @@ class NeetoTranslateCli::TranslatorTest < Minitest::Test
|
|
|
105
106
|
assert_includes output.join, "Translation job submitted successfully"
|
|
106
107
|
mock_api.verify
|
|
107
108
|
end
|
|
109
|
+
|
|
110
|
+
def test_process_sends_glossary_from_file_in_payload
|
|
111
|
+
mock_payload_builder = Minitest::Mock.new
|
|
112
|
+
mock_payload = { frontend: { missing_keys: { "nl" => ["key1"] } }, backend: { missing_keys: { "nl" => [] } } }
|
|
113
|
+
mock_payload_builder.expect :process!, mock_payload
|
|
114
|
+
mock_payload_builder.expect :no_new_changes?, false, [mock_payload]
|
|
115
|
+
|
|
116
|
+
mock_response = Minitest::Mock.new
|
|
117
|
+
mock_response.expect :body, "Translation job submitted successfully"
|
|
118
|
+
|
|
119
|
+
sent_payload = nil
|
|
120
|
+
mock_api = Object.new
|
|
121
|
+
mock_api.define_singleton_method(:translate!) do |payload|
|
|
122
|
+
sent_payload = payload
|
|
123
|
+
mock_response
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
capture_io do
|
|
127
|
+
NeetoTranslateCli::PayloadBuilder.stub :new, mock_payload_builder do
|
|
128
|
+
NeetoTranslateCli::Api.stub :new, mock_api do
|
|
129
|
+
translator = NeetoTranslateCli::Translator.new(@options.merge(glossary: File.expand_path("../fixtures/glossary.yml", __dir__)))
|
|
130
|
+
translator.process!
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
expected_glossary = {
|
|
136
|
+
"nl" => { "meeting" => "afspraak", "meetings" => "afspraken", "scheduling link" => "planningslink" }
|
|
137
|
+
}
|
|
138
|
+
assert_equal expected_glossary, sent_payload[:glossary]
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def test_process_without_glossary_option_does_not_send_glossary
|
|
142
|
+
mock_payload_builder = Minitest::Mock.new
|
|
143
|
+
mock_payload = { frontend: { missing_keys: { "nl" => ["key1"] } }, backend: { missing_keys: { "nl" => [] } } }
|
|
144
|
+
mock_payload_builder.expect :process!, mock_payload
|
|
145
|
+
mock_payload_builder.expect :no_new_changes?, false, [mock_payload]
|
|
146
|
+
|
|
147
|
+
mock_response = Minitest::Mock.new
|
|
148
|
+
mock_response.expect :body, "Translation job submitted successfully"
|
|
149
|
+
|
|
150
|
+
sent_payload = nil
|
|
151
|
+
mock_api = Object.new
|
|
152
|
+
mock_api.define_singleton_method(:translate!) do |payload|
|
|
153
|
+
sent_payload = payload
|
|
154
|
+
mock_response
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
capture_io do
|
|
158
|
+
NeetoTranslateCli::PayloadBuilder.stub :new, mock_payload_builder do
|
|
159
|
+
NeetoTranslateCli::Api.stub :new, mock_api do
|
|
160
|
+
NeetoTranslateCli::Translator.new(@options).process!
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
refute sent_payload.key?(:glossary)
|
|
166
|
+
end
|
|
167
|
+
def test_process_sends_empty_glossary_for_empty_glossary_file
|
|
168
|
+
Tempfile.create(["glossary", ".yml"]) do |file|
|
|
169
|
+
assert_equal({}, sent_payload_with_glossary(file.path)[:glossary])
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def test_process_aborts_when_glossary_terms_are_not_a_map
|
|
174
|
+
Tempfile.create(["glossary", ".yml"]) do |file|
|
|
175
|
+
file.write("nl:\n - meeting\n")
|
|
176
|
+
file.flush
|
|
177
|
+
|
|
178
|
+
assert_aborts_with_invalid_glossary(file.path)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def test_process_aborts_when_glossary_is_not_a_map
|
|
183
|
+
Tempfile.create(["glossary", ".yml"]) do |file|
|
|
184
|
+
file.write("- nl\n")
|
|
185
|
+
file.flush
|
|
186
|
+
|
|
187
|
+
assert_aborts_with_invalid_glossary(file.path)
|
|
188
|
+
end
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def test_process_aborts_when_glossary_translations_are_not_strings
|
|
192
|
+
[
|
|
193
|
+
"nl:\n meeting: [afspraak, afspraken]\n",
|
|
194
|
+
"nl:\n meeting:\n singular: afspraak\n",
|
|
195
|
+
"nl:\n meeting:\n",
|
|
196
|
+
"nl:\n meeting: \"\"\n",
|
|
197
|
+
"nl:\n meeting: 1\n",
|
|
198
|
+
"nl:\n \"\": afspraak\n",
|
|
199
|
+
"no:\n meeting: møte\n"
|
|
200
|
+
].each do |content|
|
|
201
|
+
Tempfile.create(["glossary", ".yml"]) do |file|
|
|
202
|
+
file.write(content)
|
|
203
|
+
file.flush
|
|
204
|
+
|
|
205
|
+
assert_aborts_with_invalid_glossary(file.path)
|
|
206
|
+
end
|
|
207
|
+
end
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
def test_process_aborts_when_glossary_has_invalid_yaml
|
|
211
|
+
Tempfile.create(["glossary", ".yml"]) do |file|
|
|
212
|
+
file.write("nl: [meeting\n")
|
|
213
|
+
file.flush
|
|
214
|
+
|
|
215
|
+
assert_aborts_with_invalid_glossary(file.path)
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
def test_process_aborts_when_glossary_file_is_missing
|
|
220
|
+
assert_aborts_with_invalid_glossary("test/fixtures/missing_glossary.yml")
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def test_process_does_not_load_glossary_when_there_are_no_changes
|
|
224
|
+
mock_payload_builder = Minitest::Mock.new
|
|
225
|
+
mock_payload = { frontend: { missing_keys: { "nl" => [] } }, backend: { missing_keys: { "nl" => [] } } }
|
|
226
|
+
mock_payload_builder.expect :process!, mock_payload
|
|
227
|
+
mock_payload_builder.expect :no_new_changes?, true, [mock_payload]
|
|
228
|
+
|
|
229
|
+
output = capture_io do
|
|
230
|
+
NeetoTranslateCli::PayloadBuilder.stub :new, mock_payload_builder do
|
|
231
|
+
NeetoTranslateCli::Api.stub :new, Object.new do
|
|
232
|
+
NeetoTranslateCli::Translator.new(@options.merge(glossary: "test/fixtures/missing_glossary.yml")).process!
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
assert_includes output.join, "Nothing to see here. You are all good."
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
private
|
|
241
|
+
|
|
242
|
+
def sent_payload_with_glossary(glossary_path)
|
|
243
|
+
mock_payload_builder = Minitest::Mock.new
|
|
244
|
+
mock_payload = { frontend: { missing_keys: { "nl" => ["key1"] } }, backend: { missing_keys: { "nl" => [] } } }
|
|
245
|
+
mock_payload_builder.expect :process!, mock_payload
|
|
246
|
+
mock_payload_builder.expect :no_new_changes?, false, [mock_payload]
|
|
247
|
+
|
|
248
|
+
mock_response = Minitest::Mock.new
|
|
249
|
+
mock_response.expect :body, "Translation job submitted successfully"
|
|
250
|
+
|
|
251
|
+
sent_payload = nil
|
|
252
|
+
mock_api = Object.new
|
|
253
|
+
mock_api.define_singleton_method(:translate!) do |payload|
|
|
254
|
+
sent_payload = payload
|
|
255
|
+
mock_response
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
capture_io do
|
|
259
|
+
NeetoTranslateCli::PayloadBuilder.stub :new, mock_payload_builder do
|
|
260
|
+
NeetoTranslateCli::Api.stub :new, mock_api do
|
|
261
|
+
NeetoTranslateCli::Translator.new(@options.merge(glossary: glossary_path)).process!
|
|
262
|
+
end
|
|
263
|
+
end
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
sent_payload
|
|
267
|
+
end
|
|
268
|
+
|
|
269
|
+
def assert_aborts_with_invalid_glossary(glossary_path)
|
|
270
|
+
error = assert_raises(SystemExit) { sent_payload_with_glossary(glossary_path) }
|
|
271
|
+
|
|
272
|
+
refute error.success?
|
|
273
|
+
assert_includes error.message, "Invalid glossary file #{glossary_path}"
|
|
274
|
+
end
|
|
108
275
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: neeto-translate-cli
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Neeraj Singh
|
|
@@ -50,6 +50,7 @@ files:
|
|
|
50
50
|
- test/fixtures/frontend/en.json
|
|
51
51
|
- test/fixtures/frontend/es.json
|
|
52
52
|
- test/fixtures/frontend/jp.json
|
|
53
|
+
- test/fixtures/glossary.yml
|
|
53
54
|
- test/neeto_translate_cli/api_test.rb
|
|
54
55
|
- test/neeto_translate_cli/cli_test.rb
|
|
55
56
|
- test/neeto_translate_cli/integration_test.rb
|