LibreChat/config/translations/instructions.ts
Danny Avila 2ec821ea4c
🌍 : Updated Translations & AI Generation Scripts (#2666)
* chore: bun scripts

* feat: comparisons

* refactor: move scripts to own folder

* feat: generated prompts script and Es output

* feat: generated prompts

* created prompts

* feat: Russian localization prompts

* translation setup

* additional ES translations

* additional ES translations

* translation services

* feat: additional translations

* fix regex for parseParamPrompt

* RU translations

* remove stores from git

* update gitignore

* update gitignore

* ZH translations

* move gen prompt output location

* ZH traditional translations

* AR translations

* chore: rename

* JP

* cleanup scripts

* add additional instruction prompts

* fix translation prompt and add DE

* FR translations (rate limited so not complete)

* chore: update translation comparisons

* chore: remove unused AnthropicClient changes

* refactor: use compositional styling for archive/delete buttons, fix manage archive table styling
2024-05-10 15:56:25 -04:00

75 lines
2.8 KiB
TypeScript

import fs from 'fs';
import path from 'path';
const baseDirPath = './client/src/localization/languages';
const promptsDirPath = './client/src/localization/prompts/instructions';
async function ensureDirectoryExists(directory: string) {
return fs.promises.access(directory).catch(() => fs.promises.mkdir(directory, { recursive: true }));
}
// Helper function to generate Markdown from an object, recursively if needed
function generateMarkdownFromObject(obj: any, depth: number = 0): string {
if (typeof obj !== 'object' || obj === null) {
return String(obj);
}
const indent = ' '.repeat(depth * 2);
return Object.entries(obj)
.map(([key, value]) => {
if (typeof value === 'object') {
return `\n${indent}- **${key}**:${generateMarkdownFromObject(value, depth + 1)}`;
}
return `${key === 'english' ? '\n' : ''}${indent}- **${key}**: ${value}`;
})
.join('\n');
}
async function generatePromptForFile(filePath: string, fileName: string) {
const modulePath = path.resolve(filePath); // Ensuring path is correctly resolved
const fileModule = await import(modulePath); // Dynamically importing the file as a module
let comparisonsMarkdown = '';
if (fileModule.comparisons) {
comparisonsMarkdown = generateMarkdownFromObject(fileModule.comparisons);
} else {
comparisonsMarkdown = 'No comparisons object found.';
}
// Creating markdown content
const promptContent = `# Instructions for Translation
Write a prompt that is mindful of the nuances in the language with respect to its English counterpart, which serves as the baseline for translations. Here are the comparisons between the language translations and their English counterparts:
${comparisonsMarkdown}
Please consider the above comparisons to enhance understanding and guide improvements in translations.
Provide insights or suggestions that could help refine the translation process, focusing on cultural and contextual relevance.
Please craft a prompt that can be used to better inform future translations to this language.
Write this prompt in the translated language, with all its nuances detected, not in the English.
`;
return promptContent;
}
async function createPromptsForTranslations() {
await ensureDirectoryExists(promptsDirPath);
const files = await fs.promises.readdir(baseDirPath);
for (const file of files) {
if (!file.includes('Eng.ts')) { // Ensure English or base file is excluded
const filePath = path.join(baseDirPath, file);
const promptContent = await generatePromptForFile(filePath, file);
const outputFilePath = path.join(promptsDirPath, `${path.basename(file, '.ts')}.md`);
await fs.promises.writeFile(outputFilePath, promptContent);
console.log(`Prompt created for: ${file}`);
}
}
}
createPromptsForTranslations().then(() => console.log('Prompts generation completed.'));