Use external locale resource funcs
This commit is contained in:
@@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,58 +62,24 @@ 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();
|
||||||
}
|
}
|
||||||
|
|
||||||
// ===========================================================================
|
// ===========================================================================
|
||||||
|
|
||||||
function showLocaleListCommand(command, params, client) {
|
function showLocaleListCommand(command, params, client) {
|
||||||
let localeList = getLocales().map(function(x) { return x[0]; });
|
let localeList = getLocales().map(function (x) { return x[0]; });
|
||||||
let chunkedList = splitArrayIntoChunks(localeList, 10);
|
let chunkedList = splitArrayIntoChunks(localeList, 10);
|
||||||
|
|
||||||
messagePlayerInfo(client, getLocaleString(client, "HeaderLocaleList"));
|
messagePlayerInfo(client, getLocaleString(client, "HeaderLocaleList"));
|
||||||
for(let i in chunkedList) {
|
for (let i in chunkedList) {
|
||||||
messagePlayerInfo(client, chunkedList[i].join(", "));
|
messagePlayerInfo(client, chunkedList[i].join(", "));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -155,14 +87,14 @@ function showLocaleListCommand(command, params, client) {
|
|||||||
// ===========================================================================
|
// ===========================================================================
|
||||||
|
|
||||||
function setLocaleCommand(command, params, client) {
|
function setLocaleCommand(command, params, client) {
|
||||||
if(areParamsEmpty(params)) {
|
if (areParamsEmpty(params)) {
|
||||||
messagePlayerSyntax(client, getCommandSyntaxText(command));
|
messagePlayerSyntax(client, getCommandSyntaxText(command));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let localeId = getLocaleFromParams(params);
|
let localeId = getLocaleFromParams(params);
|
||||||
|
|
||||||
if(!getLocaleData(localeId)) {
|
if (!getLocaleData(localeId)) {
|
||||||
messagePlayerInfo(client, getLocaleString(client, "InvalidLocale"));
|
messagePlayerInfo(client, getLocaleString(client, "InvalidLocale"));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -176,7 +108,7 @@ function setLocaleCommand(command, params, client) {
|
|||||||
// ===========================================================================
|
// ===========================================================================
|
||||||
|
|
||||||
function getLocaleData(localeId) {
|
function getLocaleData(localeId) {
|
||||||
if(typeof getLocales()[localeId] != "undefined") {
|
if (typeof getLocales()[localeId] != "undefined") {
|
||||||
return getLocales()[localeId];
|
return getLocales()[localeId];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,12 +136,12 @@ function reloadLocaleConfigurationCommand(command, params, client) {
|
|||||||
|
|
||||||
async function translateMessage(messageText, translateFrom = getGlobalConfig().locale.defaultLanguageId, translateTo = getGlobalConfig().locale.defaultLanguageId) {
|
async function translateMessage(messageText, translateFrom = getGlobalConfig().locale.defaultLanguageId, translateTo = getGlobalConfig().locale.defaultLanguageId) {
|
||||||
return new Promise(resolve => {
|
return new Promise(resolve => {
|
||||||
if(translateFrom == translateTo) {
|
if (translateFrom == translateTo) {
|
||||||
resolve(messageText);
|
resolve(messageText);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(let i in cachedTranslations[translateFrom][translateTo]) {
|
for (let i in cachedTranslations[translateFrom][translateTo]) {
|
||||||
if(cachedTranslations[translateFrom][translateTo][i][0] == messageText) {
|
if (cachedTranslations[translateFrom][translateTo][i][0] == messageText) {
|
||||||
logToConsole(LOG_DEBUG, `[Translate]: Using existing translation for ${getGlobalConfig().locale.locales[translateFrom].englishName} to ${getGlobalConfig().locale.locales[translateTo].englishName} - (${messageText}), (${cachedTranslations[translateFrom][translateTo][i][1]})`);
|
logToConsole(LOG_DEBUG, `[Translate]: Using existing translation for ${getGlobalConfig().locale.locales[translateFrom].englishName} to ${getGlobalConfig().locale.locales[translateTo].englishName} - (${messageText}), (${cachedTranslations[translateFrom][translateTo][i][1]})`);
|
||||||
resolve(cachedTranslations[translateFrom][translateTo][i][1]);
|
resolve(cachedTranslations[translateFrom][translateTo][i][1]);
|
||||||
return true;
|
return true;
|
||||||
@@ -220,13 +152,13 @@ async function translateMessage(messageText, translateFrom = getGlobalConfig().l
|
|||||||
httpGet(
|
httpGet(
|
||||||
thisTranslationURL,
|
thisTranslationURL,
|
||||||
"",
|
"",
|
||||||
function(data) {
|
function (data) {
|
||||||
data = ArrayBufferToString(data);
|
data = ArrayBufferToString(data);
|
||||||
let translationData = JSON.parse(data);
|
let translationData = JSON.parse(data);
|
||||||
cachedTranslations[translateFrom][translateTo].push([messageText, translationData.responseData.translatedText]);
|
cachedTranslations[translateFrom][translateTo].push([messageText, translationData.responseData.translatedText]);
|
||||||
resolve(translationData.responseData.translatedText);
|
resolve(translationData.responseData.translatedText);
|
||||||
},
|
},
|
||||||
function(data) {
|
function (data) {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user