Refactor Encounters, use new translations format for save space

This commit is contained in:
Roman Jaroš 2025-04-17 19:04:49 +02:00
parent 7d9f58650a
commit 908f45eb9b
59 changed files with 103852 additions and 58396 deletions

4
.support/utils/array.ts Normal file
View file

@ -0,0 +1,4 @@
export function splitByIndexes(input: string, indexes: number[]): string[] {
const parts = input.split('_');
return indexes.map((index) => parts[index]).filter((item) => item !== undefined);
}

View file

@ -0,0 +1,23 @@
import axios from 'axios';
export const callTolgee = async <R>(
method: 'GET' | 'POST' | 'PUT' | 'DELETE' = 'GET',
path: string,
params?: any,
data?: any,
) => {
const args = process.argv.slice(2);
const projectId = args[1];
try {
return await axios<R>(`https://translate.romanjaros.local/v2/projects/${projectId}${path}`, {
method,
data,
params,
headers: {
'X-API-KEY': process.env.TOLGEE_PAT as string,
},
});
} catch (e) {
throw e;
}
};

View file

@ -0,0 +1,10 @@
import { TolgeeKey } from '../../types';
import { writeAlsoFemaleTranslation } from '../translation';
export const writeBook = (tolgeeKey: TolgeeKey): string => {
let luaRecord = '';
luaRecord += `addon.data.book["${tolgeeKey.keyName}"] = {\n`;
luaRecord += writeAlsoFemaleTranslation(tolgeeKey.translations);
luaRecord += `}\n`;
return luaRecord;
};

View file

@ -0,0 +1,61 @@
import { TolgeeKey, TreeNode } from '../../types';
import { writeAlsoFemaleTranslation } from '../translation';
export function buildTree(inputs: string[], layer = ''): TreeNode[] {
const root: TreeNode[] = [];
inputs
.filter((input) => new RegExp(`_(${layer})([0-9]{2})$`).test(input))
.forEach((input) => {
const layerIndex = input.split('_').at(-1);
root.push({
key: input,
children: buildTree(inputs, layerIndex),
});
});
return root;
}
export const getEncounterKey = (tolgeeKey: TolgeeKey): string => {
const [, difficulty, instance, type, key] = tolgeeKey.keyName.split('_');
return [tolgeeKey.keyDescription, difficulty, instance, type, key].join('_');
};
export const getEncounterGroupKey = (tolgeeKey: TolgeeKey): string => {
const [, difficulty, instance] = tolgeeKey.keyName.split('_');
return [tolgeeKey.keyDescription, difficulty, instance].join('_');
};
export const writeEncounter = (tolgeeKey: TolgeeKey): string => {
let luaRecord = '';
const keyName = getEncounterKey(tolgeeKey);
luaRecord += `addon.data.encounter["${keyName}"] = {\n`;
luaRecord += writeAlsoFemaleTranslation(tolgeeKey.translations);
luaRecord += `}\n`;
return luaRecord;
};
const writeStructure = (structure: TreeNode[], layer = 0): string => {
let luaRecord = '';
const tab = '\t'.repeat(layer);
const layerIndex = layer + 1;
structure.forEach((node) => {
const hasEmptyChildren = node.children.length === 0;
luaRecord += `${tab}{\n`;
luaRecord += `\t${tab}key = "${node.key}",\n`;
luaRecord += `\t${tab}children = {${hasEmptyChildren ? '' : '\n'}`;
luaRecord += `${writeStructure(node.children, layerIndex)}`;
luaRecord += `${hasEmptyChildren ? '' : `\t${tab}`}},\n`;
luaRecord += `${tab}},\n`;
});
return luaRecord;
};
export const writeEncounterStructure = (key: string, structure: TreeNode[]): string => {
let luaRecord = '';
luaRecord += `addon.data.encounter["${key}"] = {\n`;
luaRecord += writeStructure(structure);
luaRecord += `}\n`;
return luaRecord;
};

View file

@ -0,0 +1,10 @@
import { TolgeeKey } from '../../types';
import { writeAlsoFemaleTranslation } from '../translation';
export const writeQuest = (tolgeeKey: TolgeeKey): string => {
let luaRecord = '';
luaRecord += `addon.data.quest["${tolgeeKey.keyName}"] = {\n`;
luaRecord += writeAlsoFemaleTranslation(tolgeeKey.translations);
luaRecord += `}\n`;
return luaRecord;
};

View file

@ -0,0 +1,10 @@
import { TolgeeKey } from '../../types';
import { normalize, writeMaleTranslation } from '../translation';
export const writeSpeech = (tolgeeKey: TolgeeKey): string => {
let luaRecord = '';
luaRecord += `addon.data.speech["${normalize(tolgeeKey.keyDescription)}_${tolgeeKey.keyName}"] = {\n`;
luaRecord += writeMaleTranslation(tolgeeKey.translations);
luaRecord += `}\n`;
return luaRecord;
};

View file

@ -0,0 +1,30 @@
import { TolgeeKey } from '../types';
export const normalize = (translation: string | null | undefined) => {
if (translation == null) {
return '';
}
return translation.replace(/\n/g, '\\n').replace(/"/g, "'");
};
export const writeMaleTranslation = (value: TolgeeKey['translations']) => {
let text = '';
const normalizedValue = normalize(value?.['cs']?.text);
if (normalizedValue != '') {
text += '\tm = "' + normalizedValue + '", \n';
}
return text;
};
export const writeAlsoFemaleTranslation = (value: TolgeeKey['translations']) => {
let text = '';
const normalizedMaleValue = normalize(value?.['cs']?.text);
const normalizedFemaleValue = normalize(value?.['csf']?.text);
if (normalizedMaleValue != '') {
text += '\tm = "' + normalizedMaleValue + '", \n';
}
if (normalizedFemaleValue != '' && normalizedMaleValue != normalizedFemaleValue) {
text += '\tf = "' + normalizedFemaleValue + '", \n';
}
return text;
};