40 lines
1.5 KiB
TypeScript
40 lines
1.5 KiB
TypeScript
import fs from 'fs';
|
|
import path from 'path';
|
|
import { NextApiRequest, NextApiResponse } from 'next';
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
const { locale, type } = req.query;
|
|
const baseFilePath = path.join(process.cwd(), `content/i18n/${locale}.json`);
|
|
const modifiedFilePath = path.join(process.cwd(), `content/i18n/${locale}.modified.json`);
|
|
|
|
let translations = {};
|
|
|
|
try {
|
|
translations = JSON.parse(fs.readFileSync(baseFilePath, 'utf8'));
|
|
if (fs.existsSync(modifiedFilePath)) {
|
|
const modifiedTranslations = JSON.parse(fs.readFileSync(modifiedFilePath, 'utf8'));
|
|
translations = { ...translations, ...modifiedTranslations };
|
|
}
|
|
} catch (error) {
|
|
console.error('Error loading translation files:', error);
|
|
}
|
|
|
|
switch (req.method) {
|
|
case 'GET':
|
|
res.status(200).json(translations);
|
|
break;
|
|
case 'POST':
|
|
try {
|
|
fs.writeFileSync(modifiedFilePath, JSON.stringify(req.body, null, 2), 'utf8');
|
|
res.status(200).json({ status: 'Updated' });
|
|
} catch (error) {
|
|
console.error('Error writing modified translation file:', error);
|
|
res.status(500).json({ error: 'Failed to update translation file' });
|
|
}
|
|
break;
|
|
default:
|
|
res.setHeader('Allow', ['GET', 'POST']);
|
|
res.status(405).end(`Method ${req.method} Not Allowed`);
|
|
}
|
|
}
|