fix: language switching now works on all pages, not just root
All checks were successful
CI / update (push) Successful in 1m48s
All checks were successful
CI / update (push) Successful in 1m48s
LanguageSelector and language store previously only dispatched languagechange events on '/'. Now any page that isn't a recipe or faith route gets inline language switching via the custom event.
This commit is contained in:
@@ -29,8 +29,8 @@
|
|||||||
languageStore.set('en');
|
languageStore.set('en');
|
||||||
} else if (path.startsWith('/rezepte') || path.startsWith('/glaube')) {
|
} else if (path.startsWith('/rezepte') || path.startsWith('/glaube')) {
|
||||||
languageStore.set('de');
|
languageStore.set('de');
|
||||||
} else if (path === '/') {
|
} else {
|
||||||
// On main page, read from localStorage
|
// On other pages, read from localStorage
|
||||||
if (typeof localStorage !== 'undefined') {
|
if (typeof localStorage !== 'undefined') {
|
||||||
const preferredLanguage = localStorage.getItem('preferredLanguage');
|
const preferredLanguage = localStorage.getItem('preferredLanguage');
|
||||||
languageStore.set(preferredLanguage === 'en' ? 'en' : 'de');
|
languageStore.set(preferredLanguage === 'en' ? 'en' : 'de');
|
||||||
@@ -102,8 +102,10 @@
|
|||||||
// Get the current path directly from window
|
// Get the current path directly from window
|
||||||
const path = typeof window !== 'undefined' ? window.location.pathname : currentPath;
|
const path = typeof window !== 'undefined' ? window.location.pathname : currentPath;
|
||||||
|
|
||||||
// If on main page, dispatch event instead of reloading
|
// For pages that handle their own translations inline (not recipe/faith routes),
|
||||||
if (path === '/') {
|
// dispatch event and stay on the page
|
||||||
|
if (!path.startsWith('/rezepte') && !path.startsWith('/recipes')
|
||||||
|
&& !path.startsWith('/glaube') && !path.startsWith('/faith')) {
|
||||||
window.dispatchEvent(new CustomEvent('languagechange', { detail: { lang } }));
|
window.dispatchEvent(new CustomEvent('languagechange', { detail: { lang } }));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,12 +11,11 @@ function createLanguageStore() {
|
|||||||
init: () => {
|
init: () => {
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
const path = window.location.pathname;
|
const path = window.location.pathname;
|
||||||
if (path.startsWith('/recipes')) {
|
if (path.startsWith('/recipes') || path.startsWith('/faith')) {
|
||||||
set('en');
|
set('en');
|
||||||
} else if (path.startsWith('/rezepte')) {
|
} else if (path.startsWith('/rezepte') || path.startsWith('/glaube')) {
|
||||||
set('de');
|
set('de');
|
||||||
} else if (path === '/') {
|
} else {
|
||||||
// On main page, read from localStorage
|
|
||||||
const preferredLanguage = localStorage.getItem('preferredLanguage');
|
const preferredLanguage = localStorage.getItem('preferredLanguage');
|
||||||
set(preferredLanguage === 'en' ? 'en' : 'de');
|
set(preferredLanguage === 'en' ? 'en' : 'de');
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user