mirror of
https://github.com/Rain-kl/OpenFlare.git
synced 2026-11-16 14:54:23 +08:00
feat(i18n): add Russian UI and harden localization
This commit is contained in:
@@ -1,17 +1,29 @@
|
||||
import { render, screen } from '@testing-library/react';
|
||||
import { useTranslations } from 'next-intl';
|
||||
import { useLocale, useTranslations } from 'next-intl';
|
||||
import { formatDateTime, formatNumber } from '@/i18n/format';
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
||||
|
||||
import enMessages from '@/messages/en.json';
|
||||
import zhCNMessages from '@/messages/zh-CN.json';
|
||||
import ruMessages from '@/messages/ru.json';
|
||||
|
||||
import { AppIntlProvider } from './intl-provider';
|
||||
import { createLocaleBootstrapScript } from '@/i18n/bootstrap';
|
||||
|
||||
function Probe() {
|
||||
const t = useTranslations('common');
|
||||
return <button>{t('save')}</button>;
|
||||
}
|
||||
|
||||
function FormattingProbe() {
|
||||
const locale = useLocale();
|
||||
return (
|
||||
<span data-testid='formats'>
|
||||
{locale}|{formatNumber(1234.5)}|{formatDateTime('2026-01-15T00:00:00Z')}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
|
||||
const zhSave = (zhCNMessages.common as Record<string, string>).save;
|
||||
const enSave = (enMessages.common as Record<string, string>).save;
|
||||
|
||||
@@ -31,6 +43,7 @@ describe('AppIntlProvider (static export locale resolution)', () => {
|
||||
|
||||
afterEach(() => {
|
||||
document.cookie = 'NEXT_LOCALE=; Max-Age=0; Path=/';
|
||||
document.documentElement.removeAttribute('data-locale-pending');
|
||||
});
|
||||
|
||||
it('flips to the NEXT_LOCALE cookie locale after mount when SSR locale is stale', async () => {
|
||||
@@ -61,6 +74,51 @@ describe('AppIntlProvider (static export locale resolution)', () => {
|
||||
expect(document.documentElement.lang).toBe('zh-CN');
|
||||
});
|
||||
|
||||
it('applies Russian cookies before showing the hydrated English page', () => {
|
||||
document.cookie = 'NEXT_LOCALE=ru; Path=/';
|
||||
new Function(createLocaleBootstrapScript('en'))();
|
||||
expect(document.documentElement.hasAttribute('data-locale-pending')).toBe(
|
||||
true,
|
||||
);
|
||||
render(
|
||||
<AppIntlProvider locale='en' messages={enMessages}>
|
||||
<Probe />
|
||||
</AppIntlProvider>,
|
||||
);
|
||||
expect(screen.getByText(ruMessages.common.save)).toBeInTheDocument();
|
||||
expect(document.documentElement.lang).toBe('ru');
|
||||
expect(document.documentElement.hasAttribute('data-locale-pending')).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
it('recognizes Russian browser variants when there is no preference', () => {
|
||||
Object.defineProperty(navigator, 'languages', {
|
||||
value: ['ru-RU', 'en-US'],
|
||||
configurable: true,
|
||||
});
|
||||
render(
|
||||
<AppIntlProvider locale='en' messages={enMessages}>
|
||||
<Probe />
|
||||
</AppIntlProvider>,
|
||||
);
|
||||
expect(screen.getByText(ruMessages.common.save)).toBeInTheDocument();
|
||||
expect(document.documentElement.lang).toBe('ru');
|
||||
});
|
||||
|
||||
it('formats existing data in Russian on the first localized render', () => {
|
||||
document.documentElement.lang = 'en';
|
||||
document.cookie = 'NEXT_LOCALE=ru; Path=/';
|
||||
render(
|
||||
<AppIntlProvider locale='en' messages={enMessages}>
|
||||
<FormattingProbe />
|
||||
</AppIntlProvider>,
|
||||
);
|
||||
expect(screen.getByTestId('formats').textContent).toBe(
|
||||
'ru|1\u00a0234,5|15.01.2026, 08:00',
|
||||
);
|
||||
});
|
||||
|
||||
it('does not flip without a cookie preference', () => {
|
||||
render(
|
||||
<AppIntlProvider locale='zh-CN' messages={zhCNMessages}>
|
||||
|
||||
@@ -7,11 +7,13 @@ import type { ReactNode } from 'react';
|
||||
import { resolveClientLocale } from '@/i18n/client';
|
||||
import type { AppLocale } from '@/i18n/config';
|
||||
import enMessages from '@/messages/en.json';
|
||||
import ruMessages from '@/messages/ru.json';
|
||||
import zhCNMessages from '@/messages/zh-CN.json';
|
||||
|
||||
const messagesByLocale: Record<AppLocale, AbstractIntlMessages> = {
|
||||
'zh-CN': zhCNMessages,
|
||||
en: enMessages,
|
||||
ru: ruMessages,
|
||||
};
|
||||
|
||||
type Props = {
|
||||
@@ -32,14 +34,25 @@ export function AppIntlProvider({
|
||||
|
||||
useLayoutEffect(() => {
|
||||
const resolved = resolveClientLocale();
|
||||
// Formatting utilities run during the context update's render. Publish
|
||||
// its locale first, while the prepaint gate still hides stale markup.
|
||||
document.documentElement.lang = resolved;
|
||||
if (resolved !== ssrLocale) {
|
||||
setClientLocale(resolved);
|
||||
}
|
||||
document.documentElement.lang = resolved;
|
||||
}, [ssrLocale]);
|
||||
|
||||
const locale = clientLocale ?? ssrLocale;
|
||||
|
||||
useLayoutEffect(() => {
|
||||
if (locale === resolveClientLocale()) {
|
||||
document.documentElement.lang = locale;
|
||||
const fallback = document.getElementById('locale-load-fallback');
|
||||
if (fallback) fallback.hidden = true;
|
||||
document.documentElement.removeAttribute('data-locale-pending');
|
||||
}
|
||||
}, [locale]);
|
||||
|
||||
return (
|
||||
<NextIntlClientProvider
|
||||
locale={locale}
|
||||
|
||||
Reference in New Issue
Block a user