Use external locale resource funcs

This commit is contained in:
Vortrex
2022-06-14 07:32:40 -05:00
parent 7a8bdcf0bd
commit 5af8770b9c
2 changed files with 31 additions and 120 deletions

View File

@@ -8,53 +8,32 @@
// =========================================================================== // ===========================================================================
function getLocaleString(stringName, ...args) { function getLocaleString(stringName, ...args) {
if(typeof getServerData().localeStrings[localLocaleId][stringName] == undefined) { return findResourceByName("agrp_locale").exports.getLocaleString(localLocaleId, stringName, args);
return "";
} }
let tempString = getServerData().localeStrings[localLocaleId][stringName]; // ===========================================================================
if(tempString == "" || tempString == null || tempString == undefined) { function getGroupedLocaleString(stringName, index, ...args) {
return ""; return findResourceByName("agrp_locale").exports.getGroupedLocaleString(localLocaleId, stringName, index, args);
}
for(let i = 1; i <= args.length; i++) {
tempString = tempString.replace(`{${i}}`, args[i-1]);
}
return tempString;
} }
// =========================================================================== // ===========================================================================
function getAvailableLocaleOptions() { function getAvailableLocaleOptions() {
return getServerData().localeOptions.filter(localeOption => localeOption.requiresUnicode == false); return findResourceByName("agrp_locale").exports.getAvailableLocaleOptions();
} }
// =========================================================================== // ===========================================================================
function loadLocaleConfig() { function getLocales() {
let configFile = loadTextFile("config/client/locale.json"); return findResourceByName("agrp_locale").exports.getLocales();
getServerData().localeOptions = JSON.parse(configFile);
}
// ===========================================================================
function loadAllLocaleStrings() {
let localeOptions = getServerData().localeOptions;
for(let i in localeOptions) {
logToConsole(LOG_INFO, `[VRR.Locale] Loading locale strings for ${localeOptions[i].englishName} (${i})`);
let localeFile = loadTextFile(`locale/${localeOptions[i].stringsFile}`);
let localeData = JSON.parse(localeFile);
getServerData().localeStrings[i] = localeData;
}
} }
// =========================================================================== // ===========================================================================
function setLocale(tempLocaleId) { function setLocale(tempLocaleId) {
logToConsole(LOG_DEBUG, `[VRR.Locale] Setting locale to ${tempLocaleId} (${getServerData().localeOptions[tempLocaleId].englishName})`); let locales = getLocales();
logToConsole(LOG_DEBUG, `[VRR.Locale] Setting locale to ${tempLocaleId} (${locales[tempLocaleId].englishName})`);
localLocaleId = tempLocaleId; localLocaleId = tempLocaleId;
resetGUIStrings(); resetGUIStrings();
} }

View File

