summaryrefslogtreecommitdiff
path: root/src/lib/getLocaleFromDocument.ts
blob: 83a27ac36e629deab47bb8f9c0dffbca1b75e4f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import locales from '$i18n/locales';
import type Locale from '$types/Locale';

/**
 * Gets the locale from the provided document.
 *
 * Assuming the document is generated using a layout inherited from Layout, the
 * locale should be set in the `lang` attribute of the <html> element.
 *
 * @returns {Locale}
 */
export function getLocaleFromDocumentOrThrow(d: Document) {
  const locale = d.getElementsByTagName('html')[0].lang as Locale;
  if (!locales.includes(locale)) {
    throw new Error(`
      Tried to find a supported locale in the lang attribute of the HTML
      element, but none was found. Found: "${locale}".
    `);
  }

  return locale;
}