Skip to content

Commit cdef5b7

Browse files
feat: added format specific methods to @replexica/sdk (#264)
1 parent 62c464d commit cdef5b7

File tree

5 files changed

+65
-6
lines changed

5 files changed

+65
-6
lines changed

.changeset/fair-roses-give.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@replexica/compiler": minor
3+
"@replexica/cli": minor
4+
"@replexica/sdk": minor
5+
"replexica": minor
6+
---
7+
8+
added format specific methods to `@replexica/sdk`

packages/cli/src/cli/i18n.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,13 +218,12 @@ export default new Command()
218218
referencePayload[i18nConfig.locale.extraSource] = extraSourcePayload;
219219
}
220220

221-
processedPayload = await replexicaEngine.localize(
221+
processedPayload = await replexicaEngine.localizeObject(
222222
finalPayload,
223223
{
224224
sourceLocale: i18nConfig.locale.source,
225225
targetLocale: targetLocale,
226226
},
227-
referencePayload,
228227
(progress) => {
229228
localeOra.text = `(${progress}%) AI localization in progress...`;
230229
}

packages/compiler/src/unplg/workers/locale-server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ function createCachedRemoteLocalizer() {
4545
// create a new cache subobject from the translated changed dictionary
4646
const dictionaryCacheUpdate = _.mapValues(translatedChangedDictionary, hashValue);
4747
// merge the dictionary cache update with the existing dictionary cache
48-
const newDictionaryCache = { ...dictionaryCache, ...dictionaryCacheUpdate };
48+
const newDictionaryCache: Record<string, any> = { ...dictionaryCache, ...dictionaryCacheUpdate };
4949
// store the new dictionary cache
5050
cache.set(cacheKey, newDictionaryCache);
5151
// return the final dictionary
@@ -65,7 +65,7 @@ function createRemoteLocalizer() {
6565
return dictionary;
6666
}
6767

68-
const data = await rplx.localize(dictionary, {
68+
const data = await rplx.localizeObject(dictionary, {
6969
sourceLocale,
7070
targetLocale,
7171
});

packages/sdk/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const replexica = new ReplexicaEngine({
4848
});
4949

5050
// Localize a JSON payload
51-
const localizedContent = await replexica.localize(
51+
const localizedContent = await replexica.localizeObject(
5252
{ myKey: 'Hello, world!' },
5353
{ sourceLocale: 'en', targetLocale: 'es' },
5454
(progress) => console.log(`Localization progress: ${progress}%`) // Optional progress callback

packages/sdk/src/index.ts

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class ReplexicaEngine {
5656
* @param progressCallback - Optional callback function to report progress
5757
* @returns Localized content
5858
*/
59-
async localize(
59+
async _localizeRaw(
6060
payload: Z.infer<typeof payloadSchema>,
6161
params: Z.infer<typeof localizationParamsSchema>,
6262
reference?: Z.infer<typeof referenceSchema>,
@@ -178,4 +178,56 @@ export class ReplexicaEngine {
178178
return 0;
179179
}
180180
}
181+
182+
/**
183+
* Localize a typical JavaScript object
184+
* @param obj - The object to be localized
185+
* @param params - Localization parameters
186+
* @param progressCallback - Optional callback function to report progress
187+
* @returns Localized object
188+
*/
189+
async localizeObject(
190+
obj: Record<string, any>,
191+
params: Z.infer<typeof localizationParamsSchema>,
192+
progressCallback?: (progress: number) => void
193+
): Promise<Record<string, any>> {
194+
return this._localizeRaw(obj, params, undefined, progressCallback);
195+
}
196+
197+
/**
198+
* Localize a text document
199+
* @param textDocument - The text to be localized
200+
* @param params - Localization parameters
201+
* @param progressCallback - Optional callback function to report progress
202+
* @returns Localized text
203+
*/
204+
async localizeDocument(
205+
textDocument: string,
206+
params: Z.infer<typeof localizationParamsSchema>,
207+
progressCallback?: (progress: number) => void
208+
): Promise<string> {
209+
const localized = await this._localizeRaw({ text: textDocument }, params, undefined, progressCallback);
210+
return localized.text || '';
211+
}
212+
213+
/**
214+
* Localize a chat sequence
215+
* @param chat - The chat sequence to be localized
216+
* @param params - Localization parameters
217+
* @param progressCallback - Optional callback function to report progress
218+
* @returns Localized chat sequence
219+
*/
220+
async localizeChat(
221+
chat: Array<{ name: string; text: string }>,
222+
params: Z.infer<typeof localizationParamsSchema>,
223+
progressCallback?: (progress: number) => void
224+
): Promise<Array<{ name: string; text: string }>> {
225+
226+
const localized = await this._localizeRaw({ chat }, params, undefined, progressCallback);
227+
228+
return Object.entries(localized).map(([key, value]) => ({
229+
name: chat[parseInt(key.split('_')[1])].name,
230+
text: value,
231+
}));
232+
}
181233
}

0 commit comments

Comments
 (0)