applocale.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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: navigator.language,
  17. localeFile: "",
  18. localData: {},
  19. init: function(localeFile, callback=undefined){
  20. applocale.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. }
  34. });
  35. },
  36. translate: function(targetLang = ""){
  37. var targetLang = targetLang || applocale.lang;
  38. //Check if the given locale exists
  39. if (applocale.localData.keys[targetLang] == undefined){
  40. console.log("[Applocale] This language is not supported. Using default")
  41. return
  42. }
  43. //Update the page content to fit the localization
  44. let hasTitleLocale = (applocale.localData.keys[targetLang].titles !== undefined);
  45. let hasStringLocale = (applocale.localData.keys[targetLang].strings !== undefined);
  46. let hasPlaceHolderLocale = (applocale.localData.keys[targetLang].placeholder !== undefined);
  47. $("*").each(function(){
  48. if ($(this).attr("title") != undefined && hasTitleLocale){
  49. let targetString = applocale.localData.keys[targetLang].titles[$(this).attr("title")];
  50. if (targetString != undefined){
  51. $(this).attr("title", targetString);
  52. }
  53. }
  54. if ($(this).attr("locale") != undefined && hasStringLocale){
  55. let targetString = applocale.localData.keys[targetLang].strings[$(this).attr("locale")];
  56. if (targetString != undefined){
  57. $(this).html(targetString);
  58. }
  59. }
  60. if ($(this).attr("placeholder") != undefined && hasPlaceHolderLocale){
  61. let targetString = applocale.localData.keys[targetLang].placeholder[$(this).attr("placeholder")];
  62. if (targetString != undefined){
  63. $(this).attr("placeholder",targetString);
  64. }
  65. }
  66. })
  67. },
  68. getString: function(key, original, type="strings"){
  69. var targetLang = applocale.lang;
  70. if (applocale.localData.keys[targetLang] == undefined){
  71. return original;
  72. }
  73. let targetString = applocale.localData.keys[targetLang].strings[key];
  74. if (targetString != undefined){
  75. return targetString
  76. }else{
  77. return original
  78. }
  79. }
  80. }