@drone1/alt 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.
Files changed (96) hide show
  1. package/.nvmrc +1 -0
  2. package/LICENSE +21 -0
  3. package/README.md +247 -0
  4. package/assets/figlet-fonts/THIS.flf +720 -0
  5. package/bin.mjs +8 -0
  6. package/localization/.localization.cache.json +1872 -0
  7. package/localization/aa.json +24 -0
  8. package/localization/af.json +24 -0
  9. package/localization/agq.json +24 -0
  10. package/localization/ak.json +24 -0
  11. package/localization/am.json +24 -0
  12. package/localization/ar.json +24 -0
  13. package/localization/as.json +24 -0
  14. package/localization/asa.json +24 -0
  15. package/localization/ast.json +24 -0
  16. package/localization/az.json +24 -0
  17. package/localization/ba.json +24 -0
  18. package/localization/bas.json +24 -0
  19. package/localization/be.json +24 -0
  20. package/localization/bem.json +24 -0
  21. package/localization/bez.json +24 -0
  22. package/localization/bg.json +24 -0
  23. package/localization/bm.json +24 -0
  24. package/localization/bn.json +24 -0
  25. package/localization/bo.json +24 -0
  26. package/localization/br.json +24 -0
  27. package/localization/brx.json +24 -0
  28. package/localization/bs.json +24 -0
  29. package/localization/byn.json +24 -0
  30. package/localization/ca.json +24 -0
  31. package/localization/ccp.json +24 -0
  32. package/localization/cd-RU.json +24 -0
  33. package/localization/ceb.json +24 -0
  34. package/localization/cgg.json +24 -0
  35. package/localization/chr.json +24 -0
  36. package/localization/co.json +24 -0
  37. package/localization/config.json +76 -0
  38. package/localization/cs.json +24 -0
  39. package/localization/cu-RU.json +24 -0
  40. package/localization/da.json +24 -0
  41. package/localization/de-AT.json +24 -0
  42. package/localization/de-CH.json +24 -0
  43. package/localization/de-DE.json +24 -0
  44. package/localization/dua.json +24 -0
  45. package/localization/dv.json +24 -0
  46. package/localization/dz.json +24 -0
  47. package/localization/ebu.json +24 -0
  48. package/localization/en.json +26 -0
  49. package/localization/es-ES.json +24 -0
  50. package/localization/es-MX.json +24 -0
  51. package/localization/et.json +24 -0
  52. package/localization/eu.json +24 -0
  53. package/localization/fr-CA.json +24 -0
  54. package/localization/fr-CH.json +24 -0
  55. package/localization/fr-FR.json +24 -0
  56. package/localization/gsw.json +24 -0
  57. package/localization/hi.json +24 -0
  58. package/localization/hr.json +24 -0
  59. package/localization/hy.json +24 -0
  60. package/localization/ja.json +24 -0
  61. package/localization/km.json +24 -0
  62. package/localization/ksf.json +24 -0
  63. package/localization/ku.json +24 -0
  64. package/localization/kw.json +24 -0
  65. package/localization/my.json +24 -0
  66. package/localization/nl.json +24 -0
  67. package/localization/prs.json +24 -0
  68. package/localization/reference.js +21 -0
  69. package/localization/ru.json +24 -0
  70. package/localization/sq.json +24 -0
  71. package/localization/swc.json +24 -0
  72. package/localization/th.json +24 -0
  73. package/localization/tzm-Latn-.json +24 -0
  74. package/localization/uk.json +24 -0
  75. package/localization/vi.json +24 -0
  76. package/localization/zh-Hans.json +24 -0
  77. package/localization/zh-Hant.json +24 -0
  78. package/npm-shrinkwrap.json +1143 -0
  79. package/package.json +48 -0
  80. package/src/assert.js +19 -0
  81. package/src/cache.js +12 -0
  82. package/src/config.js +26 -0
  83. package/src/consts.js +23 -0
  84. package/src/context-keys.js +10 -0
  85. package/src/io.js +129 -0
  86. package/src/localizer/localize.js +135 -0
  87. package/src/logging.js +35 -0
  88. package/src/logo.js +22 -0
  89. package/src/main.mjs +103 -0
  90. package/src/options.js +18 -0
  91. package/src/provider.js +17 -0
  92. package/src/providers/anthropic.mjs +39 -0
  93. package/src/providers/openai.mjs +30 -0
  94. package/src/shutdown.js +36 -0
  95. package/src/translate.js +602 -0
  96. package/src/utils.js +108 -0
