<commit_before>import Vue from 'vue'
import VueI18n from 'vue-i18n'
import { Logger } from '@vue-storefront/core/lib/logger'
import { once } from '@vue-storefront/core/helpers'
import config from 'config'

once('__VUE_EXTEND_I18N__', () => {
  Vue.use(VueI18n)
})

const loadedLanguages = ['en-US']
const i18n = new VueI18n({
  locale: config.i18n.bundleAllStoreviewLanguages ? config.i18n.defaultLocale : 'en-US', // set locale
  fallbackLocale: 'en-US',
  messages: config.i18n.bundleAllStoreviewLanguages ? require('./resource/i18n/multistoreLanguages.json') : {
    'en-US': require('./resource/i18n/en-US.json')
  }
})

function setI18nLanguage (lang: string): string {
  i18n.locale = lang
  return lang
}

export function loadLanguageAsync (lang: string): Promise<string> {
  if (!config.i18n.bundleAllStoreviewLanguages) {
    if (i18n.locale !== lang) {
      if (!loadedLanguages.includes(lang)) {
        return import(/* webpackChunkName: "lang-[request]" */ `./resource/i18n/${lang}.json`).then(msgs => {
          i18n.setLocaleMessage(lang, msgs.default)
          loadedLanguages.push(lang)
          return setI18nLanguage(lang)
        }).catch(err => { // eslint-disable-line handle-callback-err
          Logger.debug('Unable to load translation')()
          return ''
        })
      }
      return Promise.resolve(setI18nLanguage(lang))
    }
  } else {
    loadedLanguages.push(lang)
    return Promise.resolve(setI18nLanguage(lang))
  }
  return Promise.resolve(lang)
}

loadLanguageAsync(config.i18n.defaultLocale)

export default i18n<commit_msg>lazy loading date locale files on language switch<commit_after>import Vue from 'vue'
import VueI18n from 'vue-i18n'
import { Logger } from '@vue-storefront/core/lib/logger'
import { once } from '@vue-storefront/core/helpers'
import config from 'config'

once('__VUE_EXTEND_I18N__', () => {
  Vue.use(VueI18n)
})

const loadedLanguages = ['en-US']
const i18n = new VueI18n({
  locale: config.i18n.bundleAllStoreviewLanguages ? config.i18n.defaultLocale : 'en-US', // set locale
  fallbackLocale: 'en-US',
  messages: config.i18n.bundleAllStoreviewLanguages ? require('./resource/i18n/multistoreLanguages.json') : {
    'en-US': require('./resource/i18n/en-US.json')
  }
})

function setI18nLanguage (lang: string): string {
  i18n.locale = lang
  return lang
}

/**
 * Lazy load date locales file for current switched language.
 */
const loadDateLocales = async (lang: string = 'en'): Promise<void> => {
  let localeCode = lang.toLocaleLowerCase()
  try { // try to load full locale name
    await import(/* webpackChunkName: "dayjs-locales" */ `dayjs/locale/${localeCode}`)
  } catch (e) { // load simplified locale name, example: de-DE -> de
    const separatorIndex = localeCode.indexOf('-')
    if (separatorIndex) {
      localeCode = separatorIndex ? localeCode.substr(0, separatorIndex) : localeCode
      await import(/* webpackChunkName: "dayjs-locales" */ `dayjs/locale/${localeCode}`)
    }
  }
}

export async function loadLanguageAsync (lang: string): Promise<string> {
  await loadDateLocales(lang)
  if (!config.i18n.bundleAllStoreviewLanguages) {
    if (i18n.locale !== lang) {
      if (!loadedLanguages.includes(lang)) {
        try {
          const msgs = await import(/* webpackChunkName: "lang-[request]" */ `./resource/i18n/${lang}.json`)
          i18n.setLocaleMessage(lang, msgs.default)
          loadedLanguages.push(lang)
          return setI18nLanguage(lang)
        } catch (e) { // eslint-disable-line handle-callback-err
          Logger.debug('Unable to load translation')()
          return ''
        }
      }
      return setI18nLanguage(lang)
    }
  } else {
    loadedLanguages.push(lang)
    return setI18nLanguage(lang)
  }
  return lang
}

loadLanguageAsync(config.i18n.defaultLocale)

export default i18n
{'fraction_non_alphanumeric': 0.1365062761506276, 'fraction_numerical': 0.023274058577405856, 'mean_word_length': 3.5808383233532934, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4181679', 'n_tokens_mistral': 1308, 'n_tokens_neox': 1264, 'n_words': 256}