applocale.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, a, button").css({
  36. "font-family":data.keys[applocale.lang].fontFamily
  37. });
  38. console.log("[Applocale] Updating font family to: ", data.keys[applocale.lang].fontFamily)
  39. }
  40. }
  41. });
  42. },
  43. translate: function(targetLang = "") {
  44. var targetLang = targetLang || this.lang;
  45. //Check if the given locale exists
  46. if (this.localData == undefined || this.localData.keys === undefined || this.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 = (this.localData.keys[targetLang].titles !== undefined);
  52. let hasStringLocale = (this.localData.keys[targetLang].strings !== undefined);
  53. let hasPlaceHolderLocale = (this.localData.keys[targetLang].placeholder !== undefined);
  54. let localizedDataset = this.localData.keys[targetLang];
  55. $("*").each(function() {
  56. if ($(this).attr("title") != undefined && hasTitleLocale) {
  57. let targetString = localizedDataset.titles[$(this).attr("title")];
  58. if (targetString != undefined) {
  59. $(this).attr("title", targetString);
  60. }
  61. }
  62. if ($(this).attr("locale") != undefined && hasStringLocale) {
  63. let targetString = localizedDataset.strings[$(this).attr("locale")];
  64. if (targetString != undefined) {
  65. $(this).html(targetString);
  66. }
  67. }
  68. if ($(this).attr("placeholder") != undefined && hasPlaceHolderLocale) {
  69. let targetString = localizedDataset.placeholder[$(this).attr("placeholder")];
  70. if (targetString != undefined) {
  71. $(this).attr("placeholder", targetString);
  72. }
  73. }
  74. })
  75. if (applocale.localData.keys[applocale.lang] != undefined && applocale.localData.keys[applocale.lang].fontFamily != undefined){
  76. //This language has a prefered font family. Inject it
  77. $("h1, h2, h3, p, span, div, a").css({
  78. "font-family":applocale.localData.keys[applocale.lang].fontFamily
  79. });
  80. }
  81. },
  82. getString: function(key, original, type = "strings") {
  83. var targetLang = this.lang;
  84. if (this.localData.keys === undefined || this.localData.keys[targetLang] == undefined) {
  85. return original;
  86. }
  87. let targetString = this.localData.keys[targetLang].strings[key];
  88. if (targetString != undefined) {
  89. return targetString
  90. } else {
  91. return original
  92. }
  93. }
  94. }