@drone1/alt 1.0.3 → 1.1.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.
Files changed (83) hide show
  1. package/README.md +61 -45
  2. package/{localization/config.json → alt.config.json} +3 -1
  3. package/localization/.localization.cache.json +554 -140
  4. package/localization/aa.json +4 -1
  5. package/localization/af.json +4 -1
  6. package/localization/agq.json +4 -1
  7. package/localization/ak.json +4 -1
  8. package/localization/am.json +4 -1
  9. package/localization/ar.json +4 -1
  10. package/localization/as.json +4 -1
  11. package/localization/asa.json +4 -1
  12. package/localization/ast.json +4 -1
  13. package/localization/az.json +4 -1
  14. package/localization/ba.json +4 -1
  15. package/localization/bas.json +4 -1
  16. package/localization/be.json +4 -1
  17. package/localization/bem.json +4 -1
  18. package/localization/bez.json +4 -1
  19. package/localization/bg.json +4 -1
  20. package/localization/bm.json +4 -1
  21. package/localization/bn.json +4 -1
  22. package/localization/bo.json +4 -1
  23. package/localization/br.json +4 -1
  24. package/localization/brx.json +4 -1
  25. package/localization/bs.json +4 -1
  26. package/localization/byn.json +4 -1
  27. package/localization/ca.json +4 -1
  28. package/localization/ccp.json +4 -1
  29. package/localization/cd-RU.json +4 -1
  30. package/localization/ceb.json +4 -1
  31. package/localization/cgg.json +4 -1
  32. package/localization/chr.json +4 -1
  33. package/localization/co.json +4 -1
  34. package/localization/cs.json +4 -1
  35. package/localization/cu-RU.json +4 -1
  36. package/localization/da.json +4 -1
  37. package/localization/de-AT.json +4 -1
  38. package/localization/de-CH.json +4 -1
  39. package/localization/de-DE.json +4 -1
  40. package/localization/dua.json +4 -1
  41. package/localization/dv.json +4 -1
  42. package/localization/dz.json +4 -1
  43. package/localization/ebu.json +4 -1
  44. package/localization/en.json +4 -1
  45. package/localization/es-ES.json +4 -1
  46. package/localization/es-MX.json +4 -1
  47. package/localization/et.json +4 -1
  48. package/localization/eu.json +4 -1
  49. package/localization/fr-CA.json +4 -1
  50. package/localization/fr-CH.json +4 -1
  51. package/localization/fr-FR.json +4 -1
  52. package/localization/gsw.json +4 -1
  53. package/localization/hi.json +4 -1
  54. package/localization/hr.json +4 -1
  55. package/localization/hy.json +4 -1
  56. package/localization/ja.json +4 -1
  57. package/localization/km.json +4 -1
  58. package/localization/ksf.json +4 -1
  59. package/localization/ku.json +4 -1
  60. package/localization/kw.json +4 -1
  61. package/localization/my.json +4 -1
  62. package/localization/nl.json +4 -1
  63. package/localization/prs.json +4 -1
  64. package/localization/reference.js +3 -0
  65. package/localization/ru.json +4 -1
  66. package/localization/sq.json +4 -1
  67. package/localization/swc.json +4 -1
  68. package/localization/th.json +4 -1
  69. package/localization/tzm-Latn-.json +4 -1
  70. package/localization/uk.json +4 -1
  71. package/localization/vi.json +4 -1
  72. package/localization/zh-Hans.json +4 -1
  73. package/localization/zh-Hant.json +4 -1
  74. package/package.json +2 -2
  75. package/src/commands/translate.js +78 -17
  76. package/src/lib/config.js +30 -14
  77. package/src/lib/consts.js +1 -4
  78. package/src/lib/io.js +5 -0
  79. package/src/lib/reference-loader.js +1 -1
  80. package/src/lib/utils.js +1 -1
  81. package/src/main.mjs +9 -9
  82. package/test/main-cli.test.js +0 -18
  83. package/test/translate-command.test.js +6 -2
package/README.md CHANGED
@@ -23,19 +23,21 @@
23
23
  * [Display language](#display-language)
24
24
  * [Usage](#usage)
25
25
  * [Examples](#examples)
26
- * [Example I](#example-i)
26
+ * [Example I: ALT's localized display strings](#example-i-alts-localized-display-strings)
27
27
  * [Example II](#example-ii)
28
28
  * [Example III](#example-iii)
29
- * [Example: ALT's localized display strings](#example-alts-localized-display-strings)
29
+ * [Example III](#example-iii-1)
30
30
  * [Formatting](#formatting)
31
31
  * [Translation rules](#translation-rules)
32
32
  * [Additional notes](#additional-notes)
33
+ * [Yes, you should commit your .localization.cache.json](#yes-you-should-commit-your-localizationcachejson)
33
34
  * [Delayed vs. realtime writes](#delayed-vs-realtime-writes)
34
35
  * [CI](#ci)
35
- * [PR](#pr)
36
+ * [Contributing](#contributing)
37
+ * [Feedback](#feedback)
36
38
 
37
39
  <!-- Created by https://github.com/ekalinin/github-markdown-toc -->
38
- <!-- Added by: jonl, at: Wed Apr 16 11:56:33 AM CEST 2025 -->
40
+ <!-- Added by: jonl, at: Fri Apr 18 09:55:42 PM CEST 2025 -->
39
41
 
40
42
  <!--te-->
41
43
 
@@ -100,12 +102,15 @@ Sample output:
100
102
  Note that output files can be lower-cased if you pass the ``--normalize-output-filenames`` option, so `fr-FR` translations would write to `fr-fr.json`
101
103
 
102
104
  ## Config file
103
- [_optional_] For convenience, a config file is supported. You can use a config file in conjunction with command-line arguments. Command-line arguments override config file values.
105
+ [_optional_] For convenience, a config file is supported. You can use a config file in conjunction with command-line arguments.
104
106
 
105
- By default, ALT will search the output directory for `config.json`, but you can specify a path explicitly using
106
- `--config-file`.
107
+ Command-line arguments override config file values.
107
108
 
108
- Here's an example config:
109
+ If no explicit config file is specified via `--config-file`, ALT will search for a file with the name `alt.config.json` in the current working directory.
110
+
111
+ If no config is loaded, you'll need to specify `--reference-file` and `--provider`, otherwise ALT will fail.
112
+
113
+ Here's an example `alt.config.json` file you might keep in your project's root directory:
109
114
 
110
115
  ```
111
116
  {
@@ -115,13 +120,17 @@ Here's an example config:
115
120
  "lookForContextData": true,
116
121
  "contextPrefix": "_context:",
117
122
  "contextSuffix": "",
123
+ "outputDir": "localization",
124
+ "referenceFile": "localization/reference.js",
118
125
  "targetLanguages": [
119
126
  "es-MX", "zh-SG"
120
127
  ]
121
128
  }
122
129
  ```
123
130
 
124
- Any of the above settings can be specified using command-line arguments (`--app-context-message`, `--reference-language`, `--provider`, `--target-languages`). Command-line arguments take precedence.
131
+ Since in most cases these arguments will not change often, a config file is recommended.
132
+
133
+ Any of the above settings can be specified using command-line arguments, but note the mapping from _camelCase_ to _kebab-case_ (`appContextMessage` => `--app-context-message`, `referenceLanguage` => `--reference-language`, etc.).
125
134
 
126
135
  ## Adding context
127
136
  Sometimes a string isn't enough to give context to the AI, and as a result, it may give an undesirable translation. ALT allows you to specify additional context for this reason.
@@ -185,15 +194,15 @@ Environment variables:
185
194
  Usage: alt translate [options]
186
195
 
187
196
  Options:
188
- -r, --reference-file <path> Path to reference file of source strings to be translated. This file can be in .js, .mjs, .json, or .jsonc formats and is presumed to be in the reference language specified by --reference-language
189
- -c, --config-file <path> Path to config file; defaults to <output dir>/config.json
197
+ -c, --config-file <path> Path to config file; defaults to <output dir>/alt.config.json
198
+ -r, --reference-file <path> Path to reference file of source strings to be translated. This file can be in .js, .mjs, .json, or .jsonc formats and is presumed to be in the reference language specified by --reference-language; overrides any 'referenceFile' config setting
199
+ -o, --output-dir <path> Output directory for localized files; overrides any 'outputDir' config setting
190
200
  -rl, --reference-language <language> The reference file's language; overrides any 'referenceLanguage' config setting
191
- -o, --output-dir <path> Output directory for localized files
192
201
  -tl, --target-languages <list> Comma-separated list of language codes; overrides any 'targetLanguages' config setting
193
- -k, --keys <list> Comma-separated list of keys to process
194
- -R, --reference-exported-var-name <var name> For .js or .mjs reference files, this will be the exported variable, e.g. for 'export default = {...}' you'd use 'default' here, or 'data' for 'export const data = { ... }'. For .json or .jsonc reference files, this value is ignored. (default: "default")
195
- -m, --app-context-message <message> Description of your app to give context. Passed with each translation request; overrides any 'appContextMessage' config setting
196
- -f, --force Force regeneration of all translations (default: false)
202
+ -k, --keys <list> Comma-separated list of keys to process; if none are processed, all keys in the reference file will be processed
203
+ -R, --reference-exported-var-name <var name> For .js or .mjs reference files only, this will be the exported variable, e.g. for 'export default = {...}' you'd use 'default' here, or 'data' for 'export const data = { ... }'. For .json or .jsonc reference files, this value is ignored. (default: "default")
204
+ -m, --app-context-message <message> Description of your app, to be passed along to the AI, per translation request; overrides any 'appContextMessage' config setting
205
+ -f, --force Force regeneration of all keys; if no '--keys' argument is specified, all keys will be processed (default: false)
197
206
  -rtw, --realtime-writes Write updates to disk immediately, rather than on shutdown (default: false)
198
207
  -y, --tty Use tty/simple renderer; useful for CI (default: false)
199
208
  -M, --model <name> LLM model name to use; defaults are: for "anthropic": "claude-3-7-sonnet-20250219", for "google": "gemini-2.0-flash", for "openai": "gpt-4-turbo"; use the 'list-models' command to view all models
@@ -221,28 +230,29 @@ Options:
221
230
  ```
222
231
 
223
232
  ## Examples
224
- ### Example I
225
- * Import from ``loc.js``
226
- * Look for exported variable ``data``
227
- * Translate with Claude
228
- * Look for context keys starting with `_context:`
229
- * Write output files to the current working directory
233
+ ### Example I: ALT's localized display strings
234
+ The best example is probably the localization for ALT's own display strings.
235
+
236
+ See [alt.config.js](alt.config.js), which contains a `config.js` file and localization files used for the tool's own display strings.
237
+
238
+ Generated with `npm run localize-display-strings`
239
+
240
+
241
+ ### Example II
230
242
  ```bash
231
243
  alt translate --reference-file loc.js
232
- --reference-var-name data
233
- --provider anthropic
234
- --look-for-context-data
235
- --context-prefix _context:
244
+ --reference-var-name data
245
+ --provider anthropic
246
+ --look-for-context-data
247
+ --context-prefix _context:
236
248
  ```
249
+ * Imports reference strings from ``loc.js``
250
+ * Looks for exported variable ``data`` (if `loc.js` has `export data = { ... } `)
251
+ * Translates with Claude
252
+ * Looks for context keys starting with `_context:`
253
+ * Writes output files to the current working directory
237
254
 
238
- ### Example II
239
- * Import config from `./localization-config.json`
240
- * Import from ``loc.js``
241
- * Look for exported ``default`` value
242
- * Translate with ChatGPT
243
- * Look for context keys ending with `[context]`
244
- * Write to disk repeatedly, as changes are processed
245
- * Write files to `./localization`
255
+ ### Example III
246
256
  ```bash
247
257
  alt translate --config-file ./localization-config.json
248
258
  --reference-file loc.js
@@ -251,26 +261,29 @@ alt translate --config-file ./localization-config.json
251
261
  --look-for-context-data
252
262
  --context-suffix "[context]"
253
263
  ```
264
+ * Imports config from `./localization-config.json`
265
+ * Imports reference strings from ``loc.js``
266
+ * Looks for exported ``default`` value
267
+ * Translates with ChatGPT
268
+ * Looks for context keys ending with `[context]`
269
+ * Writes to disk repeatedly, as changes are processed
270
+ * Writes files to `./localization`
271
+
254
272
  ### Example III
255
- * Overrides any config's languages
256
- * Only process the specified strings
257
273
  ```bash
258
274
  alt translate --config-file config.json
259
- --reference-file reference.js
260
- --output-dir localization
275
+ --reference-file localization/reference/source.js
276
+ --output-dir localization/output
261
277
  --provider openai
262
- --look-for-context-data
263
- --context-suffix "[context]"
264
278
  --target-languages vi,aa
265
279
  --keys error-msg,title-hero,button-text-send
266
280
  ```
267
- ### Example: ALT's localized display strings
268
- See `/localization`, which contains a `config.js` file and localization files used for the tool's own display strings.
269
-
270
- Generated with `npm run localize-display-strings`
281
+ * Overrides any target languages specified in the config
282
+ * Only processes the specified keys (`error-msg`, etc.)
271
283
 
272
284
  ## Formatting
273
285
  If your reference values include formatting information like this:
286
+
274
287
  ```javascript
275
288
  "error-msg": "The server returned an error: %%details%%"
276
289
  ```
@@ -278,7 +291,7 @@ or
278
291
  ```javascript
279
292
  "error-msg": "The server returned an error: {{details}}"
280
293
  ```
281
- ...or whatever syntax your app may use, I've found the AI's consistently smart enough not to translate `%%details%%` or `{{details}}` into the target language, and will leave it untouched.
294
+ ...or whatever syntax your app may use for formatting, the AI's consistently smart enough not to translate `%%details%%` or `{{details}}` into the target language, and will leave it untouched.
282
295
 
283
296
  Internally, there is currently nothing in the prompt about this. I've tested with `%%var%%` syntax, and it hasn't failed yet.
284
297
 
@@ -304,6 +317,9 @@ NOTE: Translation will _not_ occur if ALT detects that the given value in the ta
304
317
  later, you can just delete that key/value pair from the given file.
305
318
 
306
319
  ## Additional notes
320
+ ### Yes, you should commit your .localization.cache.json
321
+ If you do not do this, you'll lose important state and need to re-translate everything.
322
+
307
323
  ### Delayed vs. realtime writes
308
324
  By default, ALT will not write to disk until the tool is shutting down (including SIGTERM &ndash; yes, `Ctrl+C` is safe).
309
325
 
@@ -1,7 +1,9 @@
1
1
  {
2
2
  "appContextMessage": "An AI-driven translation tool",
3
3
  "referenceLanguage": "en",
4
- "provider": "openai",
4
+ "provider": "anthropic",
5
+ "outputDir": "localization",
6
+ "referenceFile": "localization/reference.js",
5
7
  "targetLanguages": [
6
8
  "aa",
7
9
  "af",