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')).toLowerCase(),
  17. localeFile: "",
  18. localData: {},
  19. init: function(localeFile, callback = undefined) {
  20. this.localeFile = localeFile;
  21. $.ajax({
  22. dataType: "json",
  23. url: localeFile,
  24. success: function(data) {
  25. applocale.localData = data;
  26. if (callback != undefined) {
  27. callback(data);
  28. }
  29. if (data.keys[applocale.lang] != undefined && data.keys[applocale.lang].fwtitle != undefined && ao_module_virtualDesktop) {
  30. //Update the floatwindow title as well
  31. ao_module_setWindowTitle(data.keys[applocale.lang].fwtitle);
  32. }
  33. if (data.keys[applocale.lang] != undefined && data.keys[applocale.lang].fontFamily != undefined){
  34. //This language has a prefered font family. Inject it
  35. $("h1, h2, h3, p, span, div, span").css({
  36. "font-family":data.keys[applocale.lang].fontFamily
  37. })
  38. }
  39. }
  40. });
  41. },
  42. translate: function(targetLang = "") {
  43. var targetLang = targetLang || this.lang;
  44. //Check if the given locale exists
  45. if (this.localData == undefined || this.localData.keys === undefined || this.localData.keys[targetLang] == undefined) {
  46. console.log("[Applocale] This language is not supported. Using default")
  47. return
  48. }
  49. //Update the page content to fit the localization
  50. let hasTitleLocale = (this.localData.keys[targetLang].titles !== undefined);
  51. let hasStringLocale = (this.localData.keys[targetLang].strings !== undefined);
  52. let hasPlaceHolderLocale = (this.localData.keys[targetLang].placeholder !== undefined);
  53. let localizedDataset = this.localData.keys[targetLang];
  54. $("*").each(function() {
  55. if ($(this).attr("title") != undefined && hasTitleLocale) {
  56. let targetString = localizedDataset.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 = localizedDataset.strings[$(this).attr("locale")];
  63. if (targetString != undefined) {
  64. $(this).html(targetString);
  65. }
  66. }
  67. if ($(this).attr("placeholder") != undefined && hasPlaceHolderLocale) {
  68. let targetString = localizedDataset.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 = this.lang;
  77. if (this.localData.keys === undefined || this.localData.keys[targetLang] == undefined) {
  78. return original;
  79. }
  80. let targetString = this.localData.keys[targetLang].strings[key];
  81. if (targetString != undefined) {
  82. return targetString
  83. } else {
  84. return original
  85. }
  86. }
  87. }