diff --git a/README.md b/README.md index b7a7b874..2a29ceb4 100644 --- a/README.md +++ b/README.md @@ -71,4 +71,11 @@ Each file starts with 1 or 2 objects, each with 1 key each. The first object key | `long_description` | A longer, more detailed, description of the error. Intended to be used by the website. Markdown | | `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 | \ No newline at end of file +| `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` \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 00000000..34262a1b --- /dev/null +++ b/index.js @@ -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 +}; \ No newline at end of file