Skip to content

Commit

Permalink
create activate languages function
Browse files Browse the repository at this point in the history
  • Loading branch information
Starman3787 committed Apr 21, 2024
1 parent 1a6a0ec commit e62a494
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
active/langs/*
!placeholder.txt
89 changes: 89 additions & 0 deletions activate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const fs = require('fs');
const path = require('path');
const langs = require("./languages-conversion.json");
const fallbackLanguage = "en_us";

const baseDirectory = "langs";
const baseDirectoryActive = "active/langs";

function activate() {
Object.entries(langs).forEach(([key, value]) => {

const directory = path.join(baseDirectory, value, "bot", "standard");

if (!fs.existsSync(directory)) {
console.log(`Skipping non-existent directory: ${directory} for language code ${key}`);
return;
}

const files = fs.readdirSync(directory);
if (files.length === 0) {
console.log(`No JSON files found in ${directory}.`);
return;
}

const activeLanguageDirectory = path.join(baseDirectoryActive, value);

if (!fs.existsSync(activeLanguageDirectory))
fs.mkdirSync(activeLanguageDirectory);

const activeLanguageBotDirectory = path.join(baseDirectoryActive, value, "bot");

if (!fs.existsSync(activeLanguageBotDirectory))
fs.mkdirSync(activeLanguageBotDirectory);

const activeLanguageBotStandardDirectory = path.join(baseDirectoryActive, value, "bot", "standard");

if (!fs.existsSync(activeLanguageBotStandardDirectory))
fs.mkdirSync(activeLanguageBotStandardDirectory);

files.forEach(file => {
if (!file.endsWith('.json')) {
console.log(`Skipping non-JSON file: ${file}`);
return;
}

console.log(`Copying ${value}/${file}...`);
const filePath = path.join(directory, file);
try {
const data = fs.readFileSync(filePath, 'utf8');
const jsonData = JSON.parse(data);
const fallbackLanguageData = require(`./${baseDirectory}/${fallbackLanguage}/bot/standard/${file}`);
// console.log(jsonData);
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 activeDirectory = path.join(baseDirectoryActive, value, "bot", "standard");

if (!fs.existsSync(activeDirectory))
fs.mkdirSync(activeDirectory);

fs.writeFileSync(path.join(activeDirectory, file), JSON.stringify(fallbackLanguageData));

} catch (err) {
console.error(`Error processing ${file}:`, err);
foundErrors = true;
}
});

});

}

module.exports = activate;
1 change: 1 addition & 0 deletions active/langs/placeholder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Autogenerated files
5 changes: 5 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const activate = require("./activate");

module.exports = {
activate
};

0 comments on commit e62a494

Please sign in to comment.