- Add localization-service microservice (locale resolution, translations) - Add frontend API routes /api/locale and /api/translations/[locale] - Add LocalizationProvider and localization context - Integrate localization into layout, EmptyChat, MessageInput components - Update MICROSERVICES.md architecture docs - Add localization-service to workspaces Co-authored-by: Cursor <cursoragent@cursor.com>
70 lines
2.0 KiB
TypeScript
70 lines
2.0 KiB
TypeScript
import type { GeoDeviceContext, LocalizationContext } from '../types.js';
|
|
import {
|
|
resolveLocaleFromCountry,
|
|
normalizeAcceptLanguage,
|
|
getDefaultLocale,
|
|
} from './countryToLocale.js';
|
|
|
|
/**
|
|
* Определяет locale на основе geo-контекста.
|
|
* Приоритет: геопозиция > client.language > Accept-Language > fallback.
|
|
* Геопозиция первая — если пользователь в России, показываем русский.
|
|
*/
|
|
export function resolveLocale(ctx: GeoDeviceContext | null): LocalizationContext {
|
|
const fallback: LocalizationContext = {
|
|
locale: getDefaultLocale(),
|
|
language: getDefaultLocale(),
|
|
region: null,
|
|
countryCode: null,
|
|
timezone: null,
|
|
source: 'fallback',
|
|
};
|
|
|
|
if (!ctx) return fallback;
|
|
|
|
const geo = ctx.geo;
|
|
const acceptLang = normalizeAcceptLanguage(ctx.acceptLanguage);
|
|
const clientLang = ctx.client?.language
|
|
? ctx.client.language.split('-')[0]?.toLowerCase()
|
|
: null;
|
|
|
|
// 1. Геопозиция (countryCode) — приоритет при определении по IP
|
|
if (geo?.countryCode) {
|
|
const locale = resolveLocaleFromCountry(geo.countryCode);
|
|
return {
|
|
locale,
|
|
language: locale,
|
|
region: geo.region ?? null,
|
|
countryCode: geo.countryCode,
|
|
timezone: geo.timezone ?? null,
|
|
source: 'geo',
|
|
};
|
|
}
|
|
|
|
// 2. Язык из client (браузер navigator.language)
|
|
if (clientLang) {
|
|
return {
|
|
locale: clientLang,
|
|
language: clientLang,
|
|
region: geo?.region ?? null,
|
|
countryCode: geo?.countryCode ?? null,
|
|
timezone: geo?.timezone ?? null,
|
|
source: 'client',
|
|
};
|
|
}
|
|
|
|
// 3. Accept-Language заголовок
|
|
if (acceptLang) {
|
|
return {
|
|
locale: acceptLang,
|
|
language: acceptLang,
|
|
region: geo?.region ?? null,
|
|
countryCode: geo?.countryCode ?? null,
|
|
timezone: geo?.timezone ?? null,
|
|
source: 'accept-language',
|
|
};
|
|
}
|
|
|
|
return fallback;
|
|
}
|