internationalization_fallback.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Internationalization - fallback language
  4. *
  5. * Demo JS code for internationalization_fallback.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var I18nextFallback = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Noty.js
  15. var _componentNoty = function(lang) {
  16. if (typeof Noty == 'undefined') {
  17. console.warn('Warning - noty.min.js is not loaded.');
  18. return;
  19. }
  20. // Initialize
  21. new Noty({
  22. text: '<span class="text-uppercase">"' + lang + '"</span>' + ' locale is missing. Loading fallback language.',
  23. type: 'error',
  24. theme: 'limitless',
  25. layout: 'topRight',
  26. timeout: 2500
  27. }).show();
  28. };
  29. // Fallback language
  30. var _componentI18nextFallback = function() {
  31. if (typeof i18next == 'undefined') {
  32. console.warn('Warning - i18next.min.js is not loaded.');
  33. return;
  34. }
  35. // Configuration
  36. // -------------------------
  37. // Define main elements
  38. var $switchContainer = $('.language-switch'),
  39. englishLangClass = '.english',
  40. spanishLangClass = '.spanish',
  41. italianLangClass = '.italian',
  42. $localizationElement = $('body');
  43. // Add options
  44. i18next.use(i18nextXHRBackend).use(i18nextBrowserLanguageDetector).init({
  45. backend: {
  46. loadPath: '../../../../global_assets/locales/{{lng}}.json'
  47. },
  48. load: 'languageOnly',
  49. debug: true,
  50. fallbackLng: 'en'
  51. },
  52. function (err, t) {
  53. // Initialize library
  54. jqueryI18next.init(i18next, $);
  55. // Initialize translation
  56. $localizationElement.localize();
  57. // To avoid FOUC when translation gets initialized,
  58. // use data-fouc attribute in all elements by default. When translation
  59. // is initialized, remove it from all elements
  60. $localizationElement.find('[data-i18n]').removeAttr('data-fouc');
  61. });
  62. // Change languages in dropdown
  63. // -------------------------
  64. // Do stuff when i18Next is initialized
  65. i18next.on('initialized', function() {
  66. // English
  67. if(i18next.language === "en") {
  68. // Set active class
  69. $('.dropdown-item' + englishLangClass).addClass('active');
  70. $('.navbar-nav-link' + englishLangClass).parent().addClass('active');
  71. // Change language in dropdown
  72. $switchContainer.children('.dropdown-toggle').html(
  73. $switchContainer.find(englishLangClass).html()
  74. ).children('img').addClass('mr-2');
  75. }
  76. // Spanish
  77. else if(i18next.language === "es") {
  78. // Show error notification
  79. _componentNoty(i18next.language);
  80. // Set active class
  81. $('.dropdown-item' + spanishLangClass).addClass('active');
  82. $('.navbar-nav-link' + spanishLangClass).parent().addClass('active');
  83. // Change language in dropdown
  84. $switchContainer.children('.dropdown-toggle').html(
  85. $switchContainer.find(spanishLangClass).html()
  86. ).children('img').addClass('mr-2');
  87. }
  88. // Italian
  89. else if(i18next.language === "it") {
  90. // Show error notification
  91. _componentNoty(i18next.language);
  92. // Set active class
  93. $('.dropdown-item' + italianLangClass).addClass('active');
  94. $('.navbar-nav-link' + italianLangClass).parent().addClass('active');
  95. // Change language in dropdown
  96. $switchContainer.children('.dropdown-toggle').html(
  97. $switchContainer.find(italianLangClass).html()
  98. ).children('img').addClass('mr-2');
  99. }
  100. else {
  101. // Change language
  102. i18next.changeLanguage('en');
  103. // When changed, run translation again
  104. i18next.on('languageChanged', function() {
  105. $localizationElement.localize();
  106. });
  107. // Set active class
  108. $('.dropdown-item' + englishLangClass).addClass('active');
  109. $('.navbar-nav-link' + englishLangClass).parent().addClass('active');
  110. // Change language in dropdown
  111. $switchContainer.children('.dropdown-toggle').html(
  112. $switchContainer.find(englishLangClass).html()
  113. ).children('img').addClass('mr-2');
  114. }
  115. });
  116. };
  117. //
  118. // Return objects assigned to module
  119. //
  120. return {
  121. init: function() {
  122. _componentI18nextFallback();
  123. }
  124. }
  125. }();
  126. // Initialize module
  127. // ------------------------------
  128. document.addEventListener('DOMContentLoaded', function() {
  129. I18nextFallback.init();
  130. });