mirror of
https://github.com/DeNNiiInc/dbgate.git
synced 2026-04-23 04:36:00 +00:00
feat: sort translation json keys alphabetically
This commit is contained in:
24
common/sortJsonKeysAlphabetically.js
Normal file
24
common/sortJsonKeysAlphabetically.js
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
// @ts-check
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {object|string} json
|
||||||
|
* @returns {object}
|
||||||
|
*/
|
||||||
|
function sortJsonKeysAlphabetically(json) {
|
||||||
|
const obj = typeof json === 'string' ? JSON.parse(json) : json;
|
||||||
|
|
||||||
|
if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) {
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
const sortedObj = Object.keys(obj)
|
||||||
|
.sort()
|
||||||
|
.reduce((result, key) => {
|
||||||
|
result[key] = obj[key];
|
||||||
|
return result;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
return sortedObj;
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = sortJsonKeysAlphabetically;
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const { defaultLanguage } = require('./constants');
|
const { defaultLanguage } = require('./constants');
|
||||||
|
const sortJsonKeysAlphabetically = require('../sortJsonKeysAlphabetically');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {string} file
|
* @param {string} file
|
||||||
@@ -145,7 +146,9 @@ function getLanguageTranslations(language) {
|
|||||||
*/
|
*/
|
||||||
function setLanguageTranslations(language, translations) {
|
function setLanguageTranslations(language, translations) {
|
||||||
const file = resolveFile(`translations/${language}.json`);
|
const file = resolveFile(`translations/${language}.json`);
|
||||||
fs.writeFileSync(file, JSON.stringify(translations, null, 2));
|
const sorted = sortJsonKeysAlphabetically(translations);
|
||||||
|
|
||||||
|
fs.writeFileSync(file, JSON.stringify(sorted, null, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -155,8 +158,9 @@ function setLanguageTranslations(language, translations) {
|
|||||||
function updateLanguageTranslations(language, newTranslations) {
|
function updateLanguageTranslations(language, newTranslations) {
|
||||||
const translations = getLanguageTranslations(language);
|
const translations = getLanguageTranslations(language);
|
||||||
const updatedTranslations = { ...translations, ...newTranslations };
|
const updatedTranslations = { ...translations, ...newTranslations };
|
||||||
|
const sorted = sortJsonKeysAlphabetically(updatedTranslations);
|
||||||
|
|
||||||
setLanguageTranslations(language, updatedTranslations);
|
setLanguageTranslations(language, sorted);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAllLanguages() {
|
function getAllLanguages() {
|
||||||
|
|||||||
Reference in New Issue
Block a user