feat: added utility methods
This commit is contained in:
parent
8aea965d9b
commit
6df3a240c7
2 changed files with 86 additions and 1 deletions
|
|
@ -72,3 +72,10 @@ Each file starts with 1 or 2 objects, each with 1 key each. The first object key
|
|||
| `short_solution` | A short solution for the error. Intended to be used by the Discord bot. Plain text |
|
||||
| `long_solution` | A longer, more detailed, solution for the error. Intended to be used by the website. Markdown |
|
||||
| `support_link` | Link to a relevant support page for the error. Typically the website, but does not have to be |
|
||||
|
||||
### Utility functions
|
||||
The main purpose of this repository is to maintain the JSON translation files. However 2 utility functions are also included for JavaScript/TypeScript projects.
|
||||
|
||||
- `getModuleInfo(sysmodule, locale)` - Returns the module information for the locale, or `null` if not found
|
||||
- `getErrorInfo(sysmodule, code, locale)` - Returns the error information (including the module information) for the locale. Returns null if either the module or error code is not found
|
||||
- `getAllErrors()` - Returns an array of all error codes for all modules in the form `MODULE-CODE`
|
||||
78
index.js
Normal file
78
index.js
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
const path = require('node:path');
|
||||
const fs = require('node:fs');
|
||||
|
||||
function getModuleInfo(sysmodule, locale) {
|
||||
const localePath = path.join(__dirname, 'data', sysmodule, `${locale.replace('-', '_')}.json`);
|
||||
const defaultPath = path.join(__dirname, 'data', sysmodule, 'en_US.json');
|
||||
let content;
|
||||
|
||||
if (fs.existsSync(localePath)) {
|
||||
content = fs.readFileSync(localePath, {
|
||||
encoding: 'utf8'
|
||||
});
|
||||
} else if (fs.existsSync(defaultPath)) {
|
||||
content = fs.readFileSync(defaultPath, {
|
||||
encoding: 'utf8'
|
||||
});
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
return JSON.parse(content);
|
||||
}
|
||||
|
||||
function getErrorInfo(sysmodule, code, locale) {
|
||||
const moduleInfo = getModuleInfo(sysmodule, locale);
|
||||
|
||||
if (!moduleInfo) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const localePath = path.join(__dirname, 'data', sysmodule, code, `${locale.replace('-', '_')}.json`);
|
||||
const defaultPath = path.join(__dirname, 'data', sysmodule, code, 'en_US.json');
|
||||
let content;
|
||||
|
||||
if (fs.existsSync(localePath)) {
|
||||
content = fs.readFileSync(localePath, {
|
||||
encoding: 'utf8'
|
||||
});
|
||||
} else if (fs.existsSync(defaultPath)) {
|
||||
content = fs.readFileSync(defaultPath, {
|
||||
encoding: 'utf8'
|
||||
});
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
const errorInfo = JSON.parse(content)[sysmodule][code];
|
||||
|
||||
errorInfo.module = moduleInfo[sysmodule];
|
||||
|
||||
return errorInfo;
|
||||
}
|
||||
|
||||
function getAllErrors() {
|
||||
const errors = [];
|
||||
|
||||
const modules = fs.readdirSync(path.join(__dirname, 'data'), { withFileTypes: true });
|
||||
|
||||
for (const moduleDirent of modules) {
|
||||
if (moduleDirent.isDirectory()) {
|
||||
const errorCodes = fs.readdirSync(path.join(__dirname, 'data', moduleDirent.name), { withFileTypes: true });
|
||||
|
||||
for (const errorDirent of errorCodes) {
|
||||
if (errorDirent.isDirectory()) {
|
||||
errors.push(`${moduleDirent.name}-${errorDirent.name}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getModuleInfo,
|
||||
getErrorInfo,
|
||||
getAllErrors
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue