diff options
| author | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2023-12-27 13:00:45 +0000 |
|---|---|---|
| committer | Joe Carstairs <jcarstairs@scottlogic.com> | 2024-01-29 10:51:49 +0000 |
| commit | 4076758831660f252b844e05570deedce1d28462 (patch) | |
| tree | e3fd69818b6e0188f9cb5a9f7446ff5a6e841fe4 | |
| parent | ee11fa260f66f3afbbc4a5ecbedc42eb0f19fe5a (diff) | |
Pulls out getLocaleFromDocument()
| -rw-r--r-- | src/lib/getLocaleFromDocument.ts | 22 | ||||
| -rw-r--r-- | src/pages/[locale]/sign-up/index.astro | 4 | ||||
| -rw-r--r-- | src/pages/[locale]/sign-up/success.astro | 4 |
3 files changed, 26 insertions, 4 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; +} diff --git a/src/pages/[locale]/sign-up/index.astro b/src/pages/[locale]/sign-up/index.astro index 4f56f3c..5c6c056 100644 --- a/src/pages/[locale]/sign-up/index.astro +++ b/src/pages/[locale]/sign-up/index.astro @@ -31,17 +31,17 @@ const t = translate(locale); <script> import type { GetRegistrationOptionsResponseBody } from '$lib/api'; - import type Locale from '$types/Locale'; import * as api from '$lib/api'; import { startRegistration } from '@simplewebauthn/browser'; import UserAlreadyExistsError from '$types/UserAlreadyExistsError.d'; + import { getLocaleFromDocumentOrThrow } from '$lib/getLocaleFromDocument'; const form = document.getElementById('sign-up-form') as HTMLFormElement; const displayNameInput = document.getElementById('display-name') as HTMLInputElement; const emailInput = document.getElementById('email') as HTMLInputElement; const submitButton = document.getElementsByTagName('button')[0] as HTMLButtonElement; - const locale = document.getElementsByTagName('html')[0].lang as Locale; + const locale = getLocaleFromDocumentOrThrow(document); const statusInfoMsg = document.createElement('p'); statusInfoMsg.textContent = locale === 'en-GB' ? 'Signing you up…' : 'Signin you up…'; diff --git a/src/pages/[locale]/sign-up/success.astro b/src/pages/[locale]/sign-up/success.astro index 175b6ab..18b37e3 100644 --- a/src/pages/[locale]/sign-up/success.astro +++ b/src/pages/[locale]/sign-up/success.astro @@ -20,9 +20,9 @@ const t = translate(locale); <script> import translate from '$i18n/translate'; import tPage from '$i18n/translations/pages/signUp/success'; - import { getLocaleFromPathOrThrow } from '$lib/getLocaleFromPath'; + import { getLocaleFromDocumentOrThrow } from '$lib/getLocaleFromDocument'; - const locale = getLocaleFromPathOrThrow(window.location.pathname); + const locale = getLocaleFromDocumentOrThrow(document); const t = translate(locale); const heading = document.getElementsByTagName('h1')[0]; |
