Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(i18n): resolve i18next _one/_other plural keys in the custom translate

Akarshan Biswas1ec2f821e5fb
diff --git a/web-app/src/i18n/__tests__/plural.test.ts b/web-app/src/i18n/__tests__/plural.test.ts
new file mode 100644
index 000000000..981ee1557
--- /dev/null
+++ b/web-app/src/i18n/__tests__/plural.test.ts
@@ -0,0 +1,29 @@
+import { describe, it, expect } from 'vitest'
+import i18n from '../setup'
+
+/**
+ * Several resource keys exist only in i18next's `_one`/`_other` plural form
+ * (htmlArtifact.unresolvedAssets, preview.unresolvedRefs, subagentToolUses).
+ * The custom `t` must resolve those when a numeric `count` is passed, or the
+ * raw key renders at the user.
+ */
+describe('i18n plural resolution', () => {
+ it('picks the _one form for count 1', () => {
+ expect(i18n.t('common:subagentToolUses', { count: 1 })).toBe('1 tool use')
+ })
+
+ it('picks the _other form for other counts', () => {
+ expect(i18n.t('common:subagentToolUses', { count: 3 })).toBe('3 tool uses')
+ expect(i18n.t('common:subagentToolUses', { count: 0 })).toBe('0 tool uses')
+ })
+
+ it('still resolves plain keys with a count present', () => {
+ expect(i18n.t('common:close', { count: 2 })).toBe('Close')
+ })
+
+ it('resolves the pre-existing plural-only keys', () => {
+ expect(
+ i18n.t('common:htmlArtifact.unresolvedAssets', { count: 2 })
+ ).toContain('2 asset references')
+ })
+})
diff --git a/web-app/src/i18n/setup.ts b/web-app/src/i18n/setup.ts
index 2d02734c8..4350c62c6 100644
--- a/web-app/src/i18n/setup.ts
+++ b/web-app/src/i18n/setup.ts
@@ -85,12 +85,22 @@ const translate = (key: string, options: Record<string, unknown> = {}): string =
}, obj as unknown) as string | undefined
}
- // Try to get translation from current language
- let translation = getNestedValue(res[language]?.[namespace], translationKey)
-
- // Fallback to fallback language if not found
- if (translation === undefined && language !== fallbackLng) {
- translation = getNestedValue(res[fallbackLng]?.[namespace], translationKey)
+ // i18next resolves plural forms via `_one`/`_other` key suffixes when a
+ // numeric `count` is passed; several resource keys only exist in that form
+ // (e.g. htmlArtifact.unresolvedAssets_one), so try the suffixed key first.
+ const keysToTry =
+ typeof options.count === 'number'
+ ? [`${translationKey}_${options.count === 1 ? 'one' : 'other'}`, translationKey]
+ : [translationKey]
+
+ let translation: string | undefined
+ for (const candidate of keysToTry) {
+ // Try the current language, then the fallback language.
+ translation = getNestedValue(res[language]?.[namespace], candidate)
+ if (translation === undefined && language !== fallbackLng) {
+ translation = getNestedValue(res[fallbackLng]?.[namespace], candidate)
+ }
+ if (translation !== undefined) break
}
// Callers may supply an inline `defaultValue`, as i18next allows. Honour it