agent-ecosystem/landing/composables/useLocation.ts
iliya eda6218e67 feat(landing): expand localization support and improve content handling
- Added support for additional languages: Chinese (zh), Spanish (es), Hindi (hi), Arabic (ar), and Portuguese (pt) in the LanguageSwitcher and i18n configuration.
- Updated content files for English and Russian to clarify the free usage of the app.
- Enhanced content management by importing new language files into the content structure.
- Adjusted locale handling to ensure proper recognition of supported languages in the application.
2026-03-23 18:53:43 +02:00

43 lines
1.4 KiB
TypeScript

import { supportedLocales } from "~/data/i18n";
import { useLocaleStore } from "~/stores/locale";
export const useLocation = () => {
const nuxtApp = useNuxtApp();
const i18n = nuxtApp.$i18n;
const localeStore = useLocaleStore();
const cookie = useCookie("i18n_redirected", { default: () => "" });
const getBrowserLocale = () => {
if (!process.client) return "en";
const browserLocale = navigator.language || "en";
const normalized = browserLocale.split("-")[0].toLowerCase();
const supported: readonly string[] = supportedLocales.map((item) => item.code);
return supported.includes(normalized) ? normalized : "en";
};
const initLocale = () => {
// Sync store with actual i18n locale (already resolved from route by nuxt-i18n)
const currentLocale = i18n?.locale?.value || "en";
if (cookie.value) {
// Cookie exists — sync store, but don't override route-based locale
localeStore.setLocale(currentLocale, false);
if (cookie.value !== currentLocale) {
cookie.value = currentLocale;
}
return;
}
// No cookie — detect from browser and set
const detected = getBrowserLocale();
localeStore.setLocale(detected, false);
if (i18n?.setLocale) {
i18n.setLocale(detected);
} else if (i18n?.locale?.value) {
i18n.locale.value = detected;
}
cookie.value = detected;
};
return { initLocale, getBrowserLocale };
};