Skip to content

Commit 81527a4

Browse files
fix: references (#493)
* fix: references * fix: payload references
1 parent d5c8707 commit 81527a4

3 files changed

Lines changed: 47 additions & 16 deletions

File tree

.changeset/brown-eyes-occur.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"lingo.dev": patch
3+
"@lingo.dev/_sdk": patch
4+
---
5+
6+
fix payload references

packages/cli/src/cli/cmd/i18n.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { bucketTypeSchema, I18nConfig, localeCodeSchema, resolveOverridenLocale } from "@lingo.dev/_spec";
2-
import { ReplexicaEngine } from "@lingo.dev/_sdk";
2+
import { LingoDotDevEngine } from "@lingo.dev/_sdk";
33
import { Command } from "interactive-commander";
44
import Z from "zod";
55
import _ from "lodash";
@@ -346,7 +346,7 @@ async function retryWithExponentialBackoff<T>(
346346
}
347347

348348
function createLocalizationEngineConnection(params: { apiKey: string; apiUrl: string; maxRetries?: number }) {
349-
const replexicaEngine = new ReplexicaEngine({
349+
const replexicaEngine = new LingoDotDevEngine({
350350
apiKey: params.apiKey,
351351
apiUrl: params.apiUrl,
352352
});
@@ -373,6 +373,10 @@ function createLocalizationEngineConnection(params: { apiKey: string; apiUrl: st
373373
{
374374
sourceLocale: args.sourceLocale,
375375
targetLocale: args.targetLocale,
376+
reference: {
377+
[args.sourceLocale]: args.sourceData,
378+
[args.targetLocale]: args.targetData,
379+
},
376380
},
377381
onProgress,
378382
),

packages/sdk/src/index.ts

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ const engineParamsSchema = Z.object({
1010
}).passthrough();
1111

1212
const payloadSchema = Z.record(Z.string(), Z.any());
13+
const referenceSchema = Z.record(localeCodeSchema, payloadSchema);
1314

1415
const localizationParamsSchema = Z.object({
1516
sourceLocale: localeCodeSchema,
1617
targetLocale: localeCodeSchema,
1718
fast: Z.boolean().optional(),
19+
reference: referenceSchema.optional(),
1820
});
1921

20-
const referenceSchema = Z.record(localeCodeSchema, payloadSchema);
21-
2222
/**
23-
* ReplexicaEngine class for interacting with the Lingo.dev API
23+
* LingoDotDevEngine class for interacting with the LingoDotDev API
2424
* A powerful localization engine that supports various content types including
2525
* plain text, objects, chat sequences, and HTML documents.
2626
*/
27-
export class ReplexicaEngine {
28-
private config: Z.infer<typeof engineParamsSchema>;
27+
export class LingoDotDevEngine {
28+
protected config: Z.infer<typeof engineParamsSchema>;
2929

3030
/**
31-
* Create a new ReplexicaEngine instance
31+
* Create a new LingoDotDevEngine instance
3232
* @param config - Configuration options for the Engine
3333
*/
3434
constructor(config: Partial<Z.infer<typeof engineParamsSchema>>) {
@@ -39,15 +39,13 @@ export class ReplexicaEngine {
3939
* Localize content using the Lingo.dev API
4040
* @param payload - The content to be localized
4141
* @param params - Localization parameters including source/target locales and fast mode option
42-
* @param reference - Optional reference translations to maintain consistency
4342
* @param progressCallback - Optional callback function to report progress (0-100)
4443
* @returns Localized content
4544
* @internal
4645
*/
4746
async _localizeRaw(
4847
payload: Z.infer<typeof payloadSchema>,
4948
params: Z.infer<typeof localizationParamsSchema>,
50-
reference?: Z.infer<typeof referenceSchema>,
5149
progressCallback?: (
5250
progress: number,
5351
sourceChunk: Record<string, string>,
@@ -68,7 +66,7 @@ export class ReplexicaEngine {
6866
const processedPayloadChunk = await this.localizeChunk(
6967
finalParams.sourceLocale,
7068
finalParams.targetLocale,
71-
{ data: chunk, reference },
69+
{ data: chunk, reference: params.reference },
7270
workflowId,
7371
params.fast || false,
7472
);
@@ -93,7 +91,10 @@ export class ReplexicaEngine {
9391
private async localizeChunk(
9492
sourceLocale: string,
9593
targetLocale: string,
96-
payload: { data: any; reference: any },
94+
payload: {
95+
data: Z.infer<typeof payloadSchema>;
96+
reference?: Z.infer<typeof referenceSchema>;
97+
},
9798
workflowId: string,
9899
fast: boolean,
99100
): Promise<Record<string, string>> {
@@ -198,7 +199,7 @@ export class ReplexicaEngine {
198199
processedChunk: Record<string, string>,
199200
) => void,
200201
): Promise<Record<string, any>> {
201-
return this._localizeRaw(obj, params, undefined, progressCallback);
202+
return this._localizeRaw(obj, params, progressCallback);
202203
}
203204

204205
/**
@@ -216,7 +217,7 @@ export class ReplexicaEngine {
216217
params: Z.infer<typeof localizationParamsSchema>,
217218
progressCallback?: (progress: number) => void,
218219
): Promise<string> {
219-
const response = await this._localizeRaw({ text }, params, undefined, progressCallback);
220+
const response = await this._localizeRaw({ text }, params, progressCallback);
220221
return response.text || "";
221222
}
222223

@@ -265,7 +266,7 @@ export class ReplexicaEngine {
265266
params: Z.infer<typeof localizationParamsSchema>,
266267
progressCallback?: (progress: number) => void,
267268
): Promise<Array<{ name: string; text: string }>> {
268-
const localized = await this._localizeRaw({ chat }, params, undefined, progressCallback);
269+
const localized = await this._localizeRaw({ chat }, params, progressCallback);
269270

270271
return Object.entries(localized).map(([key, value]) => ({
271272
name: chat[parseInt(key.split("_")[1])].name,
@@ -371,7 +372,7 @@ export class ReplexicaEngine {
371372
.filter((n) => n.nodeType === 1 || (n.nodeType === 3 && n.textContent?.trim()))
372373
.forEach(processNode);
373374

374-
const localizedContent = await this._localizeRaw(extractedContent, params, undefined, progressCallback);
375+
const localizedContent = await this._localizeRaw(extractedContent, params, progressCallback);
375376

376377
// Update the DOM with localized content
377378
document.documentElement.setAttribute("lang", params.targetLocale);
@@ -428,3 +429,23 @@ export class ReplexicaEngine {
428429
return jsonResponse.locale;
429430
}
430431
}
432+
433+
/**
434+
* @deprecated Use LingoDotDevEngine instead. This class is maintained for backwards compatibility.
435+
*/
436+
export class ReplexicaEngine extends LingoDotDevEngine {
437+
constructor(config: Partial<Z.infer<typeof engineParamsSchema>>) {
438+
super(config);
439+
console.warn("ReplexicaEngine is deprecated. Please use LingoDotDevEngine instead.");
440+
}
441+
}
442+
443+
/**
444+
* @deprecated Use LingoDotDevEngine instead. This class is maintained for backwards compatibility.
445+
*/
446+
export class LingoEngine extends LingoDotDevEngine {
447+
constructor(config: Partial<Z.infer<typeof engineParamsSchema>>) {
448+
super(config);
449+
console.warn("LingoEngine is deprecated. Please use LingoDotDevEngine instead.");
450+
}
451+
}

0 commit comments

Comments
 (0)