All files / utils/store translation-store.js

100% Statements 154/154
100% Branches 28/28
100% Functions 6/6
100% Lines 154/154

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 1551x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 23x 23x 23x 23x 23x 23x 23x 23x 23x 1x 1x 1x 1x 1x 1x 1x 1x 1x 97x 97x 97x 85x 85x 12x 12x 12x 12x 12x 1x 1x 1x 1x 1x 1x 1x 53x 53x 16x 16x 37x 53x 13x 13x 53x 3x 3x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 1x 1x 1x 1x 1x 23x 23x 1x 1x 1x 70x 26x 26x 44x 44x 44x 70x 7x 7x 37x 37x 70x 13x 13x 3x 3x 13x 70x 13x 13x 37x 37x 37x 37x 37x 1x 1x 1x 1x 1x 1x 1x 1x 1x 25x 25x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import { importTranslations } from '../i18n-importer/i18n-importer.js'
import { normalizeI18nDefinitionMap } from '../i18n-normalizer/i18n-normalizer.js'
 
const emptyObj = Object.freeze({})
const initialDataStore = Object.freeze({
  languages: emptyObj,
  location: '',
})
 
/**
 * Normalizes `StoreDataEntry.data`
 * @param {StoreDataEntry} data - target data
 * @returns {{ location: string; languages: NormalizedI18nDefinitionMap }} normalized data
 */
function normalizeTranslationData (data) {
  const normalizedLanguages = normalizeI18nDefinitionMap(data.languages)
  normalizedLanguages.errors.forEach((error) => console.error('Error on %s::%s, %s', data.location, error.path, error.message))
  normalizedLanguages.warnings.forEach((error) => console.warn('Warning on %s::%s, %s', data.location, error.path, error.message))
  return {
    location: data.location,
    languages: structuredClone(normalizedLanguages.result),
  }
}
 
/** @type {WeakMap<TranslationStore, Record<string, Translations>>} */
const memoizedTranslationsMap = new WeakMap()
 
/**
 *
 * @param {TranslationStore} store - target store
 * @returns {Record<string, Translations>} - memoized translations data
 */
function getMemoizedTranslations (store) {
  const memoizedTranslations = memoizedTranslationsMap.get(store)
  if (memoizedTranslations) {
    return memoizedTranslations
  }
  /** @type {Record<string, Translations>} */
  const newMemo = {}
  memoizedTranslationsMap.set(store, newMemo)
  return newMemo
}
 
/**
 * @param {TranslationStore} store - target store
 * @param {string} locale - target locale
 * @returns {Promise<Translations>} translations on a specific locale
 */
const getTranslationsFromData = async (store, locale) => {
  const computed = getMemoizedTranslations(store)
  if (computed[locale]) {
    return computed[locale]
  }
  const definition = store.data.languages[locale]
  if (!definition) {
    return {}
  }
  if (!Array.isArray(definition.import) || definition.import.length <= 0) {
    return definition.translations
  }
  const translationsPromises = definition.import.map(extend => importTranslations(extend, store.data.location))
  const translationsArray = await Promise.all(translationsPromises)
  const importedTranslations = translationsArray.reduce((acc, translations) => ({ ...acc, ...translations }))
 
  computed[locale] = {
    ...importedTranslations,
    ...definition.translations,
  }
  return computed[locale]
}
 
/** @type {TranslationStore} */
const StorePrototype = {
 
  loadTranslations (data) {
    this.data = normalizeTranslationData(data)
    memoizedTranslationsMap.delete(this)
  },
 
  async translationsFromLanguage (locale) {
    if (typeof locale === 'string') {
      return await this.translationsFromLanguage(new Intl.Locale(locale))
    }
 
    const memoizedTranslations = getMemoizedTranslations(this)
 
    if (memoizedTranslations[locale.baseName]) {
      return memoizedTranslations[locale.baseName]
    }
    const languages = [locale.baseName]
    const intlLang = locale.language
    if (locale.region != null) {
      const langRegion = `${intlLang}-${locale.region}`
      if (!languages.includes(langRegion)) {
        languages.push(langRegion)
      }
    }
    if (!languages.includes(intlLang)) {
      languages.push(intlLang)
    }
    const translationsPromises = languages.reverse().map(language => getTranslationsFromData(this, language))
    const translationsArray = await Promise.all(translationsPromises)
    const result = translationsArray.reduce((acc, translations) => ({ ...acc, ...translations }))
    memoizedTranslations[locale.baseName] = result
    return result
  },
  data: initialDataStore,
}
 
/**
 * Creates a translation store
 * @returns {TranslationStore} new translation store
 */
export function i18nTranslationStore () {
  return Object.create(StorePrototype)
}
 
//= == Type declarations
 
/** @typedef {import('../i18n-normalizer/i18n-normalizer.js').I18nDefinitionMap} I18nDefinitionMap */
/** @typedef {import('../i18n-normalizer/i18n-normalizer.js').NormalizedI18nDefinitionMap} NormalizedI18nDefinitionMap */
 
/** @typedef {import('../i18n-normalizer/i18n-normalizer.js').Translations} Translations */
 
/**
 * @typedef {object} StoreData
 *
 * Data of {@link TranslationStore}
 * @property {NormalizedI18nDefinitionMap} languages - Store data languages
 * @property {string} location - location of stored data, used to get relative path to load additional i18n files
 */
 
/**
 * @typedef {object} StoreDataEntry
 *
 * Data to used to save in {@link TranslationStore}, it will be normalized to  {@link StoreData} on {@link TranslationStore.loadTranslations} call
 * @property {I18nDefinitionMap} languages - Store data languages
 * @property {string} location - location of stored data, used to get relative path to load additional i18n files
 */
 
/**
 * @typedef {object} TranslationStore
 *
 * Data to used to save in {@link TranslationStore}, it will be normalized to  {@link StoreData} on {@link TranslationStore.loadTranslations} call
 * @property {(data: StoreDataEntry) => void} loadTranslations - loads translations in data
 * @property {TranslationsFromLanguage} translationsFromLanguage - get stored translations from a locale
 * @property {StoreData} data - Store data where all information is saved with loadTranslations()
 */
 
/**
 * @callback TranslationsFromLanguage
 * @param {string | Intl.Locale} locale - Intl.Locale of a string definition
 * @returns {Promise<Translations>} empty locale, if locale string invalid, otherwise all translations from language
 */