use modified fallback for translations
This commit is contained in:
@ -1,35 +1,37 @@
|
|||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import { NextApiRequest, NextApiResponse } from 'next';
|
import { NextApiRequest, NextApiResponse } from 'next';
|
||||||
type Translations = {
|
|
||||||
[key: string]: string;
|
|
||||||
};
|
|
||||||
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
||||||
const { locale } = req.query;
|
|
||||||
|
|
||||||
const filePath = path.join(process.cwd(), `content/i18n/${locale}.json`);
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const translations: Translations = JSON.parse(fs.readFileSync(filePath, 'utf8'));
|
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) {
|
switch (req.method) {
|
||||||
case 'GET':
|
case 'GET':
|
||||||
|
res.status(200).json(translations);
|
||||||
|
break;
|
||||||
|
case 'POST':
|
||||||
try {
|
try {
|
||||||
res.status(200).json(translations);
|
fs.writeFileSync(modifiedFilePath, JSON.stringify(req.body, null, 2), 'utf8');
|
||||||
|
res.status(200).json({ status: 'Updated' });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error reading translation file:', error);
|
console.error('Error writing modified translation file:', error);
|
||||||
res.status(500).json({ error: 'Failed to read translation file' });
|
res.status(500).json({ error: 'Failed to update translation file' });
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'POST': try {
|
|
||||||
const newTranslations = req.body;
|
|
||||||
fs.writeFileSync(filePath, JSON.stringify(newTranslations, null, 2), 'utf8');
|
|
||||||
res.status(200).json({ status: 'Updated' });
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Error writing translation file:', error);
|
|
||||||
res.status(500).json({ error: 'Failed to update translation file' });
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
res.setHeader('Allow', ['GET', 'POST']);
|
res.setHeader('Allow', ['GET', 'POST']);
|
||||||
res.status(405).end(`Method ${req.method} Not Allowed`);
|
res.status(405).end(`Method ${req.method} Not Allowed`);
|
||||||
|
@ -1,26 +1,26 @@
|
|||||||
|
|
||||||
import axiosInstance from '../../../src/axiosSecure';
|
import axiosInstance from '../../../src/axiosSecure';
|
||||||
import common from '../../../src/helpers/common';
|
|
||||||
import { useState, useEffect } from 'react';
|
import { useState, useEffect } from 'react';
|
||||||
import { Availability, UserRole } from "@prisma/client";
|
import ProtectedRoute from "../../../components/protectedRoute";
|
||||||
import ProtectedRoute, { serverSideAuth } from "../../../components/protectedRoute";
|
import { UserRole } from "@prisma/client";
|
||||||
|
|
||||||
|
// Simulate importing locales from a config or define directly here
|
||||||
|
const locales = ['en', 'bg', 'ru'];
|
||||||
|
|
||||||
const AdminTranslations = () => {
|
const AdminTranslations = () => {
|
||||||
|
|
||||||
const [translations, setTranslations] = useState({});
|
const [translations, setTranslations] = useState({});
|
||||||
const [locale, setLocale] = useState('en');
|
const [locale, setLocale] = useState('en');
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
console.log("Current locale:", locale, "Fetching translations...");
|
|
||||||
axiosInstance.get(`/api/translations/${locale}`).then(res => setTranslations(res.data));
|
axiosInstance.get(`/api/translations/${locale}`).then(res => setTranslations(res.data));
|
||||||
}, [locale]);
|
}, [locale]);
|
||||||
|
|
||||||
const handleSave = () => {
|
const handleSave = () => {
|
||||||
axiosInstance.post(`/api/translations/${locale}`, translations).then(res => {
|
axiosInstance.post(`/api/translations/${locale}/modified`, translations)
|
||||||
if (res.data.status === 'Updated') {
|
.then(res => {
|
||||||
alert('Translations updated!');
|
if (res.data.status === 'Updated') {
|
||||||
}
|
alert('Translations updated!');
|
||||||
});
|
}
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleChange = (key, value) => {
|
const handleChange = (key, value) => {
|
||||||
@ -28,30 +28,34 @@ const AdminTranslations = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<ProtectedRoute allowedRoles={[UserRole.ADMIN, UserRole.POWERUSER]}>
|
||||||
<ProtectedRoute allowedRoles={[UserRole.ADMIN, UserRole.POWERUSER]}>
|
<div>
|
||||||
<div>
|
<h1>Edit Translations</h1>
|
||||||
<h1>Edit Translations</h1>
|
<select onChange={e => setLocale(e.target.value)} value={locale}>
|
||||||
<select onChange={e => setLocale(e.target.value)} value={locale}>
|
{locales.map(l => (
|
||||||
<option value="bg">BG</option>
|
<option key={l} value={l}>{l.toUpperCase()}</option>
|
||||||
<option value="en">English</option>
|
|
||||||
<option value="ru">RU</option>
|
|
||||||
{/* Add more options as needed */}
|
|
||||||
</select>
|
|
||||||
{Object.keys(translations).map(key => (
|
|
||||||
<div key={key}>
|
|
||||||
<label>{key}:</label>
|
|
||||||
<input
|
|
||||||
type="text"
|
|
||||||
value={translations[key]}
|
|
||||||
onChange={e => handleChange(key, e.target.value)}
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
))}
|
))}
|
||||||
<button onClick={handleSave}>Save Changes</button>
|
</select>
|
||||||
</div>
|
<table>
|
||||||
</ProtectedRoute>
|
<tbody>
|
||||||
</>
|
{Object.entries(translations).map(([key, value]) => (
|
||||||
|
<tr key={key}>
|
||||||
|
<td>{key}</td>
|
||||||
|
<td>
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
value={value}
|
||||||
|
onChange={e => handleChange(key, e.target.value)}
|
||||||
|
style={{ width: '100%' }}
|
||||||
|
/>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
))}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<button onClick={handleSave}>Save Changes</button>
|
||||||
|
</div>
|
||||||
|
</ProtectedRoute>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user