1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /*
- Application Locale Module
- author: tobychui
- This module translate the current web application page
- to the desired language specific by the browser.
- Example Usage:
- if (applocale){
- //Applocale found. Do localization
- applocale.init("../locale/file_explorer.json", function(){
- applocale.translate();
- //Do other init things on the page
- });
- }
- */
- var applocale = {
- lang: localStorage.getItem('global_language') == null || localStorage.getItem('global_language') == "default" ? navigator.language : localStorage.getItem('global_language'),
- lang: navigator.language.toLowerCase(),
- localeFile: "",
- localData: {},
- init: function(localeFile, callback = undefined) {
- applocale.localeFile = localeFile;
- $.ajax({
- dataType: "json",
- url: localeFile,
- success: function(data) {
- applocale.localData = data;
- if (callback != undefined) {
- callback(data);
- }
- if (data.keys[applocale.lang] != undefined && data.keys[applocale.lang].fwtitle != undefined && ao_module_virtualDesktop) {
- //Update the floatwindow title as well
- ao_module_setWindowTitle(data.keys[applocale.lang].fwtitle);
- }
- if (data.keys[applocale.lang] != undefined && data.keys[applocale.lang].fontFamily != undefined){
- //This language has a prefered font family. Inject it
- $("h1, h2, h3, p, span, div, span").css({
- "font-family":data.keys[applocale.lang].fontFamily
- })
- }
- }
- });
- },
- translate: function(targetLang = "") {
- var targetLang = targetLang || applocale.lang;
- //Check if the given locale exists
- if (applocale.localData.keys[targetLang] == undefined) {
- console.log("[Applocale] This language is not supported. Using default")
- return
- }
- //Update the page content to fit the localization
- let hasTitleLocale = (applocale.localData.keys[targetLang].titles !== undefined);
- let hasStringLocale = (applocale.localData.keys[targetLang].strings !== undefined);
- let hasPlaceHolderLocale = (applocale.localData.keys[targetLang].placeholder !== undefined);
- $("*").each(function() {
- if ($(this).attr("title") != undefined && hasTitleLocale) {
- let targetString = applocale.localData.keys[targetLang].titles[$(this).attr("title")];
- if (targetString != undefined) {
- $(this).attr("title", targetString);
- }
- }
- if ($(this).attr("locale") != undefined && hasStringLocale) {
- let targetString = applocale.localData.keys[targetLang].strings[$(this).attr("locale")];
- if (targetString != undefined) {
- $(this).html(targetString);
- }
- }
- if ($(this).attr("placeholder") != undefined && hasPlaceHolderLocale) {
- let targetString = applocale.localData.keys[targetLang].placeholder[$(this).attr("placeholder")];
- if (targetString != undefined) {
- $(this).attr("placeholder", targetString);
- }
- }
- })
- },
- getString: function(key, original, type = "strings") {
- var targetLang = applocale.lang;
- if (applocale.localData.keys[targetLang] == undefined) {
- return original;
- }
- let targetString = applocale.localData.keys[targetLang].strings[key];
- if (targetString != undefined) {
- return targetString
- } else {
- return original
- }
- }
- }
|