From ac18724bb87b66ec6774f3b7515774c35eec9950 Mon Sep 17 00:00:00 2001 From: Adam Matthiesen Date: Tue, 12 Nov 2024 22:54:59 -0800 Subject: [PATCH] start of i18n support --- .../src/components/OAuthButtonStack.astro | 6 +- .../src/components/StaticAuthCheck.astro | 33 + packages/studiocms_auth/src/integration.ts | 8 +- .../src/layouts/AuthLayout.astro | 14 +- .../src/routes/{ => api}/logout.ts | 0 .../studiocms_auth/src/routes/login.astro | 43 +- .../studiocms_auth/src/routes/logout.astro | 33 + .../studiocms_auth/src/routes/signup.astro | 48 +- .../studiocms_auth/src/utils/routeBuilder.ts | 4 +- .../studiocms_core/src/helpers/routemap.ts | 1 + .../src/i18n/LanguageSelector.astro | 103 +++ packages/studiocms_core/src/i18n/index.ts | 151 +++++ .../src/i18n/translations/en-us.json | 43 ++ packages/studiocms_core/src/integration.ts | 2 + packages/studiocms_core/src/stubs/i18n-dts.ts | 107 +++ .../src/utils/coreVirtualModules.ts | 1 + pnpm-lock.yaml | 641 +++++++++++++----- pnpm-workspace.yaml | 4 +- 18 files changed, 1039 insertions(+), 203 deletions(-) create mode 100644 packages/studiocms_auth/src/components/StaticAuthCheck.astro rename packages/studiocms_auth/src/routes/{ => api}/logout.ts (100%) create mode 100644 packages/studiocms_auth/src/routes/logout.astro create mode 100644 packages/studiocms_core/src/i18n/LanguageSelector.astro create mode 100644 packages/studiocms_core/src/i18n/index.ts create mode 100644 packages/studiocms_core/src/i18n/translations/en-us.json create mode 100644 packages/studiocms_core/src/stubs/i18n-dts.ts diff --git a/packages/studiocms_auth/src/components/OAuthButtonStack.astro b/packages/studiocms_auth/src/components/OAuthButtonStack.astro index 731c4f2057..bae54cdfc1 100644 --- a/packages/studiocms_auth/src/components/OAuthButtonStack.astro +++ b/packages/studiocms_auth/src/components/OAuthButtonStack.astro @@ -1,12 +1,16 @@ --- +import { getLangFromUrl, useTranslations } from 'studiocms:i18n'; import { Divider } from '@studiocms/ui/components'; import OAuthButton from './OAuthButton.astro'; import { providerData, showOAuth } from './oAuthButtonProviders'; +const lang = getLangFromUrl(Astro.url); +const t = useTranslations(lang, '@studiocms/auth:oauth-stack'); + const shouldShowOAuth = showOAuth && providerData.some(({ enabled }) => enabled); --- { shouldShowOAuth && ( - or log in using + {t('or-login-with')}
{ providerData.map(({enabled, ...props}) => enabled && ) diff --git a/packages/studiocms_auth/src/components/StaticAuthCheck.astro b/packages/studiocms_auth/src/components/StaticAuthCheck.astro new file mode 100644 index 0000000000..8452ea8976 --- /dev/null +++ b/packages/studiocms_auth/src/components/StaticAuthCheck.astro @@ -0,0 +1,33 @@ +--- +import { getUserData } from 'studiocms:auth/lib/user'; +import { StudioCMSRoutes } from 'studiocms:helpers/routemap'; +import { getLangFromUrl, useTranslatedPath } from 'studiocms:i18n'; + +const { isLoggedIn } = await getUserData(Astro); +// Get the language and translations +const referer = Astro.request.headers.get('referer'); + +if (!referer) { + throw new Error('No referer found'); +} + +const lang = getLangFromUrl(new URL(referer)); +const translatePath = useTranslatedPath(lang); +const { + mainLinks: { dashboardIndex }, +} = StudioCMSRoutes; +--- + + + \ No newline at end of file diff --git a/packages/studiocms_auth/src/integration.ts b/packages/studiocms_auth/src/integration.ts index 9d2471ac6f..a47d202985 100644 --- a/packages/studiocms_auth/src/integration.ts +++ b/packages/studiocms_auth/src/integration.ts @@ -83,6 +83,7 @@ export default defineIntegration({ }, experimental: { directRenderScript: true, + serverIslands: true, }, vite: { optimizeDeps: { @@ -112,6 +113,11 @@ export default defineIntegration({ entrypoint: resolve('./routes/api/login.ts'), enabled: usernameAndPasswordAPI, }, + { + pattern: 'logout', + entrypoint: resolve('./routes/api/logout.ts'), + enabled: dashboardEnabled && !options.dbStartPage, + }, { pattern: 'register', entrypoint: resolve('./routes/api/register.ts'), @@ -170,7 +176,7 @@ export default defineIntegration({ }, { pattern: 'logout/', - entrypoint: resolve('./routes/logout.ts'), + entrypoint: resolve('./routes/logout.astro'), enabled: dashboardEnabled && !options.dbStartPage, }, { diff --git a/packages/studiocms_auth/src/layouts/AuthLayout.astro b/packages/studiocms_auth/src/layouts/AuthLayout.astro index e378f984bc..8d79a69273 100644 --- a/packages/studiocms_auth/src/layouts/AuthLayout.astro +++ b/packages/studiocms_auth/src/layouts/AuthLayout.astro @@ -21,10 +21,11 @@ const { interface Props { title: string; description: string; - lang?: string; + lang: string; + disableScreen?: boolean; } -const { title, description, lang = 'en' } = Astro.props; +const { title, description, lang, disableScreen } = Astro.props; // Get the site config const siteConfig = await db @@ -37,9 +38,10 @@ const siteConfig = await db const loginPageBackground = siteConfig?.loginPageBackground; const loginPageCustomImage = siteConfig?.loginPageCustomImage; -const fallbackImageSrc = loginPageBackground === 'custom' - ? loginPageCustomImage - : validImages.find((x) => x.name !== 'custom' && x.name === loginPageBackground)?.dark; // TODO: Adapt to theme +const fallbackImageSrc = + loginPageBackground === 'custom' + ? loginPageCustomImage + : validImages.find((x) => x.name !== 'custom' && x.name === loginPageBackground)?.dark; // TODO: Adapt to theme --- @@ -87,7 +89,7 @@ const fallbackImageSrc = loginPageBackground === 'custom' - + { !disableScreen && }