Start simple translation approach

This commit is contained in:
Adolfo Reyna
2025-02-21 00:12:17 -05:00
parent 7902b75b5c
commit 1056a91c33
3 changed files with 53 additions and 13 deletions

View File

@@ -65,13 +65,21 @@ DB.getDB.then((DB) => {
// route for Home-Page
app.get('/', sessionChecker, async (req, res) => {
if (req.userInfo) return res.json({
status: "ok",
userInfo: req.userInfo,
profileInfo: req.profileInfo
});
// This should not happend, since the sessionChecker should redirect to login
res.json({ status: "ok" });
try {
const userInfo = req.userInfo;
if(!userInfo) {
// This should not happend, since the sessionChecker should redirect to login
return res.status(401).json({ status: "Unauthorized" });
}
return res.json({
status: "ok",
userInfo: req.userInfo,
profileInfo: req.profileInfo
});
} catch (error) {
console.error("Error processing / path", error);
return res.status(500).json({ status: "Internal server error" });
}
});
// Check for an invitation for an email
@@ -136,12 +144,24 @@ DB.getDB.then((DB) => {
// This is the endpoint to refresh the push notification token
app.post('/token/', sessionChecker, async (req, res) => {
const profileid = getProfileId(req);
let token = req.body.token
DB.setProfileToken(profileid, token);
return res.json({
status: "ok"
});
try {
const profileid = getProfileId(req);
const { token } = req.body;
// Validate token presence
if (!token) {
return res.status(400).json({ status: 'Token is required' });
}
// Set the token in the database and wait for completion
const result = await DB.setProfileToken(profileid, token);
// Assuming DB.setProfileToken() could fail, handle it appropriately
if (!result) {
return res.status(500).json({ status: 'Failed to update token' });
}
return res.json({ status: 'ok' });
} catch (error) {
console.error('Error setting profile token:', error);
return res.status(500).json({ status: 'Internal server error' });
}
});
// Used for webpush notifications

5
translations/en.json Normal file
View File

@@ -0,0 +1,5 @@
{
"hello": "Hello",
"new_password": "This is your new password: {{password}}",
"login_link": "Log in here"
}

View File

@@ -0,0 +1,15 @@
const path = require('path');
const fs = require('fs');
const loadTranslation = (languageCode) => {
try {
const filePath = path.join(__dirname, 'translations', `${languageCode}.json`);
return JSON.parse(fs.readFileSync(filePath, 'utf8'));
} catch (error) {
console.error('Error loading translation:', error);
if(languageCode !== 'en') {
return loadTranslation('en'); // Fallback to English if the specific language file is missing
}
return {}; // Fallback to empty object if language file is missing
}
};