File size: 13,925 Bytes
913928f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 | /**
* Author: Peter Dragicevic [peter@petschko.org]
* Authors-Website: https://petschko.org/
* Date: 15.06.2024
* Time: 17:56
*/
var lang;
/**
* Creates a new Language Object
*
* @param {object} languageListFile - Content about the available Languages
* @constructor - Language
*/
function Language(languageListFile) {
this.list = languageListFile.languages;
this.activeIndex = languageListFile.defaultLangIndex;
this.defaultLangIndex = languageListFile.defaultLangIndex;
this.strings = [];
this.init = false;
this.langListOpen = false;
// Misc
this.listenerAddMethod = window.addEventListener ? 'addEventListener' : 'attachEvent';
// Const
this.prefKey = 'rpgmmvd-language';
this.translateClass = 'trl';
this.translateChildClass = 'trl-child';
this.translateableText = 'data-trl';
this.translateableHasChilds = 'data-trl-has-children';
this.translateableChildIndex = 'data-trl-child';
this.translateableAlt = 'data-trl-alt';
this.translateableTitle = 'data-trl-title';
this.translationStylesId = 'translation-styles';
this.languageSwitcherBarId = 'language-switcher';
this.translatorCreditsDivId = 'translator-credits';
this.languageSelectionListContainerId = 'language-selection-list-container';
this.languageSelectionListId = 'language-selection-list';
this.activeLanguageLinkId = 'active-language';
/**
* Removes disabled languages from the List
*/
Language.prototype.removeDisabledLanguages = function() {
var list = [];
for(var i = 0; i < this.list.length; i++) {
if(this.list[i]['enabled'])
list.push(this.list[i]);
}
this.list = list;
}
/**
* Builds the Language-Switcher on the Frontend
*/
Language.prototype.updateLanguageSwitcher = function() {
var that = this;
var langMenuOpenerLink = document.getElementById(this.activeLanguageLinkId);
if(langMenuOpenerLink === null)
return;
// Generate the List
this.generateSwitchLanguageList();
// Translate the active Element
langMenuOpenerLink.innerHTML = this.generateLanguageFlagIcon(this.activeIndex) + ' ' + this.generateLanguageName(this.activeIndex)
+ ' <span id="select-language-icon" class="glyphicon glyphicon-menu-down" aria-hidden="true"></span>';
// Assign action-listener on init for showing/hide the Language-List
if(! this.init) {
langMenuOpenerLink[this.listenerAddMethod](window.addEventListener ? 'click' : 'onclick', function(event) {
that.toggleLanguageList();
event.preventDefault();
}, false);
//langMenuOpenerLink[this.listenerAddMethod](window.addEventListener ? 'focusout' : 'onfocusout', function(event) {
// that.toggleLanguageList(false);
//}, false);
// Preparations done show the bar now except only 1 language exists
if(this.list.length > 1)
this.showLanguageSwitcher(true);
}
}
/**
* Generates the Language-Name for the selected index
*
* @param {int} index - Index to create the name for
* @returns {string} - Full Language-Name
*/
Language.prototype.generateLanguageName = function(index) {
var langName = html(this.list[index]['languageName']);
if(this.list[index]['languageName'] !== this.list[index]['localizedLanguageName'] && this.list[index]['localizedLanguageName'] !== null)
langName += ' (' + html(this.list[index]['localizedLanguageName']) + ')'
return langName;
}
/**
* Generates the Flag-Icon for the selected index
*
* @param {int} index - Index to create the Flag-Icon for
* @returns {string} - Flag-Icon Html
*/
Language.prototype.generateLanguageFlagIcon = function(index) {
if(this.list[index].flagFile === null)
return '';
return '<img src="img/flags/' + html(encodeURI(this.list[index].flagFile).replace('/', '')) + '" alt="' + html(this.list[index].flagAltText) + '" class="flag-icon">';
}
/**
* Generates the switch language list
*/
Language.prototype.generateSwitchLanguageList = function() {
var listEl = document.getElementById(this.languageSelectionListId);
if(listEl === null)
return;
// Clear the List
listEl.innerHTML = '';
// Generate Entries
var that = this;
for(var i = 0; i < this.list.length; i++) {
// Don't add the current language
if(this.activeIndex === i)
continue;
// Create the Entry
var li = document.createElement('li');
var a = document.createElement('a');
a.href = 'javascript:void(0);';
a.innerHTML = this.generateLanguageFlagIcon(i) + ' ' + this.generateLanguageName(i);
// Add everything together
li.appendChild(a);
listEl.appendChild(li);
// Add Listener
a[this.listenerAddMethod](window.addEventListener ? 'click' : 'onclick', function(index) {
return function(event) {
that.switchLanguage(index);
event.preventDefault();
};
}(i), false);
}
}
/**
* Tries to read the last Language-Value from the User
*/
Language.prototype.readPref = function() {
var prefVarName;
try {
prefVarName = localStorage.getItem(this.prefKey);
} catch(e) {
// Still print the Error and use default language
console.warn('Caught Exception: ' + e.name, e.message);
prefVarName = null;
}
if(prefVarName === null)
return;
var prefIndex = this.findIndexByVarName(prefVarName);
if(prefIndex === null)
return;
if(this.indexIsValid(prefIndex))
this.activeIndex = prefIndex;
}
/**
* Saves the current-Selected Language to the Users Local-Storage
*/
Language.prototype.savePref = function() {
// Delete when it's the default lang
if(this.activeIndex === this.defaultLangIndex) {
this.deletePref();
return;
}
try {
localStorage.setItem(this.prefKey, this.list[this.activeIndex].var_name);
} catch(e) {
// Still print the Error and ignore it
console.warn('Caught Exception: ' + e.name, e.message);
}
}
/**
* Deletes the User-Pref from the Users Local-Storage
*/
Language.prototype.deletePref = function() {
try {
localStorage.removeItem(this.prefKey);
} catch(e) {
// Still print the Error and ignore it
console.warn('Caught Exception: ' + e.name, e.message);
}
}
/**
* Finds the Language-Index via the "var_name"
*
* @param {string} varName - Var-Name
* @returns {int|null} - Index or null if not found
*/
Language.prototype.findIndexByVarName = function(varName) {
for(var i = 0; i < this.list.length; i++) {
if(this.list[i].var_name === varName)
return i;
}
return null;
}
/**
* Checks if the Index is in bound of the List
*
* @param {int} index - Index to check
* @returns {boolean} - Index is valid
*/
Language.prototype.indexIsValid = function(index) {
return index > 0 || index < this.list.length;
}
/**
* Checks if the var name has no invalid characters, to prevent Eval from executing code
*
* @param {string} varName - Var-Name content to check
* @returns {boolean} - Has invalid characters
*/
Language.prototype.varNameHasInvalidChars = function(varName) {
return /[^A-Za-z0-9_]/.test(varName);
}
/**
* Updates all Text within the HTML-Elements
*/
Language.prototype.updateStringElements = function() {
var elements = document.getElementsByClassName(this.translateClass);
// Update HTML-Language Meta
document.documentElement.lang = html(this.strings['htmlLang']);
// Update Title
document.title = escapeTitle(this.strings['title']);
// Update ::before Styles
var styleEl = document.getElementById(this.translationStylesId);
if(styleEl !== null) {
styleEl.innerHTML = '';
if(this.strings['files.dragAndDrop'] !== undefined)
styleEl.innerHTML += '.drag-drop-file:before { content: \'' + html(this.strings['files.dragAndDrop']) + '\'; }';
}
// Update translatable main elements first
for(var i = 0; i < elements.length; i++) {
this.translateElement(elements[i]);
}
}
/**
* Translates an Element
*
* @param {HTMLElement} element - Element to translate
*/
Language.prototype.translateElement = function(element) {
var data;
var savedChildren = null;
// Get children first
if(element.hasAttribute(this.translateableHasChilds)) {
var hasChild = element.getAttribute(this.translateableHasChilds);
if(hasChild === "true" || hasChild === "1") {
savedChildren = element.getElementsByClassName(this.translateChildClass);
if(savedChildren.length === 0)
savedChildren = null;
}
}
if(element.hasAttribute(this.translateableText)) {
data = element.getAttribute(this.translateableText);
if(savedChildren === null)
element.innerHTML = html(this.strings[data]);
else
element.innerHTML = this.translateTextWithChild(this.strings[data], savedChildren);
}
if(element.hasAttribute(this.translateableAlt)) {
data = element.getAttribute(this.translateableAlt);
element.alt = escapeTitle(this.strings[data]);
}
if(element.hasAttribute(this.translateableTitle)) {
data = element.getAttribute(this.translateableTitle);
element.title = escapeTitle(this.strings[data]);
}
}
/**
* Translates Text with children, given as List
*
* @param {string} translationText - Translation Text with children placeholder
* @param {HTMLCollection} children - Children-HTML Elements
* @returns {string} - Translated HTML (Escaped)
*/
Language.prototype.translateTextWithChild = function(translationText, children) {
// Escape HTML
translationText = html(translationText);
// Sort child HTML-Elements first
var sortedChildren = Array(children.length);
var i;
for(i = 0; i < children.length; i++) {
if(! children[i].hasAttribute(this.translateableChildIndex))
continue;
var childIndex = children[i].getAttribute(this.translateableChildIndex);
if(isNaN(childIndex))
continue;
if(childIndex > sortedChildren.length - 1)
continue;
// Translate Element first
this.translateElement(children[i]);
// Save element as Text
sortedChildren[childIndex] = children[i].outerHTML;
}
children = null;
// Replace the placeholders with the sorted children
for(i = 0; i < sortedChildren.length; i++) {
if(sortedChildren[i] === undefined || sortedChildren[i] === null)
continue;
translationText = translationText.replaceAll('{' + i + '}', sortedChildren[i]);
}
return translationText;
}
/**
* Updates the Translator Info
*/
Language.prototype.updateTranslatorInfo = function() {
var translators = this.strings.translatedBy;
var translatorElement = document.getElementById(this.translatorCreditsDivId);
if(translators.length < 1 || translatorElement === null) {
this.showTranslatorInfo(false);
return;
}
var translatorCreditUsers = '';
for(var i = 0; i < translators.length; i++) {
var hasUrl = translators[i].url !== null && translators[i].url !== '' && translators[i].url !== undefined;
if(i > 0) {
if(i === translators.length - 1)
translatorCreditUsers += ' & ';
else
translatorCreditUsers += ', ';
}
if(hasUrl)
translatorCreditUsers += '<a href="' + translators[i].url + '" target="_blank" title="' + html(this.strings['footer.translatedByLinkTitle']) + '">' + html(translators[i].name) + '</a>';
else
translatorCreditUsers += '<b>' + html(translators[i].name) + '</b>';
}
translatorElement.innerHTML = html(this.strings['footer.translatedBy']).replace('{0}', translatorCreditUsers);
this.showTranslatorInfo(true);
}
/**
* Shows the Translator Info
*
* @param {boolean} show - Shows the Translator Info
*/
Language.prototype.showTranslatorInfo = function(show) {
var element = document.getElementById(this.translatorCreditsDivId);
if(element === null)
return;
element.style.display = show ? 'block' : '';
}
/**
* Shows the language switcher
*
* @param {boolean} show - Shows the language switcher
*/
Language.prototype.showLanguageSwitcher = function(show) {
var element = document.getElementById(this.languageSwitcherBarId);
if(element === null)
return;
element.style.display = show ? 'block' : '';
}
this.removeDisabledLanguages();
if(this.list.length > 0) {
this.readPref();
this.switchLanguage(this.activeIndex);
this.updateLanguageSwitcher();
}
this.init = true;
}
/**
* Switches the Language
*
* @param {int} newIndex - New Language-Index
*/
Language.prototype.switchLanguage = function(newIndex) {
// Always close the list first and remove focus
this.toggleLanguageList(false);
document.activeElement.blur();
if(! this.indexIsValid(newIndex))
return;
// Don't trigger this if it's the same active index after init
if(this.init && newIndex === this.activeIndex)
return;
// Exit in case any special chars are in var name to prevent abuse of eval
if(this.varNameHasInvalidChars(this.list[newIndex].var_name)) {
alert('The selected language cannot be loaded because the var-name contains illegal characters!');
return;
}
// Yes I know... Sadly it's the only way, so it works locally too
var strings = eval(this.list[newIndex].var_name);
if(strings === undefined || strings === null) {
alert('The selected language is Invalid, it doesn\'t has any translated strings! (Is the Language-File not loaded maybe or has a wrong var name?)');
return;
}
this.strings = strings;
this.activeIndex = newIndex;
this.updateStringElements();
this.updateTranslatorInfo();
if(this.init)
this.updateLanguageSwitcher();
this.savePref();
}
/**
* Shows/Hides the Language-List based on the internal value to switch it to the opposite
*
* @param {boolean=} show - Shows the language switcher or hides it forcefully, omit to use the internal value
*/
Language.prototype.toggleLanguageList = function(show) {
var element = document.getElementById(this.languageSelectionListContainerId);
if(element === null)
return;
if(show === undefined) {
element.style.display = this.langListOpen ? '' : 'block';
this.langListOpen = !this.langListOpen;
} else {
element.style.display = show ? 'block' : '';
this.langListOpen = show;
}
}
|