applocale.js 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Application Locale Module
  3. author: tobychui
  4. This module translate the current web application page
  5. to the desired language specific by the browser.
  6. Example Usage:
  7. if (applocale){
  8. //Applocale found. Do localization
  9. applocale.init("../locale/file_explorer.json", function(){
  10. applocale.translate();
  11. //Do other init things on the page
  12. });
  13. }
  14. */
  15. var applocale = {
  16. lang: localStorage.getItem('global_language') == null || localStorage.getItem('global_language') == "default" ? navigator.language : localStorage.getItem('global_language'),
  17. lang: navigator.language.toLowerCase(),
  18. localeFile: "",
  19. localData: {},
  20. init: function(localeFile, callback = undefined) {
  21. applocale.localeFile = localeFile;
  22. $.ajax({
  23. dataType: "json",
  24. url: localeFile,
  25. success: function(data) {
  26. applocale.localData = data;
  27. if (callback != undefined) {
  28. callback(data);
  29. }
  30. if (data.keys[applocale.lang] != undefined && data.keys[applocale.lang].fwtitle != undefined && ao_module_virtualDesktop) {
  31. //Update the floatwindow title as well
  32. ao_module_setWindowTitle(data.keys[applocale.lang].fwtitle);
  33. }
  34. if (data.keys[applocale.lang] != undefined && data.keys[applocale.lang].fontFamily != undefined){
  35. //This language has a prefered font family. Inject it
  36. $("h1, h2, h3, p, span, div, span").css({
  37. "font-family":data.keys[applocale.lang].fontFamily
  38. })
  39. }
  40. }
  41. });
  42. },
  43. translate: function(targetLang = "") {
  44. var targetLang = targetLang || applocale.lang;
  45. //Check if the given locale exists
  46. if (applocale.localData.keys[targetLang] == undefined) {
  47. console.log("[Applocale] This language is not supported. Using default")
  48. return
  49. }
  50. //Update the page content to fit the localization
  51. let hasTitleLocale = (applocale.localData.keys[targetLang].titles !== undefined);
  52. let hasStringLocale = (applocale.localData.keys[targetLang].strings !== undefined);
  53. let hasPlaceHolderLocale = (applocale.localData.keys[targetLang].placeholder !== undefined);
  54. $("*").each(function() {
  55. if ($(this).attr("title") != undefined && hasTitleLocale) {
  56. let targetString = applocale.localData.keys[targetLang].titles[$(this).attr("title")];
  57. if (targetString != undefined) {
  58. $(this).attr("title", targetString);
  59. }
  60. }
  61. if ($(this).attr("locale") != undefined && hasStringLocale) {
  62. let targetString = applocale.localData.keys[targetLang].strings[$(this).attr("locale")];
  63. if (targetString != undefined) {
  64. $(this).html(targetString);
  65. }
  66. }
  67. if ($(this).attr("placeholder") != undefined && hasPlaceHolderLocale) {
  68. let targetString = applocale.localData.keys[targetLang].placeholder[$(this).attr("placeholder")];
  69. if (targetString != undefined) {
  70. $(this).attr("placeholder", targetString);
  71. }
  72. }
  73. })
  74. },
  75. getString: function(key, original, type = "strings") {
  76. var targetLang = applocale.lang;
  77. if (applocale.localData.keys[targetLang] == undefined) {
  78. return original;
  79. }
  80. let targetString = applocale.localData.keys[targetLang].strings[key];
  81. if (targetString != undefined) {
  82. return targetString
  83. } else {
  84. return original
  85. }
  86. }
  87. }