mirror of
https://ghfast.top/https://github.com/CxJuice/ScWeb_Chinese_Translate.git
synced 2025-03-10 22:31:25 +08:00
让前端将Json文件自动转换为小写
This commit is contained in:
parent
2df787bc9f
commit
94c0eba717
10
server.js
10
server.js
@ -7,18 +7,18 @@ const PORT = process.env.PORT || 3000;
|
|||||||
const JSON_FOLDER_PATH = path.join(__dirname, 'json');
|
const JSON_FOLDER_PATH = path.join(__dirname, 'json');
|
||||||
const LOCALES_JSON_FOLDER_PATH = path.join(__dirname, 'json/locales');
|
const LOCALES_JSON_FOLDER_PATH = path.join(__dirname, 'json/locales');
|
||||||
|
|
||||||
function transformToLowerCase(obj) {
|
function transformKeysToLowerCase(obj) {
|
||||||
if (typeof obj !== 'object' || obj === null) {
|
if (typeof obj !== 'object' || obj === null) {
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(obj)) {
|
if (Array.isArray(obj)) {
|
||||||
return obj.map(value => transformToLowerCase(value));
|
return obj.map(value => transformKeysToLowerCase(value));
|
||||||
}
|
}
|
||||||
|
|
||||||
let newObj = {};
|
let newObj = {};
|
||||||
for (let key in obj) {
|
for (let key in obj) {
|
||||||
newObj[key.toLowerCase()] = (typeof obj[key] === 'string') ? obj[key].toLowerCase() : transformToLowerCase(obj[key]);
|
newObj[key.toLowerCase()] = transformKeysToLowerCase(obj[key]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return newObj;
|
return newObj;
|
||||||
@ -48,7 +48,7 @@ app.get('/json-files/:filename', (req, res) => {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const jsonData = JSON.parse(data);
|
const jsonData = JSON.parse(data);
|
||||||
res.json(transformToLowerCase(jsonData));
|
res.json(transformKeysToLowerCase(jsonData));
|
||||||
} catch (parseErr) {
|
} catch (parseErr) {
|
||||||
res.status(500).send('Error parsing JSON.');
|
res.status(500).send('Error parsing JSON.');
|
||||||
}
|
}
|
||||||
@ -68,7 +68,7 @@ app.get('/json-files/locales/:filename', (req, res) => {
|
|||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
const jsonData = JSON.parse(data);
|
const jsonData = JSON.parse(data);
|
||||||
res.json(transformToLowerCase(jsonData));
|
res.json(transformKeysToLowerCase(jsonData));
|
||||||
} catch (parseErr) {
|
} catch (parseErr) {
|
||||||
res.status(500).send('Error parsing JSON.');
|
res.status(500).send('Error parsing JSON.');
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user