package/.nvmrc ADDED
@@ -0,0 +1 @@
1
+ 22.14.8
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 drone1
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,247 @@
1
+ ```
2
+ ▄▀▀█▄ ▄▀▀▀▀▄ ▄▀▀▀█▀▀▄
3
+ ▐ ▄▀ ▀▄ █ █ █ █ ▐
4
+ █▄▄▄█ ▐ █ ▐ █
5
+ ▄▀ █ █ █
6
+ █ ▄▀ ▄▀▄▄▄▄▄▄▀ ▄▀
7
+ ▐ ▐ █ █
8
+ ▐ ▐
9
+ ```
10
+ ![Anthropic](https://img.shields.io/badge/Anthropic-black?logo=anthropic&logoColor=white)
11
+ ![ChatGPT](https://img.shields.io/badge/ChatGPT-74aa9c?logo=openai&logoColor=white)
12
+ ![Node.js](https://img.shields.io/badge/Node.js-339933?logo=nodedotjs&logoColor=white)
13
+ ![Contributions Welcome](https://img.shields.io/badge/contributions-welcome-brightgreen)
14
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
15
+
16
+ # AI Localization Tool
17
+ Translates all strings in a reference `.js` file to all target languages using AI.
18
+
19
+ ## Features
20
+ * Loads reference key/value pairs from a reference file
21
+ * Localizes using AI as needed, writing to a .json file per language
22
+ * App-level context can be specified [`appContextMessage`]
23
+ * Additional context can be specified per string [`--contextPrefix`, `--contextSuffix`]
24
+ * Supports multiple AI providers: Claude, OpenAI [`--provider`]
25
+ * User-modifications to output files are safe and will not be overwritten
26
+ * Languages are specified using BCP47 tags
27
+
28
+ ## Installation
29
+ ```bash
30
+ npm install -g github:drone1/alt
31
+ ```
32
+ or
33
+ ```bash
34
+ npm install -g https://github.com/drone1/alt.git
35
+ ```
36
+ ## Setup
37
+ Create a reference file for your reference data. For example, ``reference.js``:
38
+ ```javascript
39
+ export default {
40
+ 'error-msg': `Sorry, we don't know how to do anything`,
41
+ 'success-msg': `A massive "achievement"`,
42
+ '_context:success-msg': `This text is for a button when a user completes a task`
43
+ }
44
+ ```
45
+ Use whatever filename you prefer, but currently a .js extension is required.
46
+ You can specify an exported variable instead of using `default`. See `--referenceVarName`.
47
+
48
+ 2. Running
49
+ ```bash
50
+ ANTHROPIC_API_KEY=<secret>
51
+ alt --reference-file ./reference.js --reference-language en --target-languages aa,bo,es-MX,hi,zh-SG --provider anthropic
52
+ ```
53
+ or
54
+ ```bash
55
+ OPENAI_API_KEY=<secret>
56
+ alt --reference-file ./reference.js --reference-language en --target-languages aa,bo,es-MX,hi,zh-SG --provider openai
57
+ ```
58
+ These commands would iterate across all key/value pairs in the variable exported from `./reference.js` and if needed, translate.
59
+
60
+ The examples above would write `aa.json`, `bo.json`, etc., to the current working directory.
61
+
62
+ Sample output:
63
+ ```json
64
+ {
65
+ "error-msg": "དགོངས་དག་ང་ཚོས་ག་རེ་བྱེད་དགོས་མིན་ཤེས་ཀྱི་མི་འདུག",
66
+ "success-msg": "གྲུབ་འབྲས་\"ཆེན་པོ་\"ཞིག"
67
+ }
68
+ ```
69
+
70
+ 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`
71
+
72
+ ## Display language
73
+ ALT CLI itself has been localized so you can use it many languages. For non-English languages, you can set the display language with the `ALT_LANGUAGE` environment variable. Please feel free to submit
74
+ an issue or a PR if you'd like to add another language.
75
+
76
+ ## Rules
77
+ Translation will occur for a given target language & key if any of the following are true:
78
+ * The reference value was modified and translation has not yet occurred for the given language/key
79
+ * If a context value for the given target language/key is found and has been modified.
80
+ * The `--force` flag is used
81
+
82
+ Translation will _not_ occur if `alt` detects that the given value in the target language file has been manually modified. If you modify an output value manually and want it to be re-translated
83
+ later, you can just delete that key/value pair from the given file.
84
+
85
+ ## Config file
86
+ [_optional_] You can create a config file. By default, `ALT` will search the output directory for `config.json`, but you can specify a path directly using
87
+ `--config-file`.
88
+ Example
89
+ config:
90
+
91
+ ```
92
+ {
93
+ "appContextMessage": "This is a description of my app",
94
+ "referenceLanguage": "ar",
95
+ "provider": "anthropic",
96
+ "lookForContextData": true,
97
+ "contextPrefix": "_context:",
98
+ "contextSuffix": "",
99
+ "targetLanguages": [
100
+ "es-MX", "zh-SG"
101
+ ]
102
+ }
103
+ ```
104
+
105
+ 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.
106
+
107
+ ## Usage
108
+ ```
109
+ alt [options] [command]
110
+
111
+ Options:
112
+ -V, --version output the version number
113
+ -r, --reference-file <path> Path to reference JSONC file (default language)
114
+ -rl, --reference-language <language> The reference file's language; overrides any
115
+ 'referenceLanguage' config setting
116
+ -p, --provider <name> AI provider to use for translations (anthropic,
117
+ openai); overrides any 'provider' config setting
118
+ -o, --output-dir <path> Output directory for localized files
119
+ -l, --target-languages <list> Comma-separated list of language codes; overrides
120
+ any 'targetLanguages' config setting
121
+ -k, --keys <list> Comma-separated list of keys to process
122
+ -j, --reference-var-name <var name> The exported variable in the reference file, e.g.
123
+ export default = {...} you'd use 'default'
124
+ (default: "default")
125
+ -f, --force Force regeneration of all translations (default:
126
+ false)
127
+ -rtw, --realtime-writes Write updates to disk immediately, rather than on
128
+ shutdown (default: false)
129
+ -m, --app-context-message <message> Description of your app to give context. Passed
130
+ with each translation request; overrides any
131
+ 'appContextMessage' config setting
132
+ -y, --tty Use tty/simple renderer; useful for CI (default:
133
+ false)
134
+ -c, --config-file <path> Path to config file; defaults to <output
135
+ dir>/config.json
136
+ -x, --max-retries <integer> Maximum retries on failure (default: 3)
137
+ -n, --normalize-output-filenames Normalizes output filenames (to all lower-case);
138
+ overrides any 'normalizeOutputFilenames' in config
139
+ setting (default: false)
140
+ -v, --verbose Enables verbose spew (default: false)
141
+ -d, --debug Enables debug spew (default: false)
142
+ -t, --trace Enables trace spew (default: false)
143
+ --context-prefix <value> String to be prefixed to all keys to search for
144
+ additional context, which are passed along to the
145
+ AI for context
146
+ --context-suffix <value> String to be suffixed to all keys to search for
147
+ additional context, which are passed along to the
148
+ AI for context
149
+ --look-for-context-data If specified, ALT will pass any context data
150
+ specified in the reference file to the AI provider
151
+ for translation. At least one of --contextPrefix
152
+ or --contextSuffix must be specified (default:
153
+ false)
154
+ -h, --help display help for command
155
+
156
+ Commands:
157
+ translate
158
+
159
+ Environment variables:
160
+ ANTHROPIC_API_KEY Your Anthropic API key
161
+ OPENAI_API_KEY Your OpenAI API key
162
+ ALT_LANGUAGE CLI display language
163
+ ```
164
+
165
+ ## Examples
166
+ ### Example I
167
+ * Import from ``loc.js``
168
+ * Look for exported variable ``data``
169
+ * Translate with Claude
170
+ * Look for context keys starting with `_context:`
171
+ * Write output files to the current working directory
172
+ ```bash
173
+ alt --reference-file loc.js
174
+ --reference-var-name data
175
+ --provider anthropic
176
+ --look-for-context-data
177
+ --context-prefix _context:
178
+ ```
179
+ ### Example II
180
+ * Import config from `./localization-config.json`
181
+ * Import from ``loc.js``
182
+ * Look for exported ``default`` value
183
+ * Translate with ChatGPT
184
+ * Look for context keys ending with `[context]`
185
+ * Write to disk repeatedly, as changes are processed
186
+ * Write files to `./localization`
187
+ ```bash
188
+ alt --config-file ./localization-config.json
189
+ --reference-file loc.js
190
+ --output-dir localization
191
+ --provider openai
192
+ --look-for-context-data
193
+ --context-suffix "[context]"
194
+ ```
195
+ ### Example III
196
+ * Overrides any config's languages
197
+ * Only process the specified strings
198
+ ```bash
199
+ alt --config-file config.json
200
+ --reference-file reference.js
201
+ --output-dir localization
202
+ --provider openai
203
+ --look-for-context-data
204
+ --context-suffix "[context]"
205
+ --target-languages vi,aa
206
+ --keys error-msg,title-hero,button-text-send
207
+ ```
208
+ ### Example: ALT's localized display strings
209
+ See `/localization`, which contains a `config.js` file and localization files used for the tool's own display strings.
210
+
211
+ Generated with `npm run localize-display-strings`
212
+
213
+ ## Formatting
214
+ If your reference values include formatting information like this:
215
+ ```javascript
216
+ "error-msg": "The server returned an error: %%details%%"
217
+ ```
218
+ or
219
+ ```javascript
220
+ "error-msg": "The server returned an error: {{details}}"
221
+ ```
222
+ ...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.
223
+
224
+ Internally, there is currently nothing in the prompt about this. I've tested with `%%var%%` syntax, and it hasn't failed yet.
225
+
226
+ Please submit an issue if it causes you any trouble.
227
+
228
+ ## Additional notes
229
+ ### Delayed vs. realtime writes
230
+ By default, `alt` will not write to disk until the tool is shutting down (including SIGTERM &ndash; yes, `Ctrl+C` is safe).
231
+
232
+ This behavior is useful if your application is monitoring the output directory and you don't want your server
233
+ constantly restarting, for example.
234
+
235
+ If you prefer to write updates to disk in real-time (anytime any output data changes, due to translation, etc), you can pass `--realtime-writes`.
236
+
237
+ ### CI
238
+ You may want to use `--tty` for more useful output.
239
+
240
+ ## Next steps
241
+ - Add Google provider.
242
+ - Add support for reference files in JSON format
243
+ - It'd be nice to rely on $LANG in POSIX, but I didn't find a clean and reliable conversion from POSIX to BCP47 when I did a cursory search, which includes edge cases
244
+ - Bug: If a user modifies a reference value, then runs and cancels, then runs again, any language/key values which would have been affected by the change will no longer be modified. The tool needs to detect these types of changes at a higher level than it is currently so that key/values can be deleted for all languages and written to disk, so that they're effectively missing and will have to be re-translated. The state that a reference value changed would be lost across runs of the tool, however, but at least the result would be a fix, with this approach.
245
+ - Tests :]
246
+
247
+ Feel free to submit a PR.