applocale.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. function NewAppLocale(){
  16. return {
  17. lang: (localStorage.getItem('global_language') == null || localStorage.getItem('global_language') == "default" ? navigator.language : localStorage.getItem('global_language')).toLowerCase(),
  18. localeFile: "",
  19. localData: {},
  20. init: function(localeFile, callback = undefined) {
  21. this.localeFile = localeFile;
  22. let targetApplocaleObject = this;
  23. $.ajax({
  24. dataType: "json",
  25. url: localeFile,
  26. success: function(data) {
  27. targetApplocaleObject.localData = data;
  28. if (callback != undefined) {
  29. callback(data);
  30. }
  31. if (data.keys[targetApplocaleObject.lang] != undefined && data.keys[targetApplocaleObject.lang].fwtitle != undefined && data.keys[targetApplocaleObject.lang].fwtitle != "" && ao_module_virtualDesktop) {
  32. //Update the floatwindow title as well
  33. ao_module_setWindowTitle(data.keys[targetApplocaleObject.lang].fwtitle);
  34. }
  35. if (data.keys[targetApplocaleObject.lang] != undefined && data.keys[targetApplocaleObject.lang].fontFamily != undefined){
  36. //This language has a prefered font family. Inject it
  37. $("h1, h2, h3, p, span, div, a, button").css({
  38. "font-family":data.keys[targetApplocaleObject.lang].fontFamily
  39. });
  40. console.log("[Applocale] Updating font family to: ", data.keys[targetApplocaleObject.lang].fontFamily)
  41. }
  42. }
  43. });
  44. },
  45. translate: function(targetLang = "") {
  46. var targetLang = targetLang || this.lang;
  47. //Check if the given locale exists
  48. if (this.localData == undefined || this.localData.keys === undefined || this.localData.keys[targetLang] == undefined) {
  49. console.log("[Applocale] This language is not supported. Using default")
  50. return
  51. }
  52. //Update the page content to fit the localization
  53. let hasTitleLocale = (this.localData.keys[targetLang].titles !== undefined);
  54. let hasStringLocale = (this.localData.keys[targetLang].strings !== undefined);
  55. let hasPlaceHolderLocale = (this.localData.keys[targetLang].placeholder !== undefined);
  56. let localizedDataset = this.localData.keys[targetLang];
  57. $("*").each(function() {
  58. if ($(this).attr("title") != undefined && hasTitleLocale) {
  59. let targetString = localizedDataset.titles[$(this).attr("title")];
  60. if (targetString != undefined) {
  61. $(this).attr("title", targetString);
  62. }
  63. }
  64. if ($(this).attr("locale") != undefined && hasStringLocale) {
  65. let targetString = localizedDataset.strings[$(this).attr("locale")];
  66. if (targetString != undefined) {
  67. $(this).html(targetString);
  68. }
  69. }
  70. if ($(this).attr("placeholder") != undefined && hasPlaceHolderLocale) {
  71. let targetString = localizedDataset.placeholder[$(this).attr("placeholder")];
  72. if (targetString != undefined) {
  73. $(this).attr("placeholder", targetString);
  74. }
  75. }
  76. })
  77. if (this.localData.keys[this.lang] != undefined && this.localData.keys[this.lang].fontFamily != undefined){
  78. //This language has a prefered font family. Inject it
  79. $("h1, h2, h3, h4, h5, p, span, div, a").css({
  80. "font-family":this.localData.keys[this.lang].fontFamily
  81. });
  82. }
  83. },
  84. getString: function(key, original, type = "strings") {
  85. var targetLang = this.lang;
  86. if (this.localData.keys === undefined || this.localData.keys[targetLang] == undefined) {
  87. return original;
  88. }
  89. let targetString = this.localData.keys[targetLang].strings[key];
  90. if (targetString != undefined) {
  91. return targetString
  92. } else {
  93. return original
  94. }
  95. },
  96. applyFontStyle: function(target){
  97. $(target).css({
  98. "font-family":applocale.localData.keys[applocale.lang].fontFamily
  99. })
  100. }
  101. }
  102. }
  103. if (applocale == undefined){
  104. //Only allow once instance of global applocale obj
  105. var applocale = NewAppLocale();
  106. }