@l10nmonster/helpers-translated 3.0.0-alpha.4 → 3.0.0-alpha.6

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.
@@ -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
@@ -0,0 +1,6 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
package/laraProvider.js CHANGED
@@ -1,11 +1,11 @@
1
- import { logWarn, providers, styleString } from '@l10nmonster/core';
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.
8
+ * @property {Promise<string>|string} [keySecret] - The Lara API key secret. Optional, but often needed for authentication.
9
9
  * @property {string|Array<string>} [adaptTo] - An optional single translation memory ID or an array of IDs to adapt translations to.
10
10
  * @property {number} [maxChunkSize=60] - Maximum number of text segments (strings) allowed in a single API request chunk. Defaults to 60 if not provided.
11
11
  */
@@ -17,7 +17,6 @@ export class LaraProvider extends providers.ChunkedRemoteTranslationProvider {
17
17
  #keyId;
18
18
  #keySecret;
19
19
  #adaptTo;
20
- #lara;
21
20
  #translateOptions;
22
21
 
23
22
  /**
@@ -29,8 +28,6 @@ export class LaraProvider extends providers.ChunkedRemoteTranslationProvider {
29
28
  this.#keyId = keyId;
30
29
  this.#keySecret = keySecret;
31
30
  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
31
  this.#translateOptions = {
35
32
  contentType: 'text/plain',
36
33
  instructions: [],
@@ -39,6 +36,11 @@ export class LaraProvider extends providers.ChunkedRemoteTranslationProvider {
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 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 this.#lara.translate(payload, sourceLang, targetLang, translateOptions);
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 credentials = new Credentials(this.#keyId, this.#keySecret);
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 {(string | number)[]} [hints] - Hints to include in the MMT request.
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, platform, platformVersion);
70
+ const mmt = new MMTClient(await 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.4",
3
+ "version": "3.0.0-alpha.6",
4
4
  "description": "Helpers to integrate with Translated.com",
5
5
  "main": "index.js",
6
6
  "type": "module",