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
5 changes: 5 additions & 0 deletions .changeset/cute-gifts-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/clerk-js': patch
---

Fix locale fallback logic to render English values when localization keys are `undefined`.
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,65 @@ describe('Localization parsing and replacing', () => {
const localizedValue = result.current.t(localizationKeys('backButton'));
expect(localizedValue).toBe('test');
});

it('falls back to English when user locale has undefined value for a key', async () => {
const { wrapper: Wrapper } = await createFixtures();
const wrapperBefore = ({ children }) => (
<Wrapper>
<OptionsProvider
value={{
localization: {
backButton: undefined, // Explicitly undefined should fall back to English
formButtonPrimary: 'Translated', // Non-undefined should override
},
}}
>
{children}
</OptionsProvider>
</Wrapper>
);

const { result } = renderHook(() => useLocalizations(), { wrapper: wrapperBefore });

// undefined value should fall back to English
const backButtonValue = result.current.t(localizationKeys('backButton'));
expect(backButtonValue).toBe(defaultResource.backButton);

// Non-undefined value should use the translation
const formButtonValue = result.current.t(localizationKeys('formButtonPrimary'));
expect(formButtonValue).toBe('Translated');
});

it('falls back to English for nested keys with undefined values', async () => {
const { wrapper: Wrapper } = await createFixtures();
const wrapperBefore = ({ children }) => (
<Wrapper>
<OptionsProvider
value={{
localization: {
signIn: {
start: {
title: undefined, // Should fall back to English
subtitle: 'Custom subtitle', // Non-undefined should override
},
},
},
}}
>
{children}
</OptionsProvider>
</Wrapper>
);

const { result } = renderHook(() => useLocalizations(), { wrapper: wrapperBefore });

// undefined nested value should fall back to English (tokens get replaced by t())
const titleValue = result.current.t(localizationKeys('signIn.start.title'));
// The English default is 'Sign in to {{applicationName}}', tokens get replaced
expect(titleValue).toContain('Sign in to');

// Non-undefined nested value should use the translation
const subtitleValue = result.current.t(localizationKeys('signIn.start.subtitle'));
expect(subtitleValue).toBe('Custom subtitle');
});
});
6 changes: 4 additions & 2 deletions packages/clerk-js/src/ui/localization/parseLocalization.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { DeepPartial, LocalizationResource } from '@clerk/shared/types';
import { fastDeepMergeAndReplace } from '@clerk/shared/utils';
import { fastDeepMergeAndKeep, fastDeepMergeAndReplace } from '@clerk/shared/utils';
import { dequal as deepEqual } from 'dequal';

import { useOptions } from '../contexts';
Expand All @@ -15,8 +15,10 @@ const parseLocalizationResource = (
if (!cache || (!!prev && prev !== userDefined && !deepEqual(userDefined, prev))) {
prev = userDefined;
const res = {} as LocalizationResource;
fastDeepMergeAndReplace(base, res);
// Merge user-defined first (may contain undefined values)
fastDeepMergeAndReplace(userDefined, res);
// Fill in missing/undefined values from base (English)
fastDeepMergeAndKeep(base, res);
cache = res;
return cache;
}
Expand Down