All files i18n.js

12.5% Statements 8/64
0% Branches 0/27
4.55% Functions 1/22
12.7% Lines 8/63

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155        1x 1x                     1x       1x 1x 1x 1x 1x                                                                                                                                                                                                                                                                    
import Debounce from './debounce.js'
import i18nBackend from 'i18next-xhr-backend'
import i18next from 'i18next'
 
const DEFAULT_LOCALE = 'en'
const DEFAULT_DATE_TIME_OPTIONS = {
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  month: 'short',
  year: 'numeric'
}
 
class I18n {
 
  constructor() {
    this._attributeMap = [
      { attr: 'data-caption', selector: 'data-i18n-caption' },
      { attr: 'placeholder', selector: 'data-i18n-placeholder' },
    ]
    this._attributeFilters = ['data-i18n', 'data-i18n-placeholder', 'data-i18n-caption']
    this._translator = null
    this._locale = null
    this._i18next = i18next
    this._observer = null
  }
 
  getLocale() {
    return this._locale
  }
 
  formatDateTime(date, lng = this.getLocale()) {
    return new Date(date).toLocaleDateString(lng, DEFAULT_DATE_TIME_OPTIONS)
  }
 
  formatNumber(number, lng = this.getLocale()) {
    return new Intl.NumberFormat(lng).format(number)
  }
 
  initSessionLocale() {
    const searchParams = new URLSearchParams(window.location.search)
    const locale = searchParams.get('locale')
    if (locale) {
      sessionStorage.setItem('locale', locale)
    }
  }
 
  async init() {
    return new Promise(async (resolve, reject) => {
      const priorLocale = this._locale
      this.initSessionLocale()
 
      if (this._translator && priorLocale === this._locale) {
        this._translator = await this.setLocale(this._locale)
        resolve(this._translator)
        return
      }
 
      this._i18next.use(i18nBackend)
        .init({
          backend: {
            // for all available options read the backend's repository readme file
            loadPath: '/assets/locales/{{lng}}.json'
          },
          fallbackLng: DEFAULT_LOCALE,
          interpolation: {
            format: (value, format, lng) => {
              if (format === 'DateTime' || value instanceof Date) {
                return this.formatDateTime(value, lng)
              }
              if (format === 'FormatNumber') {
                return this.formatNumber(value, lng)
              }
              return value
            }
          }
        })
        .then(async () => {
          this._translator = await this.setLocale(this._locale)
          resolve(this._translator)
        }).catch(error => {
          reject(error)
        })
    })
  }
 
  /**
   * Sets the local based on a language key stored in session.
   */
  async setLocale(locale = null) {
    return new Promise(async (resolve, reject) => {
      if (!locale) {
        locale = window.sessionStorage.getItem('locale') || DEFAULT_LOCALE
      }
      this._locale = locale
      const lang = (locale.length > 2) ? locale.substring(0, 2) : locale
      document.documentElement.setAttribute('lang', lang)
 
      this._i18next.changeLanguage(locale)
        .then(translator => {
          this._translator = translator
          resolve(translator)
        }).catch(error => {
          reject(error)
        }
        )
    })
  }
 
  setPageTitle(i18lKey) {
    const titleTag = document.head.querySelector('title')
    if (titleTag && i18lKey) {
      titleTag.dataset.i18n = i18lKey
    }
  }
 
  translateAttribute({ attr, selector }) {
    const elements = Array.from(document.querySelectorAll(`[${selector}]`))
    elements.forEach(element => {
      const attribute = element.getAttribute(selector)
      element.setAttribute(attr, this._translator(attribute))
    })
  }
 
  /* One way to do 'static' HTML page translations is with client-side code that scans for a data attribute.
     This is an alternative to performing the translations on the back end.
  */
  translatePage() {
    const elements = Array.from(document.querySelectorAll('[data-i18n]'))
    elements.forEach(element => {
      const i18nKey = element.dataset.i18n
      element.textContent = this._translator(i18nKey)
    })
    this._attributeMap.forEach(def => this.translateAttribute(def))
  }
 
  async enableDocumentObserver() {
    if (!this._observer) {
      await this.init()
      const observerConfig = { attributeFilter: this._attributeFilters, attributes: true, characterData: false, characterDataOldValue: false, childList: true, subtree: true }
      const translateDebounce = Debounce(() => {
        this._observer.disconnect()
        this.translatePage()
        this._observer.observe(document, observerConfig)
      })
      this._observer = new MutationObserver(translateDebounce.bind(this))
      this._observer.observe(document, observerConfig)
      translateDebounce()
    }
  }
 
}
 
export default new I18n()