Skip to content
Closed
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: 2 additions & 2 deletions src/renderer/src/components/stats/StatsPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
DropdownMenuTrigger
} from '../ui/dropdown-menu'
import { AgentIcon } from '@/lib/agent-catalog'
import { translate } from '@/i18n/i18n'
import { getIntlLocale, translate } from '@/i18n/i18n'
export { getStatsPaneSearchEntries } from './stats-search'

function formatDuration(ms: number): string {
Expand Down Expand Up @@ -44,7 +44,7 @@ function formatTrackingSince(timestamp: number | null): string {
}
const date = new Date(timestamp)
return translate('auto.components.stats.StatsPane.trackingSince', 'Tracking since {{value0}}', {
value0: date.toLocaleDateString(undefined, {
value0: date.toLocaleDateString(getIntlLocale(), {
month: 'short',
day: 'numeric',
year: 'numeric'
Expand Down
18 changes: 18 additions & 0 deletions src/renderer/src/i18n/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ export async function setRendererUiLanguage(language: UiLanguage): Promise<void>
const registeredPluginLanguages = new Set<string>()
let pluginLanguagePacks: readonly PluginLanguagePackRegistration[] = []

/**
* BCP-47 tag for `Intl` formatting. Plugin catalogs register under a synthetic
* `plugin<hex>` resource language that `Intl` rejects, so fall back to the tag
* the pack declares (for example `ru-RU`) and finally to the default locale.
*/
export function getIntlLocale(): string {
const active = i18n.language
const pack = pluginLanguagePacks.find((entry) => entry.resourceLanguage === active)
const candidate = pack?.locale ?? active
try {
// Why: an empty result means Intl has no data for the tag, so fall through
// to the default locale instead of letting Intl pick the runtime one.
return Intl.DateTimeFormat.supportedLocalesOf(candidate)[0] ?? DEFAULT_LOCALE
} catch {
return DEFAULT_LOCALE
}
}

export function resolveRendererResourceLanguage(language: string): string {
return pluginLanguagePacks.find((pack) => pack.id === language)?.resourceLanguage ?? language
}
Expand Down
79 changes: 79 additions & 0 deletions src/renderer/src/i18n/intl-locale.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* `getIntlLocale()` exists because plugin catalogs register under a synthetic
* `plugin<hex>` resource language. Passing that straight to `Intl` throws, and
* passing `undefined` silently falls back to the OS locale rather than the
* language the user selected.
*
* The branch assertions stub `supportedLocalesOf` so they describe the helper's
* logic rather than whichever locales the runtime's ICU build happens to carry.
*/
import { afterEach, describe, expect, it, vi } from 'vitest'
import { getIntlLocale, i18n, setRendererPluginLanguagePacks } from './i18n'
import { pluginLanguageResourceId } from '../../../shared/plugins/plugin-language-pack-artifact'
import { DEFAULT_LOCALE } from './supported-languages'

const PACK_ID = 'plugin:smwbev.russian/ru-RU' as const
const PACK_RESOURCE = pluginLanguageResourceId(PACK_ID)
const PACK = {
id: PACK_ID,
resourceLanguage: PACK_RESOURCE,
pluginKey: 'smwbev.russian',
locale: 'ru-RU',
catalog: {}
}

type Packs = Parameters<typeof setRendererPluginLanguagePacks>[0]

async function activate(language: string, packs: Packs = []): Promise<void> {
setRendererPluginLanguagePacks(packs)
await i18n.changeLanguage(language)
}

/** Stubs ICU lookup so a locale counts as supported only when listed. */
function withSupportedLocales(supported: readonly string[]): void {
vi.spyOn(Intl.DateTimeFormat, 'supportedLocalesOf').mockImplementation((requested) => {
const tags = Array.isArray(requested) ? requested : [requested as string]
return tags.filter((tag) => supported.includes(tag)) as string[]
})
}

afterEach(async () => {
vi.restoreAllMocks()
setRendererPluginLanguagePacks([])
await i18n.changeLanguage(DEFAULT_LOCALE)
})

describe('getIntlLocale', () => {
it('passes a supported built-in locale straight through', async () => {
withSupportedLocales(['es'])
await activate('es')
expect(getIntlLocale()).toBe('es')
})

it('resolves a plugin resource language to the locale the pack declares', async () => {
withSupportedLocales(['ru-RU'])
await activate(PACK_RESOURCE, [PACK])
// Without the pack lookup this would reach Intl as `plugin<hex>`.
expect(getIntlLocale()).toBe('ru-RU')
})

it('falls back to the default locale when ICU has no data for the tag', async () => {
withSupportedLocales([])
await activate('es')
// Returning the tag here would let Intl silently format with the runtime locale.
expect(getIntlLocale()).toBe(DEFAULT_LOCALE)
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.

it('falls back to the default locale when Intl rejects the tag', async () => {
vi.spyOn(Intl.DateTimeFormat, 'supportedLocalesOf').mockImplementation(() => {
throw new RangeError('invalid language tag')
})
await activate(PACK_RESOURCE)
expect(getIntlLocale()).toBe(DEFAULT_LOCALE)
})

it('keeps the synthetic resource language unusable for Intl directly', () => {
// Unstubbed on purpose: this is a property of the tag, not of ICU data.
expect(() => Intl.DateTimeFormat.supportedLocalesOf(PACK_RESOURCE)).toThrow(RangeError)
})
})