@l10nmonster/helpers-translated 3.0.0-alpha.1 → 3.0.0-alpha.10
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.
- package/.releaserc.json +31 -0
- package/CHANGELOG.md +6 -0
- package/laraProvider.js +16 -16
- package/mmtProvider.js +11 -13
- package/package.json +2 -2
package/.releaserc.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
{
|
|
2
|
+
"branches": [
|
|
3
|
+
"main",
|
|
4
|
+
{
|
|
5
|
+
"name": "next",
|
|
6
|
+
"prerelease": "alpha"
|
|
7
|
+
},
|
|
8
|
+
{
|
|
9
|
+
"name": "beta",
|
|
10
|
+
"prerelease": "beta"
|
|
11
|
+
}
|
|
12
|
+
],
|
|
13
|
+
"tagFormat": "@l10nmonster/helpers-translated@${version}",
|
|
14
|
+
"plugins": [
|
|
15
|
+
"@semantic-release/commit-analyzer",
|
|
16
|
+
"@semantic-release/release-notes-generator",
|
|
17
|
+
{
|
|
18
|
+
"path": "@semantic-release/changelog",
|
|
19
|
+
"changelogFile": "CHANGELOG.md"
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
"path": "@semantic-release/npm",
|
|
23
|
+
"npmPublish": true
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
"path": "@semantic-release/git",
|
|
27
|
+
"assets": ["CHANGELOG.md", "package.json"],
|
|
28
|
+
"message": "chore(release): @l10nmonster/helpers-translated@${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
}
|
package/CHANGELOG.md
ADDED
package/laraProvider.js
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { providers, styleString } from '@l10nmonster/core';
|
|
2
2
|
import { Credentials, Translator } from '@translated/lara';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* @typedef {object} LaraProviderOptions
|
|
6
6
|
* @extends ChunkedRemoteTranslationProviderOptions
|
|
7
7
|
* @property {string} keyId - The Lara API key id. This is required.
|
|
8
|
-
* @property {string} [keySecret] - The Lara API key secret. Optional, but often needed for authentication.
|
|
9
|
-
* @property {string|Array<string>} [adaptTo] - An optional single translation memory ID or an array of IDs to adapt translations to.
|
|
8
|
+
* @property {Promise<string>|string} [keySecret] - The Lara API key secret. Optional, but often needed for authentication.
|
|
9
|
+
* @property {string | Array<string>} [adaptTo] - An optional single translation memory ID or an array of IDs to adapt translations to.
|
|
10
|
+
* @property {string | Array<string>} [glossaries] - Glossaries to include in the request.
|
|
10
11
|
* @property {number} [maxChunkSize=60] - Maximum number of text segments (strings) allowed in a single API request chunk. Defaults to 60 if not provided.
|
|
11
12
|
*/
|
|
12
13
|
|
|
@@ -16,29 +17,30 @@ import { Credentials, Translator } from '@translated/lara';
|
|
|
16
17
|
export class LaraProvider extends providers.ChunkedRemoteTranslationProvider {
|
|
17
18
|
#keyId;
|
|
18
19
|
#keySecret;
|
|
19
|
-
#adaptTo;
|
|
20
|
-
#lara;
|
|
21
20
|
#translateOptions;
|
|
22
21
|
|
|
23
22
|
/**
|
|
24
23
|
* Initializes a new instance of the LaraProvider class.
|
|
25
24
|
* @param {LaraProviderOptions} options - Configuration options for the provider.
|
|
26
25
|
*/
|
|
27
|
-
constructor({ keyId, keySecret, adaptTo, ...options }) {
|
|
26
|
+
constructor({ keyId, keySecret, adaptTo, glossaries, ...options }) {
|
|
28
27
|
super({ maxChunkSize: 60, ...options }); // maximum number of strings sent to Lara is 128 including notes
|
|
29
28
|
this.#keyId = keyId;
|
|
30
29
|
this.#keySecret = keySecret;
|
|
31
|
-
this.#adaptTo = adaptTo && (Array.isArray(adaptTo) ? adaptTo : adaptTo.split(','));
|
|
32
|
-
const credentials = new Credentials(this.#keyId, this.#keySecret);
|
|
33
|
-
this.#lara = new Translator(credentials);
|
|
34
30
|
this.#translateOptions = {
|
|
35
31
|
contentType: 'text/plain',
|
|
36
32
|
instructions: [],
|
|
37
33
|
};
|
|
38
|
-
|
|
34
|
+
adaptTo && (this.#translateOptions.adaptTo = Array.isArray(adaptTo) ? adaptTo : adaptTo.split(','));
|
|
35
|
+
glossaries && (this.#translateOptions.glossaries = Array.isArray(glossaries) ? glossaries : glossaries.split(','));
|
|
39
36
|
this.defaultInstructions && this.#translateOptions.instructions.push(this.defaultInstructions);
|
|
40
37
|
}
|
|
41
38
|
|
|
39
|
+
async #getLara() {
|
|
40
|
+
const credentials = new Credentials(this.#keyId, await (typeof this.#keySecret === 'function' ? this.#keySecret() : this.#keySecret));
|
|
41
|
+
return new Translator(credentials);
|
|
42
|
+
}
|
|
43
|
+
|
|
42
44
|
prepareTranslateChunkArgs({ sourceLang, targetLang, xmlTus, instructions }) {
|
|
43
45
|
const payload = xmlTus.map(xmlTu => {
|
|
44
46
|
const textBlock = [];
|
|
@@ -52,8 +54,9 @@ export class LaraProvider extends providers.ChunkedRemoteTranslationProvider {
|
|
|
52
54
|
|
|
53
55
|
async startTranslateChunk(args) {
|
|
54
56
|
const { payload, sourceLang, targetLang, translateOptions } = args;
|
|
57
|
+
const lara = await this.#getLara();
|
|
55
58
|
try {
|
|
56
|
-
return await
|
|
59
|
+
return await lara.translate(payload, sourceLang, targetLang, translateOptions);
|
|
57
60
|
} catch (e) {
|
|
58
61
|
throw new Error(`Lara API error ${e.statusCode}: ${e.type}: ${e.message}`);
|
|
59
62
|
}
|
|
@@ -72,15 +75,12 @@ export class LaraProvider extends providers.ChunkedRemoteTranslationProvider {
|
|
|
72
75
|
return info;
|
|
73
76
|
}
|
|
74
77
|
try {
|
|
75
|
-
const
|
|
76
|
-
const lara = new Translator(credentials);
|
|
78
|
+
const lara = await this.#getLara();
|
|
77
79
|
const languages = (await lara.getLanguages()).sort();
|
|
78
80
|
info.description.push(styleString`Vendor-supported languages: ${languages?.join(', ') ?? 'unknown'}`);
|
|
79
81
|
const memories = await lara.memories.list();
|
|
80
82
|
if (memories.length > 0) {
|
|
81
|
-
memories.forEach(m =>
|
|
82
|
-
info.description.push(styleString`Vendor TM "${m.name}": id: ${m.id} owner: ${m.ownerId} collaborators: ${m.collaboratorsCount} created: ${m.createdAt} updated: ${m.updatedAt}`)
|
|
83
|
-
);
|
|
83
|
+
memories.forEach(m => info.description.push(styleString`Vendor TM "${m.name}": id: ${m.id} owner: ${m.ownerId} collaborators: ${m.collaboratorsCount} created: ${m.createdAt} updated: ${m.updatedAt}`));
|
|
84
84
|
} else {
|
|
85
85
|
info.description.push(styleString`No TMs configured.`);
|
|
86
86
|
}
|
package/mmtProvider.js
CHANGED
|
@@ -5,10 +5,12 @@ import { ModernMT as MMTClient } from 'modernmt';
|
|
|
5
5
|
* @typedef {object} MMTProviderOptions
|
|
6
6
|
* @extends ChunkedRemoteTranslationProviderOptions
|
|
7
7
|
* @property {string} [id] - Global identifier (by default 'MMTBatch' or 'MMTRealtime')
|
|
8
|
-
* @property {string} apiKey - The ModernMT API key.
|
|
8
|
+
* @property {Promise<string>|string} apiKey - The ModernMT API key.
|
|
9
9
|
* @property {string} [webhook] - The webhook URL for batch translation.
|
|
10
10
|
* @property {function(any): any} [chunkFetcher] - The chunk fetcher operation name.
|
|
11
|
-
* @property {
|
|
11
|
+
* @property {string | number[]} [hints] - Hints to include in the MMT request.
|
|
12
|
+
* @property {string | number[]} [glossaries] - Glossaries to include in the MMT request.
|
|
13
|
+
* @property {boolean} [ignoreGlossaryCase] - Whether to use ignore case in glossary entries.
|
|
12
14
|
* @property {boolean} [multiline] - Whether to use multiline mode.
|
|
13
15
|
*/
|
|
14
16
|
|
|
@@ -16,11 +18,13 @@ import { ModernMT as MMTClient } from 'modernmt';
|
|
|
16
18
|
* Provider for Translated Modern MT.
|
|
17
19
|
*/
|
|
18
20
|
export class MMTProvider extends providers.ChunkedRemoteTranslationProvider {
|
|
21
|
+
#apiKey;
|
|
22
|
+
|
|
19
23
|
/**
|
|
20
24
|
* Initializes a new instance of the MMTProvider class.
|
|
21
25
|
* @param {MMTProviderOptions} options - Configuration options for the provider.
|
|
22
26
|
*/
|
|
23
|
-
constructor({ id, apiKey, webhook, chunkFetcher, hints, multiline = true, ...options }) {
|
|
27
|
+
constructor({ id, apiKey, webhook, chunkFetcher, hints, glossaries, ignoreGlossaryCase, multiline = true, ...options }) {
|
|
24
28
|
id ??= webhook ? 'MMTBatch' : 'MMTRealtime';
|
|
25
29
|
super({ id, ...options });
|
|
26
30
|
if (webhook) {
|
|
@@ -30,24 +34,19 @@ export class MMTProvider extends providers.ChunkedRemoteTranslationProvider {
|
|
|
30
34
|
throw new Error('If you specify a webhook you must also specify a chunkFetcher');
|
|
31
35
|
}
|
|
32
36
|
}
|
|
37
|
+
this.#apiKey = apiKey;
|
|
33
38
|
this.baseRequest = {
|
|
34
|
-
mmtConstructor: [ apiKey, 'l10n.monster/MMT', '3.0' ],
|
|
35
39
|
hints,
|
|
36
40
|
options: {
|
|
37
41
|
multiline,
|
|
38
42
|
format: 'text/xml',
|
|
43
|
+
glossaries,
|
|
44
|
+
ignoreGlossaryCase,
|
|
39
45
|
},
|
|
40
46
|
webhook,
|
|
41
47
|
}
|
|
42
48
|
}
|
|
43
49
|
|
|
44
|
-
start(job) {
|
|
45
|
-
if (!this.baseRequest.mmtConstructor[0]) {
|
|
46
|
-
throw new Error('You must have an apiKey to start an MMT job');
|
|
47
|
-
}
|
|
48
|
-
return super.start(job);
|
|
49
|
-
}
|
|
50
|
-
|
|
51
50
|
prepareTranslateChunkArgs({ sourceLang, targetLang, xmlTus, jobGuid, chunkNumber }) {
|
|
52
51
|
return {
|
|
53
52
|
sourceLang,
|
|
@@ -67,9 +66,8 @@ export class MMTProvider extends providers.ChunkedRemoteTranslationProvider {
|
|
|
67
66
|
|
|
68
67
|
async startTranslateChunk(args) {
|
|
69
68
|
const { sourceLang, targetLang, q, hints, contextVector, options, webhook, batchOptions } = args;
|
|
70
|
-
const [ apiKey, platform, platformVersion ] = this.baseRequest.mmtConstructor;
|
|
71
69
|
try {
|
|
72
|
-
const mmt = new MMTClient(apiKey,
|
|
70
|
+
const mmt = new MMTClient(await (typeof this.#apiKey === 'function' ? this.#apiKey() : this.#apiKey), 'l10n.monster/MMT', '3.0');
|
|
73
71
|
if (webhook) {
|
|
74
72
|
const response = await mmt.batchTranslate(webhook, sourceLang, targetLang, q, hints, contextVector, batchOptions);
|
|
75
73
|
return { enqueued: response };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@l10nmonster/helpers-translated",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.10",
|
|
4
4
|
"description": "Helpers to integrate with Translated.com",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
"author": "Diego Lagunas",
|
|
11
11
|
"license": "MIT",
|
|
12
12
|
"peerDependencies": {
|
|
13
|
-
"@l10nmonster/core": "
|
|
13
|
+
"@l10nmonster/core": "^3.0.0-alpha.0"
|
|
14
14
|
},
|
|
15
15
|
"dependencies": {
|
|
16
16
|
"@translated/lara": "^1.4.0",
|