All files / utils/i18n-importer implementation.js

100% Statements 39/39
100% Branches 3/3
100% Functions 2/2
100% Lines 39/39

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 401x 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 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x  
import { normalizeI18nDefinitionMap, normalizeTranslations } from '../i18n-normalizer/i18n-normalizer.js'
import { provide } from './provider.js'
/** @import { Translations, I18nDefinitionMap } from './i18n-importer.js' */
 
/**
 * Imports translations from an URL
 * @param {string | URL} url - translations URL
 * @param {string | URL} baseUrl - base URL to search if it is a relative URL
 * @returns {Promise<Translations>} translations map
 */
export async function importTranslations (url, baseUrl) {
  const absoluteUrl = new URL(url, baseUrl)
  const response = await fetch(absoluteUrl)
  const json = await response.json()
  const normalizeResult = normalizeTranslations(json)
  normalizeResult.errors.forEach((error) => console.error('Error on %s::%s, %s', absoluteUrl.href, error.path, error.message))
  return normalizeResult.result
}
 
/**
 * Imports an i18n definition map from an URL
 * @param {string | URL} url - definition file URL
 * @param {string | URL} baseUrl - base URL to search if it is a relative URL
 * @returns {Promise<I18nDefinitionMap>} i18n definition map
 */
export async function importDefinitionMap (url, baseUrl) {
  const absoluteUrl = new URL(url, baseUrl)
  const response = await fetch(absoluteUrl)
  const json = await response.json()
  const normalizeResult = normalizeI18nDefinitionMap(json)
  normalizeResult.errors.forEach((error) => console.error('Error on %s::%s, %s', absoluteUrl.href, error.path, error.message))
  normalizeResult.warnings.forEach((warning) => console.warn('Warning on %s::%s, %s', absoluteUrl.href, warning.path, warning.message))
  return normalizeResult.result
}
 
provide({
  importTranslations,
  importDefinitionMap,
})