Enable multi lingual

This commit is contained in:
Ichitux
2026-04-05 03:11:25 +02:00
parent 1082d36c12
commit 594b50b77f
10 changed files with 272 additions and 238 deletions

View File

@@ -30,7 +30,8 @@ exports.bookRoutes.get('/genres/:genreId', (req, res) => {
// Generate book outline based on genre and idea
exports.bookRoutes.post('/outline', async (req, res) => {
try {
const { genre, idea, title } = req.body;
const { genre, idea, title, language } = req.body;
const targetLang = language && language.startsWith('es') ? 'Spanish' : 'English';
if (!genre || !idea) {
return res.status(400).json({ error: 'genre and idea are required' });
}
@@ -45,6 +46,8 @@ Core Idea: ${idea}
Generate a detailed chapter outline following the structure: ${template.structure.join(' → ')}
IMPORTANT: The response MUST be written in ${targetLang}. All text values (title, logline, chapter summaries, etc) must be translated to ${targetLang}. Keep the exact JSON KEYS in English.
Return the response in JSON format:
{
"title": "Book Title",
@@ -101,7 +104,8 @@ Return the response in JSON format:
// Generate a chapter based on outline
exports.bookRoutes.post('/chapter', async (req, res) => {
try {
const { genre, chapterTitle, chapterSummary, previousContent } = req.body;
const { genre, chapterTitle, chapterSummary, previousContent, language } = req.body;
const targetLang = language && language.startsWith('es') ? 'Spanish' : 'English';
if (!genre || !chapterTitle || !chapterSummary) {
return res.status(400).json({ error: 'genre, chapterTitle, and chapterSummary are required' });
}
@@ -112,6 +116,8 @@ Chapter: ${chapterTitle}
Summary: ${chapterSummary}
Tone: ${template.defaults.tone}
POV: ${template.defaults.pov}
IMPORTANT: The entire chapter content MUST be written strictly in ${targetLang}.
`;
if (previousContent) {
prompt += `\n\nPrevious content for context:\n${previousContent.substring(0, 2000)}...`;
@@ -146,7 +152,8 @@ POV: ${template.defaults.pov}
// Expand or refine text
exports.bookRoutes.post('/expand', async (req, res) => {
try {
const { text, instruction = 'Expand and improve this text' } = req.body;
const { text, instruction = 'Expand and improve this text', language } = req.body;
const targetLang = language && language.startsWith('es') ? 'Spanish' : 'English';
if (!text) {
return res.status(400).json({ error: 'text is required' });
}
@@ -155,7 +162,7 @@ exports.bookRoutes.post('/expand', async (req, res) => {
Original text:
${text}
Provide an expanded and improved version:`;
Provide an expanded and improved version. IMPORTANT: Write the expanded text entirely in ${targetLang}:`;
const response = await axios_1.default.post('https://openrouter.ai/api/v1/chat/completions', {
model: 'nvidia/nemotron-3-nano-30b-a3b:free',
messages: [{ role: 'user', content: prompt }],
@@ -183,7 +190,8 @@ Provide an expanded and improved version:`;
// Generate character suggestions
exports.bookRoutes.post('/characters', async (req, res) => {
try {
const { genre, storyIdea } = req.body;
const { genre, storyIdea, language } = req.body;
const targetLang = language && language.startsWith('es') ? 'Spanish' : 'English';
if (!genre || !storyIdea) {
return res.status(400).json({ error: 'genre and storyIdea are required' });
}
@@ -193,6 +201,8 @@ exports.bookRoutes.post('/characters', async (req, res) => {
Story Idea: ${storyIdea}
Genre conventions: ${template.description}
IMPORTANT: Write the names, traits, motivation, and backstory values completely in ${targetLang}. Keep the required JSON keys strictly in English.
Return ONLY a valid JSON array with this exact format:
[
{
@@ -252,7 +262,8 @@ Do not include any text outside the JSON array.`;
// Generate plot suggestions
exports.bookRoutes.post('/plot', async (req, res) => {
try {
const { genre, currentPlot, issue } = req.body;
const { genre, currentPlot, issue, language } = req.body;
const targetLang = language && language.startsWith('es') ? 'Spanish' : 'English';
if (!genre || !currentPlot) {
return res.status(400).json({ error: 'genre and currentPlot are required' });
}
@@ -267,7 +278,7 @@ ${issue ? `Specific Issue: ${issue}` : 'Suggest plot developments and twists.'}
Consider genre conventions: ${template.description}
Structure: ${template.structure.join(' → ')}
Provide specific, actionable plot suggestions:`;
IMPORTANT: Provide all specific, actionable plot suggestions completely in ${targetLang}:`;
const response = await axios_1.default.post('https://openrouter.ai/api/v1/chat/completions', {
model: 'nvidia/nemotron-3-nano-30b-a3b:free',
messages: [{ role: 'user', content: prompt }],