import { translatedObject } from '..';
import { error, messages, success } from '../utils/console';
import { getFile, getRootFolder, saveFilePublic } from './core';
import { objectTranslator } from './json_object';
import { matchYamlExt } from '../utils/yaml';
import { TranslationConfig } from '../modules/modules';
import { getLanguageKeyFromValue } from '../modules/helpers';
export async function fileTranslator(
TranslationConfig: TranslationConfig,
tempObjectPath: string,
from: string,
to: string[],
newFileName: string
) {
let { jsonObj, objectPath } = await getFileFromPath(tempObjectPath);
if (jsonObj === undefined) {
error(messages.file.no_file_in_path);
return;
}
jsonObj = { data: JSON.parse(jsonObj) };
let oldTranslations = JSON.parse("{}")
let latestPath = objectPath.replace(/\\/g, '/');
const fileExt = getFileExt(latestPath);
let rootFolder = getRootFolder(latestPath);
for (const lang of to) {
let baseFileName = newFileName
? `${newFileName}.${lang}.${fileExt}`
: `${lang}.${fileExt}`;
let fileName = `${rootFolder}/${baseFileName}`;
let response = await getFileFromPath(fileName);
let oldTranslation = response?.jsonObj
try{
if (oldTranslation === undefined) {
console.log(`📋 Cache miss: No existing translation found for ${lang} in ${fileName}`);
oldTranslations[lang] = { data: {} };
} else {
oldTranslation = { data: JSON.parse(oldTranslation) };
oldTranslations[lang] = oldTranslation;
console.log(`📋 Cache hit: Found existing translation for ${lang} in ${fileName}`);
}
} catch{
console.log(`⚠️ Error parsing translation file for ${lang} in ${fileName}`);
oldTranslations[lang] = { data: {} };
}
}
let newJsonObj = await objectTranslator(TranslationConfig, jsonObj, from, to, oldTranslations);
if (newJsonObj === undefined) {
error(messages.file.cannot_translate);
return;
}
(newJsonObj as Array<translatedObject>).forEach(async (element, index) => {
const currentJsonObj = element.data;
let fileName = newFileName
? `/${newFileName}.${to[index]}.${fileExt}`
: `/${to[index]}.${fileExt}`;
await saveFilePublic(rootFolder + fileName, currentJsonObj);
success(
`For ${getLanguageKeyFromValue(
to[index],
TranslationConfig.TranslationModule.languages
)} --> ${fileName} created.`
);
});
}
export async function getFileFromPath(
objectPath: string
): Promise<{ jsonObj: any; objectPath: string }> {
let jsonObj: any = await getFile(objectPath);
if (jsonObj === undefined) {
objectPath = __dirname + '\\' + objectPath;
jsonObj = await getFile(objectPath);
}
return { jsonObj, objectPath };
}
function getFileExt(latestPath: string): string {
const sourceFileMatchYamlExt = matchYamlExt(latestPath);
const fileExt = sourceFileMatchYamlExt || 'json';
return fileExt;
}