Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/desktop/src/renderer/src/components/LanguageToggle.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { setLocale as applyLocale, getCurrentLocale, useT } from '@open-codesign/i18n';
import type { Locale } from '@open-codesign/i18n';
import { Globe } from 'lucide-react';
import type { CSSProperties } from 'react';
import { useEffect, useState } from 'react';

const noDragStyle = { WebkitAppRegion: 'no-drag' } as CSSProperties;

function nextLocale(locale: Locale): Locale {
return locale === 'en' ? 'zh-CN' : 'en';
}
Expand Down Expand Up @@ -30,6 +33,7 @@ export function LanguageToggle() {
<button
type="button"
onClick={() => void handleToggle()}
style={noDragStyle}
className="inline-flex items-center gap-2 h-8 px-3 rounded-[var(--radius-md)] border border-[var(--color-border)] bg-[var(--color-surface)] text-[12px] text-[var(--color-text-primary)] hover:bg-[var(--color-surface-hover)] transition-colors"
aria-label={t('settings.language.label')}
title={t('settings.language.label')}
Expand Down
8 changes: 5 additions & 3 deletions apps/desktop/src/renderer/src/components/Settings.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { setLocale as applyLocale } from '@open-codesign/i18n';
import type {
OnboardingState,
PROVIDER_SHORTLIST,
Expand Down Expand Up @@ -777,9 +778,10 @@ function AppearanceTab() {

async function handleLocaleChange(v: string) {
if (!window.codesign) return;
setLocale(v);
try {
await window.codesign.locale.set(v);
const persisted = await window.codesign.locale.set(v);
const applied = await applyLocale(persisted);
setLocale(applied);
} catch (err) {
pushToast({
variant: 'error',
Expand Down Expand Up @@ -829,7 +831,7 @@ function AppearanceTab() {
</div>

<div className="pt-2 border-t border-[var(--color-border-subtle)]">
<Row label="Language" hint="Restart the app after changing the language.">
<Row label="Language" hint="Language changes take effect immediately.">
<NativeSelect
value={locale}
onChange={handleLocaleChange}
Expand Down
24 changes: 23 additions & 1 deletion packages/i18n/src/i18n.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { describe, expect, it, vi } from 'vitest';
import { availableLocales, initI18n, isSupportedLocale, normalizeLocale, setLocale } from './index';
import {
availableLocales,
getCurrentLocale,
initI18n,
isSupportedLocale,
normalizeLocale,
setLocale,
} from './index';

describe('normalizeLocale', () => {
it('returns the value unchanged when it is supported', () => {
Expand Down Expand Up @@ -69,4 +76,19 @@ describe('initI18n + setLocale (live switching)', () => {
expect(warn).toHaveBeenCalled();
warn.mockRestore();
});

it('setLocale updates getCurrentLocale and i18n.t() immediately (no restart needed)', async () => {
const { i18n } = await import('./index');
await initI18n('en');
expect(getCurrentLocale()).toBe('en');
expect(i18n.t('common.send')).toBe('Send');

await setLocale('zh-CN');
expect(getCurrentLocale()).toBe('zh-CN');
expect(i18n.t('common.send')).toBe('发送');

await setLocale('en');
expect(getCurrentLocale()).toBe('en');
expect(i18n.t('common.send')).toBe('Send');
});
});
Loading