Skip to content

Commit 7597b99

Browse files
authored
feat(sdk): support null value for sourceLocale (#537)
Lingo.dev will automatically detect the language of supplied content. Localization requests with language detection will take longer compared to passing explicit `sourceLocale`.
1 parent e22a8f8 commit 7597b99

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

.changeset/weak-jobs-shave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@lingo.dev/_sdk": patch
3+
---
4+
5+
automatic source locale detection

packages/sdk/src/index.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const payloadSchema = Z.record(Z.string(), Z.any());
1313
const referenceSchema = Z.record(localeCodeSchema, payloadSchema);
1414

1515
const localizationParamsSchema = Z.object({
16-
sourceLocale: localeCodeSchema,
16+
sourceLocale: Z.union([localeCodeSchema, Z.null()]),
1717
targetLocale: localeCodeSchema,
1818
fast: Z.boolean().optional(),
1919
reference: referenceSchema.optional(),
@@ -49,8 +49,8 @@ export class LingoDotDevEngine {
4949
progressCallback?: (
5050
progress: number,
5151
sourceChunk: Record<string, string>,
52-
processedChunk: Record<string, string>,
53-
) => void,
52+
processedChunk: Record<string, string>
53+
) => void
5454
): Promise<Record<string, string>> {
5555
const finalPayload = payloadSchema.parse(payload);
5656
const finalParams = localizationParamsSchema.parse(params);
@@ -68,7 +68,7 @@ export class LingoDotDevEngine {
6868
finalParams.targetLocale,
6969
{ data: chunk, reference: params.reference },
7070
workflowId,
71-
params.fast || false,
71+
params.fast || false
7272
);
7373

7474
if (progressCallback) {
@@ -89,14 +89,14 @@ export class LingoDotDevEngine {
8989
* @returns Localized chunk
9090
*/
9191
private async localizeChunk(
92-
sourceLocale: string,
92+
sourceLocale: string | null,
9393
targetLocale: string,
9494
payload: {
9595
data: Z.infer<typeof payloadSchema>;
9696
reference?: Z.infer<typeof referenceSchema>;
9797
},
9898
workflowId: string,
99-
fast: boolean,
99+
fast: boolean
100100
): Promise<Record<string, string>> {
101101
const res = await fetch(`${this.config.apiUrl}/i18n`, {
102102
method: "POST",
@@ -115,7 +115,7 @@ export class LingoDotDevEngine {
115115
reference: payload.reference,
116116
},
117117
null,
118-
2,
118+
2
119119
),
120120
});
121121

@@ -196,8 +196,8 @@ export class LingoDotDevEngine {
196196
progressCallback?: (
197197
progress: number,
198198
sourceChunk: Record<string, string>,
199-
processedChunk: Record<string, string>,
200-
) => void,
199+
processedChunk: Record<string, string>
200+
) => void
201201
): Promise<Record<string, any>> {
202202
return this._localizeRaw(obj, params, progressCallback);
203203
}
@@ -215,7 +215,7 @@ export class LingoDotDevEngine {
215215
async localizeText(
216216
text: string,
217217
params: Z.infer<typeof localizationParamsSchema>,
218-
progressCallback?: (progress: number) => void,
218+
progressCallback?: (progress: number) => void
219219
): Promise<string> {
220220
const response = await this._localizeRaw({ text }, params, progressCallback);
221221
return response.text || "";
@@ -236,16 +236,16 @@ export class LingoDotDevEngine {
236236
sourceLocale: LocaleCode;
237237
targetLocales: LocaleCode[];
238238
fast?: boolean;
239-
},
239+
}
240240
) {
241241
const responses = await Promise.all(
242242
params.targetLocales.map((targetLocale) =>
243243
this.localizeText(text, {
244244
sourceLocale: params.sourceLocale,
245245
targetLocale,
246246
fast: params.fast,
247-
}),
248-
),
247+
})
248+
)
249249
);
250250

251251
return responses;
@@ -264,7 +264,7 @@ export class LingoDotDevEngine {
264264
async localizeChat(
265265
chat: Array<{ name: string; text: string }>,
266266
params: Z.infer<typeof localizationParamsSchema>,
267-
progressCallback?: (progress: number) => void,
267+
progressCallback?: (progress: number) => void
268268
): Promise<Array<{ name: string; text: string }>> {
269269
const localized = await this._localizeRaw({ chat }, params, progressCallback);
270270

@@ -288,7 +288,7 @@ export class LingoDotDevEngine {
288288
async localizeHtml(
289289
html: string,
290290
params: Z.infer<typeof localizationParamsSchema>,
291-
progressCallback?: (progress: number) => void,
291+
progressCallback?: (progress: number) => void
292292
): Promise<string> {
293293
const jsdomPackage = await import("jsdom");
294294
const { JSDOM } = jsdomPackage;
@@ -320,7 +320,7 @@ export class LingoDotDevEngine {
320320
}
321321

322322
const siblings = Array.from(parent.childNodes).filter(
323-
(n) => n.nodeType === 1 || (n.nodeType === 3 && n.textContent?.trim()),
323+
(n) => n.nodeType === 1 || (n.nodeType === 3 && n.textContent?.trim())
324324
);
325325
const index = siblings.indexOf(current);
326326
if (index !== -1) {
@@ -386,7 +386,7 @@ export class LingoDotDevEngine {
386386

387387
for (const index of indices) {
388388
const siblings = Array.from(parent.childNodes).filter(
389-
(n) => n.nodeType === 1 || (n.nodeType === 3 && n.textContent?.trim()),
389+
(n) => n.nodeType === 1 || (n.nodeType === 3 && n.textContent?.trim())
390390
);
391391
current = siblings[parseInt(index)] || null;
392392
if (current?.nodeType === 1) {

0 commit comments

Comments
 (0)