schon/storefront/app/composables/languages/useLanguage.ts
Alexandr SaVBaD Waltz 2ea18eb8a6 feat(storefront): refactor i18n and cart/wishlist handling for improved user experience
Refactored i18n configuration, replacing `DEFAULT_LOCALE` with `DEFAULT_LOCALE_FALLBACK` and enhancing environment-based locale validation. Improved cookie persistence for cart and wishlist, ensuring fallback handling for unauthenticated users.

Enhancements:
- Added `createProjectKey` utility for consistent project key generation.
- Reworked cart and wishlist composables (`useOrderOverwrite`, `useWishlistOverwrite`) to decouple product identifier and handle cookies robustly.
- Centralized `DEFAULT_LOCALE` logic for better maintainability.
- Refined `useOrderSync` and `useWishlistSync` for clean synchronization across auth states.
- Updated SCSS in hero and header styles for alignment corrections.

Breaking Changes: `DEFAULT_LOCALE` constant removed; replaced with runtime config and fallback logic. Consumers must adapt to `DEFAULT_LOCALE_FALLBACK` and `$appHelpers.DEFAULT_LOCALE`.
2026-02-28 22:38:45 +03:00

37 lines
1 KiB
TypeScript

import { SUPPORTED_LOCALES } from '@appConstants';
import { GET_LANGUAGES } from '@graphql/queries/standalone/languages.js';
import type { ILanguage, ILanguagesResponse } from '@types';
export async function useLanguages() {
const languageStore = useLanguageStore();
const { $appHelpers } = useNuxtApp();
const cookieLocale = useCookie($appHelpers.COOKIES_LOCALE_KEY, {
default: () => $appHelpers.DEFAULT_LOCALE,
path: '/',
});
const { data, error } = await useAsyncQuery<ILanguagesResponse>(GET_LANGUAGES);
if (!error.value && data.value?.languages) {
const filteredLanguages = data.value.languages.filter((locale: ILanguage) =>
SUPPORTED_LOCALES.some((supportedLocale) => supportedLocale.code === locale.code),
);
languageStore.setLanguages(filteredLanguages);
const currentLocale = filteredLanguages.find((locale) => locale.code === cookieLocale.value);
if (currentLocale) {
languageStore.setCurrentLocale(currentLocale);
}
}
watch(error, (err) => {
if (err) {
console.error('useLanguage error:', err);
}
});
return {};
}