Skip to content

Commit

Permalink
Fix arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed Jun 3, 2024
1 parent 2c8427f commit ebff701
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 9 deletions.
11 changes: 8 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { normalize } from 'path';
import OpenAI from 'openai';

import { GptTranslateJsonOptions, Translation } from './types';
import { deepSet } from './merge';
import { deepMerge, deepSet } from './merge';
import { getJsonPaths, parseJson, toJsonString } from './json-functions';

/**
Expand Down Expand Up @@ -292,16 +292,21 @@ export async function gptTranslateJson(options: GptTranslateJsonOptions) {
for (const [lang, translatedFilesMap] of translatedMap) {
for (const [filename, translatedData] of translatedFilesMap) {
// Existing or new translation
const originalTranslation = assetsMap.get(resolvedOptions.originalLang)?.get(filename) || {};
const translation: Translation = assetsMap.get(lang)?.get(filename) || {};

// To handle arrays before deep set
const mergedTranslation = deepMerge(originalTranslation, translation);

const keys = Array.from(translatedData.keys());
keys.forEach(key => {
// Set meta
metaPaths.add(key);
// Set keys
deepSet(translation, key.split('.'), translatedData.get(key) || '');
deepSet(mergedTranslation, key.split('.'), translatedData.get(key) || '');
});
// Write
await writeAsset(translation, filename, lang);
await writeAsset(mergedTranslation, filename, lang);
}
// Set meta
metaLangs.add(lang);
Expand Down
29 changes: 28 additions & 1 deletion src/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,34 @@ export function deepSet(target: Translation, keys: string[], val: string | Trans
const len = keys.length;
while (i < len) {
const key = keys[i++];
target[key] = (i === len) ? val : typeof target[key] === 'object' ? target[key] : {};
target[key] = target[key] && !val ?
target[key] : (i === len) ?
val : typeof target[key] === 'object' ?
target[key] : {};
target = target[key];
}
}

export function deepMerge(target: Translation, source: Translation): any {
const output = Object.assign({}, target);

if (isObject(target) && isObject(source)) {
Object.keys(source).forEach((key) => {
if (isObject(source[key])) {
if (!(key in target)) {
Object.assign(output, { [key]: source[key] });
} else {
output[key] = deepMerge(target[key], source[key]);
}
} else {
Object.assign(output, { [key]: source[key] });
}
});
}

return output;
}

function isObject(item: any): boolean {
return typeof item === 'object' && !Array.isArray(item);
}
40 changes: 35 additions & 5 deletions tests/mock.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@
export const mockAsset = JSON.stringify({
"app": {
"greeting": "Hi! I am {{name}}"
"greeting": "Hi! I am {{name}}",
"list": [
"First test",
"Second test"
]
}
}, null, 2);

export const mockTranslatedAsset = JSON.stringify({
"app": {
"greeting": "Ciao! Sono {{name}}"
"greeting": "Ciao! Sono {{name}}",
"list": [
"Prima prova",
"Seconda prova"
]
}
}, null, 2);

export const mockMetaTranslated = JSON.stringify([
"app.greeting"
"app.greeting",
"app.list.0",
"app.list.1"
], null, 2);

export const mockMetaTranslatedLangs = JSON.stringify([
Expand All @@ -22,7 +32,7 @@ export const mockResponse = {
choices: [
{
message: {
content: "[\"Ciao! Sono {{name}}\"]"
content: "[\"Ciao! Sono {{name}}\",\"Prima prova\",\"Seconda prova\"]"
}
}
],
Expand All @@ -34,19 +44,29 @@ export const mockResponse = {
export const mockAddTranslationAsset = JSON.stringify({
"app": {
"greeting": "Hi! I am {{name}}",
"list": [
"First test",
"Second test"
],
"title": "<h1>Library to translate JSON using GPT</h1>"
}
}, null, 2);

export const mockAddTranslationTranslatedAsset = JSON.stringify({
"app": {
"greeting": "Ciao! Sono {{name}}",
"list": [
"Prima prova",
"Seconda prova"
],
"title": "<h1>Libreria per tradurre JSON usando GPT</h1>"
}
}, null, 2);

export const mockAddTranslationMetaTranslated = JSON.stringify([
"app.greeting",
"app.list.0",
"app.list.1",
"app.title"
], null, 2);

Expand All @@ -70,19 +90,29 @@ export const mockAddTranslationResponse = {
export const mockAddLangAsset = JSON.stringify({
"app": {
"greeting": "Hi! I am {{name}}",
"list": [
"First test",
"Second test"
],
"title": "<h1>Library to translate JSON using GPT</h1>"
}
}, null, 2);

export const mockAddLangTranslatedAsset = JSON.stringify({
"app": {
"greeting": "¡Hola! Soy {{name}}",
"list": [
"Primera prueba",
"Segunda prueba"
],
"title": "<h1>Biblioteca para traducir JSON usando GPT</h1>"
}
}, null, 2);

export const mockAddLangMetaTranslated = JSON.stringify([
"app.greeting",
"app.list.0",
"app.list.1",
"app.title"
], null, 2);

Expand All @@ -95,7 +125,7 @@ export const mockAddLangResponse = {
choices: [
{
message: {
content: "[\"¡Hola! Soy {{name}}\",\"<h1>Biblioteca para traducir JSON usando GPT</h1>\"]"
content: "[\"¡Hola! Soy {{name}}\",\"Primera prueba\",\"Segunda prueba\",\"<h1>Biblioteca para traducir JSON usando GPT</h1>\"]"
}
}
],
Expand Down

0 comments on commit ebff701

Please sign in to comment.