PageRenderTime 21ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/WebContent/localization/OSRM.Localization.js

https://gitlab.com/khangaikhuu/route
JavaScript | 153 lines | 98 code | 17 blank | 38 comment | 45 complexity | 0c35a2d0c68be7029870e2f99f491db9 MD5 | raw file
  1. /*
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU AFFERO General Public License as published by
  4. the Free Software Foundation; either version 3 of the License, or
  5. any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU Affero General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  13. or see http://www.gnu.org/licenses/agpl.txt.
  14. */
  15. // OSRM localization
  16. // [basic localization options]
  17. OSRM.Localization = {
  18. // default directory for localization files
  19. DIRECTORY: "localization/",
  20. // currently active language and fallback language (used if a string is not available in the current language)
  21. current_language: OSRM.DEFAULTS.LANGUAGE,
  22. fallback_language: "en",
  23. // language that is currently being loaded on demand
  24. load_on_demand_language: null,
  25. //initialize localization
  26. init: function() {
  27. var supported_languages = OSRM.DEFAULTS.LANGUAGE_SUPPORTED;
  28. // check browser language
  29. if( OSRM.DEFAULTS.LANGUAGE_USE_BROWSER_SETTING == true ) {
  30. var language_label = (navigator.language || navigator.userLanguage || "").substring(0,2);
  31. for(var i=0; i<supported_languages.length; ++i) {
  32. if( supported_languages[i].encoding == language_label )
  33. OSRM.Localization.current_language = language_label;
  34. }
  35. }
  36. // fill option list and find default entry
  37. var options = [];
  38. var options_2 = [];
  39. var selected = 0;
  40. for(var i=0, size=supported_languages.length; i<size; i++) {
  41. options.push( {display:supported_languages[i].encoding, value:supported_languages[i].encoding} );
  42. options_2.push( {display:supported_languages[i].name, value:supported_languages[i].encoding} );
  43. if( supported_languages[i].encoding == OSRM.Localization.current_language )
  44. selected=i;
  45. }
  46. // generate selectors
  47. OSRM.GUI.selectorInit("gui-language-toggle", options, selected, OSRM.Localization.setLanguageWrapper);
  48. OSRM.GUI.selectorInit("gui-language-2-toggle", options_2, selected, OSRM.Localization.setLanguageWrapper);
  49. // load language
  50. OSRM.Localization.setLanguage( OSRM.Localization.fallback_language );
  51. OSRM.Localization.setLanguage( OSRM.Localization.current_language );
  52. },
  53. setLanguageWrapper: function(language) { // wrapping required to correctly prevent localization tooltip from showing
  54. OSRM.GUI.deactivateTooltip( "LOCALIZATION" );
  55. OSRM.Localization.setLanguage(language);
  56. },
  57. setLanguage: function(language, loaded_on_demand) {
  58. // check if loaded-on-demand language is still wanted as current language
  59. if( loaded_on_demand ) {
  60. // fix for racing condition when fallback language gets loaded after current language
  61. var fb_localization = OSRM.Localization[OSRM.Localization.fallback_language];
  62. if( fb_localization == null || fb_localization.loading != null ) // fallback language still loading
  63. return;
  64. var od_localization = OSRM.Localization[OSRM.Localization.load_on_demand_language];
  65. if( od_localization != null && od_localization.loading == null ) // on demand language has loaded
  66. language = OSRM.Localization.load_on_demand_language;
  67. if( language != OSRM.Localization.load_on_demand_language )
  68. return;
  69. }
  70. // change value of both language selectors
  71. OSRM.GUI.selectorChange( 'gui-language-toggle', language );
  72. OSRM.GUI.selectorChange( 'gui-language-2-toggle', language );
  73. if( OSRM.Localization[language] ) {
  74. if( OSRM.Localization[language].loading ) // check if localization is currently being loaded
  75. return;
  76. OSRM.Localization.current_language = language;
  77. OSRM.Localization.load_on_demand_language = null;
  78. // change gui language
  79. OSRM.GUI.setLabels();
  80. // change notifications
  81. OSRM.GUI.updateNotifications();
  82. // change abbreviations
  83. OSRM.Utils.updateAbbreviationCache();
  84. // change map language
  85. for(var i=0, size=OSRM.G.localizable_maps.length; i<size; i++) {
  86. OSRM.G.localizable_maps[i].options.culture = OSRM.loc("CULTURE");
  87. }
  88. if(OSRM.G.map.layerControl.getActiveLayer().redraw)
  89. OSRM.G.map.layerControl.getActiveLayer().redraw();
  90. // change map layer languages
  91. OSRM.G.map.layerControl.setLayerLabels();
  92. // requery data
  93. if( OSRM.G.markers == null )
  94. return;
  95. if( OSRM.G.markers.route.length > 1)
  96. OSRM.Routing.getRoute({keepAlternative:true});
  97. else if(OSRM.G.markers.route.length > 0 && document.getElementById('information-box').innerHTML != "" ) {
  98. OSRM.Geocoder.call( OSRM.C.SOURCE_LABEL, document.getElementById("gui-input-source").value );
  99. OSRM.Geocoder.call( OSRM.C.TARGET_LABEL, document.getElementById("gui-input-target").value );
  100. } else {
  101. OSRM.Geocoder.updateAddress(OSRM.C.SOURCE_LABEL, false);
  102. OSRM.Geocoder.updateAddress(OSRM.C.TARGET_LABEL, false);
  103. OSRM.GUI.clearResults();
  104. }
  105. } else if(OSRM.DEFAULTS.LANUGAGE_ONDEMAND_RELOADING==true) {
  106. var supported_languages = OSRM.DEFAULTS.LANGUAGE_SUPPORTED;
  107. for(var i=0, size=supported_languages.length; i<size; i++) {
  108. if( supported_languages[i].encoding == language) {
  109. OSRM.Localization.load_on_demand_language = language;
  110. OSRM.Localization[language] = {loading:true}; // add dummy localization until localization is loaded
  111. var script = document.createElement('script');
  112. script.type = 'text/javascript';
  113. script.src = OSRM.Localization.DIRECTORY+"OSRM.Locale."+language+".js";
  114. document.head.appendChild(script);
  115. break;
  116. }
  117. }
  118. }
  119. },
  120. // if existing, return localized string -> English string -> input string
  121. // [if fallback given and localized, English strings not existent, localize and return fallback string]
  122. translate: function(text, fallback) {
  123. if( OSRM.Localization[OSRM.Localization.current_language] && OSRM.Localization[OSRM.Localization.current_language][text] )
  124. return OSRM.Localization[OSRM.Localization.current_language][text];
  125. else if( OSRM.Localization[OSRM.Localization.fallback_language] && OSRM.Localization[OSRM.Localization.fallback_language][text] )
  126. return OSRM.Localization[OSRM.Localization.fallback_language][text];
  127. else if( fallback )
  128. return OSRM.loc( fallback );
  129. else
  130. return text;
  131. }
  132. };
  133. // shorter call to translate function
  134. OSRM.loc = OSRM.Localization.translate;