Skip to content

Commit 85dfc10

Browse files
feat(sdk): added new sdk methods localizeStringArray (#1049)
* feat(sdk): add new methods in sdk as localizeSimpleMap and localizeStringArray * feat(sdk): adds sdk test for new methods * style(sdk): format sdk using prettier * feat(sdk): removes unwanted sdk methods localizeSimpleMap and its test --------- Co-authored-by: Max Prilutskiy <[email protected]>
1 parent d844def commit 85dfc10

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

.changeset/empty-meals-type.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@lingo.dev/_sdk": minor
3+
"@replexica/sdk": minor
4+
---
5+
6+
Added new methods to the SDK:
7+
8+
1. `localizeStringArray`: Localizes an array of strings while maintaining their order.
9+
10+
Also added comprehensive tests for these methods using Vitest.

packages/sdk/src/index.spec.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,62 @@ describe("ReplexicaEngine", () => {
9191
);
9292
});
9393
});
94+
95+
describe("localizeStringArray", () => {
96+
it("should localize an array of strings and maintain order", async () => {
97+
const engine = new LingoDotDevEngine({ apiKey: "test" });
98+
const mockLocalizeObject = vi.spyOn(engine, "localizeObject");
99+
mockLocalizeObject.mockImplementation(async (obj: any) => {
100+
// Simulate translation by adding 'ES:' prefix to all string values
101+
return Object.fromEntries(
102+
Object.entries(obj).map(([key, value]) => [key, `ES:${value}`]),
103+
);
104+
});
105+
106+
const inputArray = ["Hello", "Goodbye", "How are you?"];
107+
108+
const result = await engine.localizeStringArray(inputArray, {
109+
sourceLocale: "en",
110+
targetLocale: "es",
111+
});
112+
113+
// Verify the mapped object was passed to localizeObject
114+
expect(mockLocalizeObject).toHaveBeenCalledWith(
115+
{
116+
item_0: "Hello",
117+
item_1: "Goodbye",
118+
item_2: "How are you?",
119+
},
120+
{
121+
sourceLocale: "en",
122+
targetLocale: "es",
123+
},
124+
);
125+
126+
// Verify the result maintains the original order
127+
expect(result).toEqual(["ES:Hello", "ES:Goodbye", "ES:How are you?"]);
128+
expect(result).toHaveLength(3);
129+
});
130+
131+
it("should handle empty array", async () => {
132+
const engine = new LingoDotDevEngine({ apiKey: "test" });
133+
const mockLocalizeObject = vi.spyOn(engine, "localizeObject");
134+
mockLocalizeObject.mockImplementation(async () => ({}));
135+
136+
const result = await engine.localizeStringArray([], {
137+
sourceLocale: "en",
138+
targetLocale: "es",
139+
});
140+
141+
expect(mockLocalizeObject).toHaveBeenCalledWith(
142+
{},
143+
{
144+
sourceLocale: "en",
145+
targetLocale: "es",
146+
},
147+
);
148+
149+
expect(result).toEqual([]);
150+
});
151+
});
94152
});

packages/sdk/src/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,31 @@ export class LingoDotDevEngine {
298298
return responses;
299299
}
300300

301+
/**
302+
* Localize an array of strings
303+
* @param strings - An array of strings to be localized
304+
* @param params - Localization parameters:
305+
* - sourceLocale: The source language code (e.g., 'en')
306+
* - targetLocale: The target language code (e.g., 'es')
307+
* - fast: Optional boolean to enable fast mode (faster for bigger batches)
308+
* @returns An array of localized strings in the same order
309+
*/
310+
async localizeStringArray(
311+
strings: string[],
312+
params: Z.infer<typeof localizationParamsSchema>,
313+
): Promise<string[]> {
314+
const mapped = strings.reduce(
315+
(acc, str, i) => {
316+
acc[`item_${i}`] = str;
317+
return acc;
318+
},
319+
{} as Record<string, string>,
320+
);
321+
322+
const result = await this.localizeObject(mapped, params);
323+
return Object.values(result);
324+
}
325+
301326
/**
302327
* Localize a chat sequence while preserving speaker names
303328
* @param chat - Array of chat messages, each with 'name' and 'text' properties

0 commit comments

Comments
 (0)