summaryrefslogtreecommitdiff
path: root/src/lib/getLocaleFromDocument.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/getLocaleFromDocument.ts')
-rw-r--r--src/lib/getLocaleFromDocument.ts22
1 files changed, 22 insertions, 0 deletions
diff --git a/src/lib/getLocaleFromDocument.ts b/src/lib/getLocaleFromDocument.ts
new file mode 100644
index 0000000..83a27ac
--- /dev/null
+++ b/src/lib/getLocaleFromDocument.ts
@@ -0,0 +1,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;
+}