Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Translations #3

Merged
merged 4 commits into from Feb 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 56 additions & 1 deletion functions.js
Expand Up @@ -10,6 +10,8 @@ const { Permissions } = require("discord.js");
const locales = require("./locales.js");
const literals = require("./literals.js");

const colors = require("colors");

module.exports = {
createError: errorMessage => {
return `\u274c ${errorMessage}`; // \u274c = red cross mark unicode emoji
Expand Down Expand Up @@ -125,6 +127,54 @@ module.exports = {
setTimeout(() => module.exports.setupPresenceTimer(client), 5000);
},

// Temporary function to resolve locale string using new locales schema
resolveLocaleNew: (client, localeString, localeCode) => {
localeString = localeString.toLowerCase().trim();

// Anything else than #locale{<content>} isn't a locale string
if(!localeString.startsWith('#locale{') || !localeString.endsWith('}')) return localeString;

if(!literals.locales.includes(localeCode)) return null; // Could not recognise locale code

if(Object.prototype.hasOwnProperty.call(literals.locales_map, localeCode))
localeCode = literals.locales_map[localeCode]; // No country code encountered, map to default country

// Check a locale string for any matches
const regex = /#locale\{([a-zA-Z0-9_:]+)\}/g;

// Get the content of the brackets and split by colon
let content = regex.exec(localeString)[1].split(':');

// Check if the locale is available
let hasLocale = Object.prototype.hasOwnProperty.call(client.locales.localeStrings, localeCode);

let resolvedLocale = content.reduce((o, i) => {
try {
return o[i];
} catch(e) {
console.error(`Cannot find locale string \`${localeString}\` for language \`${localeCode}\`. Perhaps missing?`);
console.error(e);
return undefined;
}
}, client.locales.locale_strings[localeCode]);

if(!resolvedLocale || resolvedLocale === undefined) {
resolvedLocale = content.reduce((o, i) => {
try {
return o[i];
} catch(e) {
console.error(`Cannot find locale string \`${localeString}\` for language \`${localeCode}\`. Perhaps missing?`);
console.error(e);
return undefined;
}
}, client.locales.locale_strings[client.locales.fallback_locale]);

if(!resolvedLocale || resolvedLocale === undefined) return null; // Undefined, returns to null
}

return resolvedLocale; // Return a resolved locale then
},

// Resolve locale string
resolveLocale: (localeString, localeCode) => {
localeString = localeString.toLowerCase().trim();
Expand All @@ -134,6 +184,9 @@ module.exports = {

if(!literals.locales.includes(localeCode)) return null; // Could not recognise locale code

if(Object.prototype.hasOwnProperty.call(literals.locales_map, localeCode))
localeCode = literals.locales_map[localeCode]; // No country code encountered, map to default country

// Check a locale string for any matches
const regex = /#locale\{([a-zA-Z0-9_:]+)\}/g;

Expand All @@ -145,14 +198,16 @@ module.exports = {
try {
return o[i]; // Decompile string to an object reference
} catch(e) {
console.error(`Cannot find locale string \`${localeString}\` for language \`${localeCode}\`. Perhaps missing?`);
console.error(e);
return undefined; // If reached an unreachable end, return undefined (safe reference)
}
}, locales.locale_strings);

if(!availableLocales || availableLocales.constructor !== Object) return null; // Reached an unreachable reference

// Locale string for this locale code not found => use default locale code
if(!Object.prototype.hasOwnProperty.call(availableLocales, localeString)) return availableLocales[locales.default_locale];
if(!Object.prototype.hasOwnProperty.call(availableLocales, localeCode)) return availableLocales[locales.default_locale];

// Get and return the correct locale string
return availableLocales[localeCode];
Expand Down
6 changes: 4 additions & 2 deletions literals.js
Expand Up @@ -59,7 +59,7 @@ module.exports = {
'ns': 'ns-ZA',
'pa': 'pa-IN',
'pl': 'pl-PL',
'ps': 'ps-AR',
'ps': 'ps-AF',
'pt': 'pt-PT',
'qu': 'qu-PE',
'ro': 'ro-RO',
Expand Down Expand Up @@ -125,6 +125,7 @@ module.exports = {
'da-DK',
'de',
'de-AT',
'de-BE',
'de-CH',
'de-DE',
'de-LI',
Expand Down Expand Up @@ -207,6 +208,7 @@ module.exports = {
'it',
'it-CH',
'it-IT',
'it-MC',
'ja',
'ja-JP',
'ka',
Expand Down Expand Up @@ -251,7 +253,7 @@ module.exports = {
'pl',
'pl-PL',
'ps',
'ps-AR',
'ps-AF',
'pt',
'pt-BR',
'pt-PT',
Expand Down
4 changes: 3 additions & 1 deletion locales/.template.json
Expand Up @@ -26,7 +26,9 @@
"swearguard": {
"warn": "[user][warns]",
"mute": "[user][warns][time]"
}
},
"userjoin": "[user]",
"userleave": "[user]"
},
"#commands": "Translation of commands output pieces and their help files",
"commands": {
Expand Down
4 changes: 3 additions & 1 deletion locales/cs-CZ.json
Expand Up @@ -18,7 +18,9 @@
"swearguard": {
"warn": "Prosím, [user], vyvaruj se použití nadávek. Byl jsi [warns]x varován.",
"mute": "Maximální počet [warns] varování překročen. Na dobu [time] ti byl nasazen roubík, [user]."
}
},
"userjoin": "[user] se přidal k naší komunitě. \ud83d\udc4b Vítej a měj se tu dobře! \ud83d\ude0a",
"userleave": "[user] opustil naší komunitu. \ud83d\ude25 Žij dlouho a blaze. \ud83d\udd96"
},
"commands": {
"whois": {
Expand Down
4 changes: 3 additions & 1 deletion locales/en-GB.json
Expand Up @@ -18,7 +18,9 @@
"swearguard": {
"warn": "Please refrain from swearing [user]. You have been warned [warns] time(s).",
"mute": "Maximum number of [warns] warns exceeded. You have been muted for [time] [user]"
}
},
"userjoin": "[user] has joined our community. \ud83d\udc4b Welcome and have a nice day! \ud83d\ude0a",
"userleave": "[user] has left our community. \ud83d\ude25 Live long and prosper. \ud83d\udd96"
},
"commands": {
"whois": {
Expand Down
4 changes: 3 additions & 1 deletion locales/en-US.json
Expand Up @@ -18,7 +18,9 @@
"swearguard": {
"warn": "Please refrain from swearing [user]. You have been warned [warns] time(s).",
"mute": "Maximum number of [warns] warns exceeded. You have been muted for [time] [user]"
}
},
"userjoin": "[user] has joined our community. \ud83d\udc4b Welcome and have a nice day! \ud83d\ude0a",
"userleave": "[user] has left our community. \ud83d\ude25 Live long and prosper. \ud83d\udd96"
},
"commands": {
"whois": {
Expand Down
9 changes: 1 addition & 8 deletions main.js
Expand Up @@ -64,16 +64,9 @@ client.credentials = {
//dbl_token: process.env.DBL_TOKEN
};

// Setup locale strings
client.locales = {
fallback_locale: "en-US",
supported_locales: {
partially: [],
completely: [
'cs-CZ',
'en-GB',
'en-US'
]
},
locale_strings: {} // Will be filled in programatically
}

Expand Down