neeto-translate-cli 0.3.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2843b446edd2c585c87d15b48cc8b950e80dab86bac2d6eaf928478ceb4fd8ba
4
- data.tar.gz: 0dd014fabdbb035100109d4abf5a6d9f27412def945e5f40780839bb43672581
3
+ metadata.gz: 2092a3c08b7cb6d52526218c211f8f3a13fa3a0c69c4c378e1e2b8d3ef39b9b5
4
+ data.tar.gz: 0c9f7ec5827a257d696bab24e85f99013fb4562108504f93d85ac0196e376bb3
5
5
  SHA512:
6
- metadata.gz: 6b6f0aa12230ed0e04f997051c5275a866745f0407c4dd940fbc76919a2fe36401bfae67d65fbaffebd6ccfd9368fbb9b9ed10985ea871873e57de2f2552b347
7
- data.tar.gz: eda8a84c5c70b9d5d690f1aedf53e939bbdc51bb81530c2f389b7bfa04354a64359a02916cb727de98e0a298beadf58fa5f6ac051f6d16eacde553e40131480e
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
@@ -9,6 +9,7 @@ module NeetoTranslateCli
9
9
  # Same plural rule as Github::CreatePullRequestService on the server.
10
10
  PLURAL_FORM_REGEX = /\A(?<base>.+)(?<separator>[._])(?:zero|one|two|few|many|other)\z/
11
11
  PLURAL_BASE_FORMS = %w[one other].freeze
12
+ PLURAL_SEPARATORS = %w[_ .].freeze
12
13
 
13
14
  attr_reader :options, :updated_keys
14
15
 
@@ -117,7 +118,9 @@ module NeetoTranslateCli
117
118
  translations = load_translations(type, lang)
118
119
  next unless translations
119
120
 
120
- removed[lang] = (flatten_hash(translations).keys - base_keys).reject { |key| plural_form_of?(key, base_keys) }
121
+ removed[lang] = (flatten_hash(translations).keys - base_keys).reject do |key|
122
+ plural_form_of?(key, base_keys) || plural_base_of?(key, base_keys)
123
+ end
121
124
  end
122
125
  end
123
126
 
@@ -140,6 +143,14 @@ module NeetoTranslateCli
140
143
  PLURAL_BASE_FORMS.any? { |form| base_keys.include?("#{match[:base]}#{match[:separator]}#{form}") }
141
144
  end
142
145
 
146
+ # Languages like ar and ru fall back to the base key (e.g. `item`) for the plural forms they don't have,
147
+ # so it is not treated as removed when en has its plural forms (`item_one`, `items.other`).
148
+ def plural_base_of?(key, base_keys)
149
+ PLURAL_BASE_FORMS.product(PLURAL_SEPARATORS).any? do |form, separator|
150
+ base_keys.include?("#{key}#{separator}#{form}")
151
+ end
152
+ end
153
+
143
154
  def load_json(file_path)
144
155
  return {} unless File.file?(file_path)
145
156
 
@@ -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,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module NeetoTranslateCli
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
@@ -0,0 +1,4 @@
1
+ nl:
2
+ meeting: afspraak
3
+ meetings: afspraken
4
+ scheduling link: planningslink
@@ -1,4 +1,4 @@
1
- require "test_helper"
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
 
@@ -191,6 +191,27 @@ class NeetoTranslateCli::PayloadBuilderTest < Minitest::Test
191
191
  end
192
192
  end
193
193
 
194
+ def test_removed_keys_keep_base_keys_whose_plural_forms_are_in_en
195
+ with_locale_files do |payload_builder|
196
+ payload = payload_builder.process!
197
+
198
+ refute_includes payload.dig(:frontend, :removed_keys, "es"), "common.item"
199
+ end
200
+ end
201
+
202
+ def test_removed_keys_keep_base_keys_whose_nested_plural_forms_are_in_en
203
+ with_locale_files do |payload_builder|
204
+ frontend, backend = payload_builder.options.values_at(:frontend, :backend)
205
+ FileUtils.cp(File.join(frontend, "en.json"), File.join(frontend, "pl.json"))
206
+ pl_yml = { "pl" => { "common" => { "ok" => "OK", "items" => "Elementy" } } }
207
+ File.write(File.join(backend, "pl.yml"), pl_yml.to_yaml)
208
+
209
+ payload = payload_builder.process!
210
+
211
+ refute_includes payload.dig(:backend, :removed_keys, "pl"), "common.items"
212
+ end
213
+ end
214
+
194
215
  def test_no_new_changes_is_false_when_keys_were_only_removed
195
216
  with_locale_files do |payload_builder|
196
217
  payload = payload_builder.process!
@@ -212,7 +233,7 @@ class NeetoTranslateCli::PayloadBuilderTest < Minitest::Test
212
233
  en_json = { "common" => { "ok" => "OK", "item_one" => "Item", "item_other" => "Items" } }
213
234
  es_json = {
214
235
  "common" => { "ok" => "Aceptar", "item_one" => "Elemento", "item_other" => "Elementos",
215
- "item_many" => "Elementos", "deleted" => "Borrado" },
236
+ "item_many" => "Elementos", "item" => "Elemento", "deleted" => "Borrado" },
216
237
  "dashboard" => { "stats" => { "removed" => "Eliminado" } }
217
238
  }
218
239
  File.write(File.join(frontend, "en.json"), JSON.generate(en_json))
@@ -1,4 +1,5 @@
1
- require "test_helper"
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,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neeto-translate-cli
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neeraj Singh
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2026-11-15 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: faraday
@@ -51,6 +50,7 @@ files:
51
50
  - test/fixtures/frontend/en.json
52
51
  - test/fixtures/frontend/es.json
53
52
  - test/fixtures/frontend/jp.json
53
+ - test/fixtures/glossary.yml
54
54
  - test/neeto_translate_cli/api_test.rb
55
55
  - test/neeto_translate_cli/cli_test.rb
56
56
  - test/neeto_translate_cli/integration_test.rb
@@ -63,7 +63,6 @@ licenses: []
63
63
  metadata:
64
64
  homepage_uri: https://github.com/neetozone/neeto-translate
65
65
  source_code_uri: https://github.com/neetozone/neeto-translate
66
- post_install_message:
67
66
  rdoc_options: []
68
67
  require_paths:
69
68
  - lib
@@ -78,8 +77,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
78
77
  - !ruby/object:Gem::Version
79
78
  version: '0'
80
79
  requirements: []
81
- rubygems_version: 3.5.16
82
- signing_key:
80
+ rubygems_version: 4.0.20
83
81
  specification_version: 4
84
82
  summary: neeto-translate CLI tool to extract translation files and make requests to
85
83
  the neeto-translate API