-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
472f0f0
commit 59779bf
Showing
3 changed files
with
121 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,95 +1,121 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
import fs from "fs"; | ||
import path from "path"; | ||
const fallbackLanguage = "en_us"; | ||
|
||
const baseDirectory = "bot"; | ||
const languages = require('./bot/languages.json'); | ||
import languages from "./bot/languages.json" with { type: "json" }; | ||
|
||
const languagesLocation = path.join(process.cwd(), "languages", baseDirectory); | ||
const activeLanguagesLocation = path.join(process.cwd(), "languages", "active", baseDirectory); | ||
const activeLanguagesLocation = path.join( | ||
process.cwd(), | ||
"languages", | ||
"active", | ||
baseDirectory | ||
); | ||
|
||
// relative file path is the path after the locale directory | ||
function loadFile(locale, relativeFilePath) { | ||
|
||
const filePath = path.join(languagesLocation, locale, relativeFilePath); | ||
|
||
try { | ||
let data; | ||
if (!fs.existsSync(filePath)) | ||
data = "{}"; | ||
else | ||
data = fs.readFileSync(filePath, 'utf8'); | ||
const jsonData = JSON.parse(data); | ||
const fallbackLanguageData = JSON.parse(fs.readFileSync(path.join(languagesLocation, fallbackLanguage, relativeFilePath), 'utf8')); | ||
function checkFields(obj, path = '') { | ||
Object.keys(obj).forEach(key => { | ||
const value = obj[key]; | ||
const currentPath = path ? `${path}.${key}` : key; | ||
|
||
if (typeof value === 'string') { | ||
let pointer = fallbackLanguageData; | ||
const structPath = currentPath.split("."); | ||
for (let i = 0; i < structPath.length - 1; i++) { | ||
pointer = pointer[structPath[i]]; | ||
} | ||
pointer[structPath[structPath.length - 1]] = value; | ||
} else if (typeof value === 'object' && value !== null) { | ||
checkFields(value, currentPath); | ||
} | ||
}); | ||
const filePath = path.join(languagesLocation, locale, relativeFilePath); | ||
|
||
try { | ||
let data; | ||
if (!fs.existsSync(filePath)) data = "{}"; | ||
else data = fs.readFileSync(filePath, "utf8"); | ||
const jsonData = JSON.parse(data); | ||
const fallbackLanguageData = JSON.parse( | ||
fs.readFileSync( | ||
path.join(languagesLocation, fallbackLanguage, relativeFilePath), | ||
"utf8" | ||
) | ||
); | ||
function checkFields(obj, path = "") { | ||
Object.keys(obj).forEach((key) => { | ||
const value = obj[key]; | ||
const currentPath = path ? `${path}.${key}` : key; | ||
|
||
if (typeof value === "string") { | ||
let pointer = fallbackLanguageData; | ||
const structPath = currentPath.split("."); | ||
for (let i = 0; i < structPath.length - 1; i++) { | ||
pointer = pointer[structPath[i]]; | ||
} | ||
pointer[structPath[structPath.length - 1]] = value; | ||
} else if (typeof value === "object" && value !== null) { | ||
checkFields(value, currentPath); | ||
} | ||
|
||
checkFields(jsonData); | ||
|
||
const absolutePathToActiveSubdirectory = path.join(activeLanguagesLocation, locale, relativeFilePath); | ||
|
||
if (!fs.existsSync(path.dirname(absolutePathToActiveSubdirectory))) | ||
fs.mkdirSync(path.dirname(absolutePathToActiveSubdirectory), { recursive: true }); | ||
|
||
fs.writeFileSync(absolutePathToActiveSubdirectory, JSON.stringify(fallbackLanguageData), { recursive: true }); | ||
|
||
} catch (err) { | ||
console.error(`Error processing ${filePath}:`, err); | ||
foundErrors = true; | ||
}); | ||
} | ||
|
||
checkFields(jsonData); | ||
|
||
const absolutePathToActiveSubdirectory = path.join( | ||
activeLanguagesLocation, | ||
locale, | ||
relativeFilePath | ||
); | ||
|
||
if (!fs.existsSync(path.dirname(absolutePathToActiveSubdirectory))) | ||
fs.mkdirSync(path.dirname(absolutePathToActiveSubdirectory), { | ||
recursive: true, | ||
}); | ||
|
||
fs.writeFileSync( | ||
absolutePathToActiveSubdirectory, | ||
JSON.stringify(fallbackLanguageData), | ||
{ recursive: true } | ||
); | ||
} catch (err) { | ||
console.error(`Error processing ${filePath}:`, err); | ||
foundErrors = true; | ||
} | ||
} | ||
|
||
function loadSubdirectory(locale, relativePath) { | ||
const absolutePath = path.join(languagesLocation, fallbackLanguage, relativePath); | ||
const subdirectories = fs.readdirSync(absolutePath, { withFileTypes: true }) | ||
.filter(dirent => dirent.isDirectory()) | ||
.map(dirent => dirent.name); | ||
const subfiles = fs.readdirSync(absolutePath, { withFileTypes: true }) | ||
.filter(dirent => !dirent.isDirectory()) | ||
.map(dirent => dirent.name); | ||
for (let i = 0; i < subdirectories.length; i++) { | ||
const subdirectoryPath = path.join(relativePath, subdirectories[i]); | ||
loadSubdirectory(locale, subdirectoryPath); | ||
} | ||
for (let i = 0; i < subfiles.length; i++) { | ||
const filePath = path.join(relativePath, subfiles[i]); | ||
loadFile(locale, filePath); | ||
} | ||
const absolutePath = path.join( | ||
languagesLocation, | ||
fallbackLanguage, | ||
relativePath | ||
); | ||
const subdirectories = fs | ||
.readdirSync(absolutePath, { withFileTypes: true }) | ||
.filter((dirent) => dirent.isDirectory()) | ||
.map((dirent) => dirent.name); | ||
const subfiles = fs | ||
.readdirSync(absolutePath, { withFileTypes: true }) | ||
.filter((dirent) => !dirent.isDirectory()) | ||
.map((dirent) => dirent.name); | ||
for (let i = 0; i < subdirectories.length; i++) { | ||
const subdirectoryPath = path.join(relativePath, subdirectories[i]); | ||
loadSubdirectory(locale, subdirectoryPath); | ||
} | ||
for (let i = 0; i < subfiles.length; i++) { | ||
const filePath = path.join(relativePath, subfiles[i]); | ||
loadFile(locale, filePath); | ||
} | ||
} | ||
|
||
function activate() { | ||
Object.entries(languages).forEach(([key, value]) => { | ||
const directory = path.join(process.cwd(), "languages", baseDirectory, value); | ||
console.log(`Processing language code ${key}...`); | ||
if (!fs.existsSync(directory)) { | ||
console.log(`Skipping non-existent directory: ${directory} for language code ${key}`); | ||
return; | ||
} | ||
|
||
if (!fs.existsSync(activeLanguagesLocation)) | ||
fs.mkdirSync(activeLanguagesLocation, { recursive: true }); | ||
|
||
console.log(`Copying ${directory}...`); | ||
loadSubdirectory(value, "/"); | ||
Object.entries(languages).forEach(([key, value]) => { | ||
const directory = path.join( | ||
process.cwd(), | ||
"languages", | ||
baseDirectory, | ||
value | ||
); | ||
console.log(`Processing language code ${key}...`); | ||
if (!fs.existsSync(directory)) { | ||
console.log( | ||
`Skipping non-existent directory: ${directory} for language code ${key}` | ||
); | ||
return; | ||
} | ||
|
||
}); | ||
if (!fs.existsSync(activeLanguagesLocation)) | ||
fs.mkdirSync(activeLanguagesLocation, { recursive: true }); | ||
|
||
console.log(`Copying ${directory}...`); | ||
loadSubdirectory(value, "/"); | ||
}); | ||
} | ||
|
||
module.exports = activate; | ||
export default activate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
const activate = require("./activate"); | ||
|
||
module.exports = { | ||
activate | ||
}; | ||
export { default as activate } from "./activate.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "languages", | ||
"version": "1.0.0", | ||
"description": "Languages for Quark", | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/quark-bot-discord/languages.git" | ||
}, | ||
"author": "Quark", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/quark-bot-discord/languages/issues" | ||
}, | ||
"homepage": "https://github.com/quark-bot-discord/languages#readme" | ||
} |