From f66d69653731c977d477f3f65f5ed2eb0bc787c9 Mon Sep 17 00:00:00 2001 From: elweyn Date: Thu, 14 Mar 2024 16:55:50 +0100 Subject: [PATCH 01/40] Make i18n for page titles. --- presenter/locales/extractLocale.ts | 18 ++++++++++++++++++ presenter/locales/index.ts | 2 ++ presenter/locales/locales.ts | 2 ++ presenter/renderer/+onBeforeRoute.ts | 19 +++++++++++++++++++ presenter/src/pages/_error/+title.ts | 5 +++++ presenter/src/pages/optin/+title.ts | 12 +++++++++++- 6 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 presenter/locales/extractLocale.ts create mode 100644 presenter/locales/index.ts create mode 100644 presenter/locales/locales.ts create mode 100644 presenter/renderer/+onBeforeRoute.ts diff --git a/presenter/locales/extractLocale.ts b/presenter/locales/extractLocale.ts new file mode 100644 index 0000000000..b61effd9ad --- /dev/null +++ b/presenter/locales/extractLocale.ts @@ -0,0 +1,18 @@ +import { locales, localeDefault } from './locales' + +export function extractLocale(url: string) { + const urlPaths = url.split('/') + let locale + let urlWithoutLocale + // We remove the URL locale, for example `/de-DE/about` => `/about` + const firstPath = urlPaths[1] + if (locales.filter((locale) => locale !== localeDefault).includes(firstPath)) { + locale = firstPath + urlWithoutLocale = '/' + urlPaths.slice(2).join('/') + } else { + locale = localeDefault + urlWithoutLocale = url + } + + return { locale, urlWithoutLocale } +} diff --git a/presenter/locales/index.ts b/presenter/locales/index.ts new file mode 100644 index 0000000000..1a7100b67e --- /dev/null +++ b/presenter/locales/index.ts @@ -0,0 +1,2 @@ +export { extractLocale } from './extractLocale' +export { locales, localeDefault } from './locales' diff --git a/presenter/locales/locales.ts b/presenter/locales/locales.ts new file mode 100644 index 0000000000..0f43c38f37 --- /dev/null +++ b/presenter/locales/locales.ts @@ -0,0 +1,2 @@ +export const locales = ['de', 'en'] +export const localeDefault = locales[0] diff --git a/presenter/renderer/+onBeforeRoute.ts b/presenter/renderer/+onBeforeRoute.ts new file mode 100644 index 0000000000..91bbc4f2c3 --- /dev/null +++ b/presenter/renderer/+onBeforeRoute.ts @@ -0,0 +1,19 @@ +import { PageContext } from 'vike/types' + +import { extractLocale } from '#root/locales/extractLocale' + +export function onBeforeRoute(pageContext: PageContext) { + const { urlWithoutLocale, locale } = extractLocale(pageContext.urlPathname) + // eslint-disable-next-line no-console + console.log('urlWithoutLocale', urlWithoutLocale) + // eslint-disable-next-line no-console + console.log('locale', locale) + return { + pageContext: { + // Make `locale` available as `pageContext.locale` + locale, + // Vike's router will use pageContext.urlLogical instead of pageContext.urlOriginal + urlLogical: urlWithoutLocale, + }, + } +} diff --git a/presenter/src/pages/_error/+title.ts b/presenter/src/pages/_error/+title.ts index 17faffcf02..73f4ec1aa8 100644 --- a/presenter/src/pages/_error/+title.ts +++ b/presenter/src/pages/_error/+title.ts @@ -1 +1,6 @@ +// import { usePageContext } from '#context/usePageContext' + +// const pageContext = usePageContext() + +// export const title = pageContext.locale === 'de-DE' ? 'DreamMall | Fehler' : '' export const title = 'DreamMall | Fehler' diff --git a/presenter/src/pages/optin/+title.ts b/presenter/src/pages/optin/+title.ts index 0260dfd2c5..8f783fc8ee 100644 --- a/presenter/src/pages/optin/+title.ts +++ b/presenter/src/pages/optin/+title.ts @@ -1 +1,11 @@ -export const title = 'DreamMall | Newsletter' +import { PageContext } from 'vike/types' + +// export const title = 'DreamMall | Newsletter' + +export function title(pageContext: PageContext) { + // const title = pageContext.locale === 'de' ? 'DreamMall | Newsletter' : 'DreamMall | Newsletter' + // eslint-disable-next-line no-console + console.log('pageContext', pageContext) + const title = 'DreamMall | Newsletter' + return title +} From e33dd421ac99e3ed027457449726885edbf786b6 Mon Sep 17 00:00:00 2001 From: elweyn Date: Sat, 16 Mar 2024 09:46:33 +0100 Subject: [PATCH 02/40] Locale in i18n.global.locale.value is 'de' or 'en' for testing set fixe default. --- presenter/locales/locales.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/presenter/locales/locales.ts b/presenter/locales/locales.ts index 0f43c38f37..056d414e46 100644 --- a/presenter/locales/locales.ts +++ b/presenter/locales/locales.ts @@ -1,2 +1,2 @@ export const locales = ['de', 'en'] -export const localeDefault = locales[0] +export const localeDefault = 'de' From a4e9ccfa93b1240f93fb5d8f17e97332b776e281 Mon Sep 17 00:00:00 2001 From: elweyn Date: Sat, 16 Mar 2024 09:47:07 +0100 Subject: [PATCH 03/40] Locale in i18n.global.locale.value is 'de' or 'en' for testing set fixe default. --- presenter/locales/extractLocale.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/presenter/locales/extractLocale.ts b/presenter/locales/extractLocale.ts index b61effd9ad..eed67e11aa 100644 --- a/presenter/locales/extractLocale.ts +++ b/presenter/locales/extractLocale.ts @@ -2,12 +2,12 @@ import { locales, localeDefault } from './locales' export function extractLocale(url: string) { const urlPaths = url.split('/') - let locale + let locale: 'en' | 'de' let urlWithoutLocale - // We remove the URL locale, for example `/de-DE/about` => `/about` + // We remove the URL locale, for example `/de/about` => `/about` const firstPath = urlPaths[1] if (locales.filter((locale) => locale !== localeDefault).includes(firstPath)) { - locale = firstPath + locale = 'en' urlWithoutLocale = '/' + urlPaths.slice(2).join('/') } else { locale = localeDefault From c2044a155775b8b4470950692bc23913f4710bd1 Mon Sep 17 00:00:00 2001 From: elweyn Date: Sat, 16 Mar 2024 09:47:50 +0100 Subject: [PATCH 04/40] We don't want to use 'de-DE' and use 'de' instead. --- presenter/src/pages/_error/+title.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/presenter/src/pages/_error/+title.ts b/presenter/src/pages/_error/+title.ts index 73f4ec1aa8..3d07e33baf 100644 --- a/presenter/src/pages/_error/+title.ts +++ b/presenter/src/pages/_error/+title.ts @@ -2,5 +2,5 @@ // const pageContext = usePageContext() -// export const title = pageContext.locale === 'de-DE' ? 'DreamMall | Fehler' : '' +// export const title = pageContext.locale === 'de' ? 'DreamMall | Fehler' : '' export const title = 'DreamMall | Fehler' From f547fe86903537097b41c5a3cd3b4ef1c59ece14 Mon Sep 17 00:00:00 2001 From: elweyn Date: Sat, 16 Mar 2024 09:48:35 +0100 Subject: [PATCH 05/40] Set i18n.global.locale.value to the value extracted out of the url. --- presenter/renderer/+onBeforeRoute.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/presenter/renderer/+onBeforeRoute.ts b/presenter/renderer/+onBeforeRoute.ts index 91bbc4f2c3..a9180e33fb 100644 --- a/presenter/renderer/+onBeforeRoute.ts +++ b/presenter/renderer/+onBeforeRoute.ts @@ -1,13 +1,17 @@ import { PageContext } from 'vike/types' +import i18n from '#plugins/i18n' import { extractLocale } from '#root/locales/extractLocale' export function onBeforeRoute(pageContext: PageContext) { + if (pageContext.urlPathname.endsWith('.js')) + return { pageContext: { urlLogical: pageContext.urlPathname } } const { urlWithoutLocale, locale } = extractLocale(pageContext.urlPathname) // eslint-disable-next-line no-console console.log('urlWithoutLocale', urlWithoutLocale) // eslint-disable-next-line no-console console.log('locale', locale) + i18n.global.locale.value = locale return { pageContext: { // Make `locale` available as `pageContext.locale` From a89eb31cf636caf6b01d2978620300fa785a46c1 Mon Sep 17 00:00:00 2001 From: elweyn Date: Sat, 16 Mar 2024 10:05:14 +0100 Subject: [PATCH 06/40] Change export function to export const title. --- presenter/src/pages/optin/+title.ts | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/presenter/src/pages/optin/+title.ts b/presenter/src/pages/optin/+title.ts index 8f783fc8ee..bddb594732 100644 --- a/presenter/src/pages/optin/+title.ts +++ b/presenter/src/pages/optin/+title.ts @@ -1,11 +1,11 @@ -import { PageContext } from 'vike/types' +// const title = pageContext.locale === 'de' ? 'DreamMall | Newsletter' : 'DreamMall | Newsletter' +export const title = 'DreamMall | Newsletter' -// export const title = 'DreamMall | Newsletter' - -export function title(pageContext: PageContext) { - // const title = pageContext.locale === 'de' ? 'DreamMall | Newsletter' : 'DreamMall | Newsletter' - // eslint-disable-next-line no-console - console.log('pageContext', pageContext) - const title = 'DreamMall | Newsletter' - return title -} +// import { PageContext } from 'vike/types' +// export function title(pageContext: PageContext) { +// // const title = pageContext.locale === 'de' ? 'DreamMall | Newsletter' : 'DreamMall | Newsletter' +// // eslint-disable-next-line no-console +// console.log('pageContext', pageContext) +// const title = 'DreamMall | Newsletter' +// return title +// } From e5ab2d7a529078da81986c09c81325f423d1d947 Mon Sep 17 00:00:00 2001 From: elweyn Date: Mon, 18 Mar 2024 10:23:59 +0100 Subject: [PATCH 07/40] Create localeStore. --- presenter/renderer/+onBeforeRoute.ts | 8 ++++---- presenter/src/stores/localeStore.ts | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 presenter/src/stores/localeStore.ts diff --git a/presenter/renderer/+onBeforeRoute.ts b/presenter/renderer/+onBeforeRoute.ts index a9180e33fb..5c31270b84 100644 --- a/presenter/renderer/+onBeforeRoute.ts +++ b/presenter/renderer/+onBeforeRoute.ts @@ -1,17 +1,17 @@ import { PageContext } from 'vike/types' -import i18n from '#plugins/i18n' import { extractLocale } from '#root/locales/extractLocale' +import { useLocaleStore } from '#stores/localeStore' export function onBeforeRoute(pageContext: PageContext) { - if (pageContext.urlPathname.endsWith('.js')) - return { pageContext: { urlLogical: pageContext.urlPathname } } + if (pageContext.urlPathname.endsWith('.js')) return { pageContext: {} } const { urlWithoutLocale, locale } = extractLocale(pageContext.urlPathname) // eslint-disable-next-line no-console console.log('urlWithoutLocale', urlWithoutLocale) // eslint-disable-next-line no-console console.log('locale', locale) - i18n.global.locale.value = locale + const localeStore = useLocaleStore() + localeStore.save(locale) return { pageContext: { // Make `locale` available as `pageContext.locale` diff --git a/presenter/src/stores/localeStore.ts b/presenter/src/stores/localeStore.ts new file mode 100644 index 0000000000..fa9fa63165 --- /dev/null +++ b/presenter/src/stores/localeStore.ts @@ -0,0 +1,18 @@ +import { defineStore } from 'pinia' + +import i18n from '#plugins/i18n' + +export const useLocaleStore = defineStore('locale', { + state: () => ({ locale: 'de' }), + actions: { + save(l: 'de' | 'en') { + this.locale = l + i18n.global.locale.value = l + }, + clear() { + this.locale = 'de' + i18n.global.locale.value = 'de' + }, + }, + persist: true, +}) From 85a6817604274306b1b811a3db56c379f2b3ef95 Mon Sep 17 00:00:00 2001 From: elweyn Date: Mon, 18 Mar 2024 10:25:58 +0100 Subject: [PATCH 08/40] Remove changes on +title.ts --- presenter/src/pages/_error/+title.ts | 5 ----- presenter/src/pages/optin/+title.ts | 10 ---------- 2 files changed, 15 deletions(-) diff --git a/presenter/src/pages/_error/+title.ts b/presenter/src/pages/_error/+title.ts index 3d07e33baf..17faffcf02 100644 --- a/presenter/src/pages/_error/+title.ts +++ b/presenter/src/pages/_error/+title.ts @@ -1,6 +1 @@ -// import { usePageContext } from '#context/usePageContext' - -// const pageContext = usePageContext() - -// export const title = pageContext.locale === 'de' ? 'DreamMall | Fehler' : '' export const title = 'DreamMall | Fehler' diff --git a/presenter/src/pages/optin/+title.ts b/presenter/src/pages/optin/+title.ts index bddb594732..0260dfd2c5 100644 --- a/presenter/src/pages/optin/+title.ts +++ b/presenter/src/pages/optin/+title.ts @@ -1,11 +1 @@ -// const title = pageContext.locale === 'de' ? 'DreamMall | Newsletter' : 'DreamMall | Newsletter' export const title = 'DreamMall | Newsletter' - -// import { PageContext } from 'vike/types' -// export function title(pageContext: PageContext) { -// // const title = pageContext.locale === 'de' ? 'DreamMall | Newsletter' : 'DreamMall | Newsletter' -// // eslint-disable-next-line no-console -// console.log('pageContext', pageContext) -// const title = 'DreamMall | Newsletter' -// return title -// } From db89d5ee2ff95d5045bbf1ed265d6bd4ef57592f Mon Sep 17 00:00:00 2001 From: elweyn Date: Tue, 19 Mar 2024 11:24:29 +0100 Subject: [PATCH 09/40] Add locale to pageContext. --- presenter/renderer/+onBeforeRoute.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/presenter/renderer/+onBeforeRoute.ts b/presenter/renderer/+onBeforeRoute.ts index 5c31270b84..793b05d46d 100644 --- a/presenter/renderer/+onBeforeRoute.ts +++ b/presenter/renderer/+onBeforeRoute.ts @@ -15,7 +15,7 @@ export function onBeforeRoute(pageContext: PageContext) { return { pageContext: { // Make `locale` available as `pageContext.locale` - locale, + locale: locale, // Vike's router will use pageContext.urlLogical instead of pageContext.urlOriginal urlLogical: urlWithoutLocale, }, From d40547eda9cca89f221f8a613527b612ade73f60 Mon Sep 17 00:00:00 2001 From: elweyn Date: Tue, 19 Mar 2024 11:25:26 +0100 Subject: [PATCH 10/40] Change html lang to store value. --- presenter/renderer/+onRenderHtml.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/presenter/renderer/+onRenderHtml.ts b/presenter/renderer/+onRenderHtml.ts index 531125a3c2..fdf223df3d 100644 --- a/presenter/renderer/+onRenderHtml.ts +++ b/presenter/renderer/+onRenderHtml.ts @@ -11,6 +11,7 @@ import { createApp } from './app' import { getDescription, getTitle } from './utils' import type { App } from 'vue' +import { useLocaleStore } from '#stores/localeStore' // this fixes a warning which occurs when building // > "resolveComponent" is imported from external module "vue" but never used in ... @@ -18,9 +19,10 @@ import type { App } from 'vue' resolveComponent('nothing') async function render(pageContext: PageContextServer & PageContext) { - const { app, i18n } = createApp(pageContext, false) + const { app /*, i18n */ } = createApp(pageContext, false) - const locale = i18n.global.locale.value + const localeStore = useLocaleStore() + const locale = localeStore.locale // i18n.global.locale.value const appHtml = await renderToString(app) From 94c691548a479dad4ca6a98187744a7bf9ee28df Mon Sep 17 00:00:00 2001 From: elweyn Date: Tue, 19 Mar 2024 11:27:38 +0100 Subject: [PATCH 11/40] Use localeStore as value of languageselector. --- presenter/src/components/language/LanguageSelector.vue | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/presenter/src/components/language/LanguageSelector.vue b/presenter/src/components/language/LanguageSelector.vue index 07fdbfb0cb..8201324ea9 100644 --- a/presenter/src/components/language/LanguageSelector.vue +++ b/presenter/src/components/language/LanguageSelector.vue @@ -1,6 +1,6 @@