language.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. (function() {
  2. function LanguageBannerElement(targetLanguage) {
  3. this.el = document.createElement('div')
  4. this.render = function() {
  5. this.el.innerHTML = this.constructor.template
  6. this.el.querySelector('a').href = this.itemPath() || this.kbPath()
  7. this.el.querySelector('.close').addEventListener('click', this.close.bind(this))
  8. }
  9. this.kbPath = function() {
  10. var html = document.documentElement
  11. return html.dataset.basePath.replace(encodeURIComponent('{locale}'), targetLanguage)
  12. }
  13. this.close = function() {
  14. this.el.remove()
  15. }
  16. this.itemPath = function() {
  17. var item = document.querySelector('main')
  18. if(!item) { return null }
  19. var path = item.dataset.basePath
  20. var object_locales = item.dataset.availableLocales
  21. if(!path || !object_locales) { return null }
  22. object_locales = object_locales.split(',')
  23. if(!LanguageDetector.includes(object_locales, targetLanguage)) { return null }
  24. path = path.replace(encodeURIComponent('{locale}'), targetLanguage)
  25. return path
  26. }
  27. this.render()
  28. }
  29. LanguageBannerElement.template = '<div class="language-banner">' +
  30. Zammad.Util.generateIcon('mood-supergood') +
  31. ' <p class="language-banner-text">' +
  32. ' The Knowledge Base is available in your language <a class="button button--small">activate</a>' +
  33. ' </p>' +
  34. ' <div class="spacer"></div>' +
  35. ' <div class="close">' +
  36. Zammad.Util.generateIcon('diagonal-cross') +
  37. ' </div>' +
  38. '</div>';
  39. function LanguageDetector() { }
  40. LanguageDetector.html = function() { return document.documentElement }
  41. LanguageDetector.document_languages = function() {
  42. return this.html().dataset.availableLocales.split(',')
  43. }
  44. LanguageDetector.user_languages = function() {
  45. var languages = navigator.userLanguage || navigator.languages || navigator.language
  46. if(Array.isArray(languages)) {
  47. return languages
  48. } else if(languages) {
  49. return [languages]
  50. } else {
  51. return []
  52. }
  53. }
  54. LanguageDetector.is_current = function(locale) {
  55. return this.extract_language(this.html().lang) === this.extract_language(locale)
  56. }
  57. LanguageDetector.includes = function(array, item) {
  58. item = this.extract_language(item)
  59. return array.filter(function(locale) { return this.extract_language(locale) === item}, this)[0] !== undefined
  60. }
  61. LanguageDetector.offer_language = function() {
  62. if(this.includes(this.user_languages(), this.html().lang)) {
  63. return null;
  64. }
  65. return this.document_languages().filter(function(lang) { return this.includes(this.user_languages(), lang)}, this)[0]
  66. }
  67. LanguageDetector.extract_language = function(value) {
  68. return value.split('-')[0]
  69. }
  70. LanguageDetector.checkIfBetterLanguageAvailable = function() {
  71. var another_language = this.offer_language()
  72. if(!another_language) { return }
  73. this.show(another_language)
  74. }
  75. LanguageDetector.show = function(lang) {
  76. var elem = new LanguageBannerElement(lang)
  77. document.querySelector('.js-wrapper').prepend(elem.el)
  78. }
  79. LanguageDetector.checkIfBetterLanguageAvailable()
  80. }())