firstsun-dev_git-files-sync/tests/i18n/index.test.ts
ClaudiaFang 144eb286d8 feat: add i18n (multi-language) support (#38)
Add an i18n layer (t(key, vars?)) with English (default) and
Traditional Chinese translations, detecting the active locale via
Obsidian's window.moment.locale() with a fallback to English for
unsupported locales.

Replace ~130 hardcoded UI strings across the settings tab, ribbon/
command labels, Notice messages, and the sync status view/modals with
translated keys. Left untranslated on purpose: service provider names
(GitHub/GitLab/Gitea), diff-format markers, URL placeholders, and
changelog release-note content.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_011BNWwPd5zudtW2JQr6ZAUm
2026-07-13 14:28:18 +00:00

54 lines
1.8 KiB
TypeScript

import { describe, it, expect, afterEach } from 'vitest';
import { t, getActiveLocale } from '../../src/i18n';
type MomentGlobal = { moment?: { locale: () => string } };
function setMomentLocale(locale: string | undefined): void {
const w = window as unknown as MomentGlobal;
if (locale === undefined) {
delete w.moment;
} else {
w.moment = { locale: () => locale };
}
}
describe('i18n', () => {
afterEach(() => {
setMomentLocale(undefined);
});
it('falls back to English when window.moment is unavailable', () => {
setMomentLocale(undefined);
expect(getActiveLocale()).toBe('en');
expect(t('confirmModal.cancel')).toBe('Cancel');
});
it('falls back to English for an unsupported locale', () => {
setMomentLocale('fr');
expect(getActiveLocale()).toBe('en');
expect(t('confirmModal.cancel')).toBe('Cancel');
});
it('resolves zh-tw translations when moment locale is zh-tw', () => {
setMomentLocale('zh-tw');
expect(getActiveLocale()).toBe('zh-tw');
expect(t('confirmModal.cancel')).toBe('取消');
});
it('maps a bare "zh" locale to zh-tw', () => {
setMomentLocale('zh');
expect(getActiveLocale()).toBe('zh-tw');
});
it('interpolates variables into the template', () => {
setMomentLocale(undefined);
expect(t('settings.testConnection.success', { service: 'GitHub' })).toBe('GitHub connection successful!');
});
it('falls back to English text when a zh-tw key is missing a translation', () => {
setMomentLocale('zh-tw');
// Every key defined in en.ts should resolve to *some* string even if
// zh-tw hasn't translated it yet, rather than throwing or returning undefined.
expect(typeof t('confirmModal.title')).toBe('string');
});
});