internationalization_callbacks.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* ------------------------------------------------------------------------------
  2. *
  3. * # Internationalization - callbacks
  4. *
  5. * Demo JS code for internationalization_callbacks.html page
  6. *
  7. * ---------------------------------------------------------------------------- */
  8. // Setup module
  9. // ------------------------------
  10. var I18nextCallbacks = function() {
  11. //
  12. // Setup module components
  13. //
  14. // Noty.js
  15. var _componentNoty = function(message) {
  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: message,
  23. type: 'info',
  24. theme: 'limitless',
  25. layout: 'topRight',
  26. timeout: 2500
  27. }).show();
  28. };
  29. // Translation callbacks
  30. var _componentI18nextCallbacks = 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. russianLangClass = '.russian',
  41. ukrainianLangClass = '.ukrainian',
  42. $localizationElement = $('body');
  43. // Add options
  44. i18next.use(i18nextXHRBackend).use(i18nextBrowserLanguageDetector).init({
  45. backend: {
  46. loadPath: '../../../../global_assets/locales/{{lng}}.json'
  47. },
  48. debug: true,
  49. fallbackLng: false
  50. },
  51. function (err, t) {
  52. // Initialize library
  53. jqueryI18next.init(i18next, $);
  54. // Initialize translation
  55. $localizationElement.localize();
  56. // To avoid FOUC when translation gets initialized,
  57. // use data-fouc attribute in all elements by default. When translation
  58. // is initialized, remove it from all elements
  59. $localizationElement.find('[data-i18n]').removeAttr('data-fouc');
  60. });
  61. // Change languages in dropdown
  62. // -------------------------
  63. // Do stuff when i18Next is initialized
  64. i18next.on('initialized', function() {
  65. // Notification
  66. _componentNoty('i18Next has been initialized. <br> The following language has beed detected: ' + '<span class="font-weight-semibold text-uppercase">' + i18next.language + '</span>');
  67. // English
  68. if(i18next.language === "en") {
  69. // Set active class
  70. $('.dropdown-item' + englishLangClass).addClass('active');
  71. $('.navbar-nav-link' + englishLangClass).parent().addClass('active');
  72. // Change language in dropdown
  73. $switchContainer.children('.dropdown-toggle').html(
  74. $switchContainer.find(englishLangClass).html()
  75. ).children('img').addClass('mr-2');
  76. }
  77. // Russian
  78. if(i18next.language === "ru") {
  79. // Set active class
  80. $('.dropdown-item' + russianLangClass).addClass('active');
  81. $('.navbar-nav-link' + russianLangClass).parent().addClass('active');
  82. // Change language in dropdown
  83. $switchContainer.children('.dropdown-toggle').html(
  84. $switchContainer.find(russianLangClass).html()
  85. ).children('img').addClass('mr-2');
  86. }
  87. // Ukrainian
  88. if(i18next.language === "ua") {
  89. // Set active class
  90. $('.dropdown-item' + ukrainianLangClass).addClass('active');
  91. $('.navbar-nav-link' + ukrainianLangClass).parent().addClass('active');
  92. // Change language in dropdown
  93. $switchContainer.children('.dropdown-toggle').html(
  94. $switchContainer.find(ukrainianLangClass).html()
  95. ).children('img').addClass('mr-2');
  96. }
  97. });
  98. // Change languages in navbar
  99. // -------------------------
  100. // English
  101. $(englishLangClass).on('click', function () {
  102. // Change language
  103. i18next.changeLanguage('en');
  104. // When changed, run translation again
  105. i18next.on('languageChanged', function() {
  106. // Localize
  107. $localizationElement.localize();
  108. // Notification
  109. _componentNoty('Language has been changed to: ' + '<span class="font-weight-semibold text-uppercase">' + i18next.language + '</span>');
  110. });
  111. // Change lang in dropdown
  112. $switchContainer.children('.dropdown-toggle').html(
  113. $(englishLangClass).html()
  114. ).children('img').addClass('mr-2');
  115. // Set active class
  116. $switchContainer.find('.dropdown-item.active, .nav-item.active').removeClass('active');
  117. $('.dropdown-item' + englishLangClass).addClass('active');
  118. $('.navbar-nav-link' + englishLangClass).parent().addClass('active');
  119. });
  120. // Russian
  121. $(russianLangClass).on('click', function () {
  122. // Change language
  123. i18next.changeLanguage('ru');
  124. // When changed, run translation again
  125. i18next.on('languageChanged', function() {
  126. // Localize
  127. $localizationElement.localize();
  128. // Notification
  129. _componentNoty('Language has been changed to: ' + '<span class="font-weight-semibold text-uppercase">' + i18next.language + '</span>');
  130. });
  131. // Change lang in dropdown
  132. $switchContainer.children('.dropdown-toggle').html(
  133. $(russianLangClass).html()
  134. ).children('img').addClass('mr-2');
  135. // Set active class
  136. $switchContainer.find('.dropdown-item.active, .nav-item.active').removeClass('active');
  137. $('.dropdown-item' + russianLangClass).addClass('active');
  138. $('.navbar-nav-link' + russianLangClass).parent().addClass('active');
  139. });
  140. // Ukrainian
  141. $(ukrainianLangClass).on('click', function () {
  142. // Change language
  143. i18next.changeLanguage('ua');
  144. // When changed, run translation again
  145. i18next.on('languageChanged', function() {
  146. // Localize
  147. $localizationElement.localize();
  148. // Notification
  149. _componentNoty('Language has been changed to: ' + '<span class="font-weight-semibold text-uppercase">' + i18next.language + '</span>');
  150. });
  151. // Change lang in dropdown
  152. $switchContainer.children('.dropdown-toggle').html(
  153. $(ukrainianLangClass).html()
  154. ).children('img').addClass('mr-2');
  155. // Set active class
  156. $switchContainer.find('.dropdown-item.active, .nav-item.active').removeClass('active');
  157. $('.dropdown-item' + ukrainianLangClass).addClass('active');
  158. $('.navbar-nav-link' + ukrainianLangClass).parent().addClass('active');
  159. });
  160. };
  161. //
  162. // Return objects assigned to module
  163. //
  164. return {
  165. init: function() {
  166. _componentI18nextCallbacks();
  167. }
  168. }
  169. }();
  170. // Initialize module
  171. // ------------------------------
  172. document.addEventListener('DOMContentLoaded', function() {
  173. I18nextCallbacks.init();
  174. });