@@ -15,76 +15,42 @@ function initLocaleScript() {
// =========================================================================== // ===========================================================================
function getLocaleString(client, stringName, ...args) { function getLocaleString(client, stringName, ...args) {
let tempString = getRawLocaleString(stringName, getPlayerData(client).locale); let localeId = getPlayerData(client).locale;
if(tempString == "" || tempString == null || typeof tempString == "undefined") { if (!findResourceByName("agrp_locale").exports.doesLocaleStringExist(localeId, stringName)) {
logToConsole(LOG_WARN, `[VRR.Locale] Locale string missing for ${stringName} on language ${getLocaleData(getPlayerData(client).locale).englishName}`);
submitBugReport(client, `(AUTOMATED REPORT) Locale string "${stringName}" is missing for "${getPlayerLocaleName(client)}"`); submitBugReport(client, `(AUTOMATED REPORT) Locale string "${stringName}" is missing for "${getPlayerLocaleName(client)}"`);
return ""; return "";
} }
for(let i = 1; i <= args.length; i++) { return findResourceByName("agrp_locale").exports.getLocaleString(localeId, stringName, args);
tempString = tempString.replace(`{${i}}`, args[i-1]);
}
return tempString;
} }
// =========================================================================== // ===========================================================================
function getLanguageLocaleString(localeId, stringName, ...args) { function getLanguageLocaleString(localeId, stringName, ...args) {
let tempString = getRawLocaleString(stringName, localeId); if (!findResourceByName("agrp_locale").exports.doesLocaleStringExist(localeId, stringName)) {
if(tempString == "" || tempString == null || typeof tempString == "undefined") {
logToConsole(LOG_WARN, `[VRR.Locale] Locale string missing for ${stringName} on language ${getLocaleData(getPlayerData(client).locale).englishName}`);
submitBugReport(client, `(AUTOMATED REPORT) Locale string "${stringName}" is missing for "${getPlayerLocaleName(client)}"`); submitBugReport(client, `(AUTOMATED REPORT) Locale string "${stringName}" is missing for "${getPlayerLocaleName(client)}"`);
return "";
} }
for(let i = 1; i <= args.length; i++) { return findResourceByName("agrp_locale").exports.getLocaleString(getPlayerData(client).locale, stringName, args);
tempString = tempString.replace(`{${i}}`, args[i-1]);
}
return tempString;
} }
// =========================================================================== // ===========================================================================
function getGroupedLocaleString(client, stringName, index, ...args) { function getGroupedLocaleString(client, stringName, index, ...args) {
let tempString = getRawGroupedLocaleString(stringName, getPlayerData(client).locale, index); return findResourceByName("agrp_locale").exports.getGroupedLocaleString(getPlayerData(client).locale, stringName, index, args);
for(let i = 1; i <= args.length; i++) {
tempString = tempString.replace(`{${i}}`, args[i-1]);
}
return tempString;
} }
// =========================================================================== // ===========================================================================
function getRawLocaleString(stringName, localeId) { function getRawLocaleString(stringName, localeId) {
return getLocaleStrings()[localeId][stringName]; return findResourceByName("agrp_locale").exports.getRawLocaleString(localeId, stringName);
//if(findResourceByName("agrp_locale").exports.doesLocaleStringExist(localeId, stringName) == false) {
// return "";
//}
//let tempString = findResourceByName("agrp_locale").exports.getRawLocaleString(localeId, stringName);
//if(tempString == "" || tempString == null || tempString == undefined) {
// return "";
//}
} }
// =========================================================================== // ===========================================================================
function getRawGroupedLocaleString(stringName, localeId, index) { function getRawGroupedLocaleString(stringName, localeId, index) {
return getLocaleStrings()[localeId][stringName][index]; return findResourceByName("agrp_locale").exports.getRawGroupedLocaleString(localeId, stringName, index);
//if(findResourceByName("agrp_locale").exports.doesLocaleStringExist(localeId, stringName) == false) {
// return "";
//}
//let tempString = findResourceByName("agrp_locale").exports.getRawLocaleString(localeId, stringName);
//if(tempString == "" || tempString == null || tempString == undefined) {
// return "";
//}
} }
// =========================================================================== // ===========================================================================
@@ -96,48 +62,14 @@ function getPlayerLocaleName(client) {
// =========================================================================== // ===========================================================================
function loadAllLocaleStrings() {
let tempLocaleStrings = {};
let locales = getGlobalConfig().locale.locales;
for(let i in locales) {
let localeData = locales[i];
let localeFile = JSON.parse(loadTextFile(`locale/${localeData.stringsFile}`));
tempLocaleStrings[i] = localeFile;
}
return tempLocaleStrings;
}
// ===========================================================================
function getLocaleStrings() {
return getServerData().localeStrings;
}
// ===========================================================================
function getLocaleFromParams(params) { function getLocaleFromParams(params) {
let locales = getLocales(); return findResourceByName("agrp_locale").exports.getLocaleFromParams(params);
if(isNaN(params)) {
for(let i in locales) {
if(toLowerCase(locales[i].isoCode).indexOf(toLowerCase(params)) != -1) {
return i;
}
if(toLowerCase(locales[i].englishName).indexOf(toLowerCase(params)) != -1) {
return i;
}
}
}
return -1;
} }
// =========================================================================== // ===========================================================================
function getLocales() { function getLocales() {
return getGlobalConfig().locale.locales; return findResourceByName("agrp_locale").exports.getLocales();
} }
// =========================================================================== // ===========================================================================