Skip to content
Open
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
1 change: 1 addition & 0 deletions packages/javascript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ export {default as resolveFlowTemplateLiterals} from './utils/resolveFlowTemplat
export {default as countryCodeToFlagEmoji} from './utils/countryCodeToFlagEmoji';
export {default as resolveLocaleDisplayName} from './utils/resolveLocaleDisplayName';
export {default as resolveLocaleEmoji} from './utils/resolveLocaleEmoji';
export {default as getBaseLanguage} from './utils/getBaseLanguage';
export {default as buildValidatorFromRules} from './utils/buildValidatorFromRules';
export {default as evaluateValidationRule, DEFAULT_VALIDATION_MESSAGE_KEYS} from './utils/evaluateValidationRule';
export {default as processOpenIDScopes} from './utils/processOpenIDScopes';
Expand Down
41 changes: 41 additions & 0 deletions packages/javascript/src/utils/getBaseLanguage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com).
*
* WSO2 LLC. licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except
* in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
* Resolves a BCP 47 locale tag to its base (primary) language subtag, so that
* region-qualified tags compare equal to their bare form (e.g. "en-US" and "en"
* both resolve to "en"). Uses `Intl.Locale` when available, falling back to a
* simple split on the first "-".
*
* @param tag - BCP 47 locale tag to resolve (e.g. "en-US", "hi-IN", "en")
* @returns The lowercased base language subtag (e.g. "en", "hi")
*
* @example
* ```typescript
* getBaseLanguage('en-US') // 'en'
* getBaseLanguage('hi-IN') // 'hi'
* getBaseLanguage('en') // 'en'
* ```
*/
export default function getBaseLanguage(tag: string): string {
try {
return new Intl.Locale(tag).language.toLowerCase();
} catch {
return tag.split('-')[0].toLowerCase();
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2026 The ThunderID Authors
// SPDX-License-Identifier: Apache-2.0

import {resolveLocaleDisplayName, resolveLocaleEmoji} from '@thunderid/browser';
import {getBaseLanguage, resolveLocaleDisplayName, resolveLocaleEmoji} from '@thunderid/browser';
import {FC, ReactElement, ReactNode, useEffect, useMemo} from 'react';
import BaseLanguageSwitcher, {LanguageOption, LanguageSwitcherRenderProps} from './BaseLanguageSwitcher';
import useFlowMeta from '../../../contexts/FlowMeta/useFlowMeta';
Expand Down Expand Up @@ -89,22 +89,45 @@ const LanguageSwitcher: FC<LanguageSwitcherProps> = ({children, className}: Lang
[effectiveLanguageCodes],
);

// If the detected language isn't supported by the server, fall back to the first available language.
// If the detected language isn't supported by the server, fall back to English (matched by base
// language, e.g. browser "en-US" against server "en"), or the first available language if the
// server doesn't offer English either.
useEffect(() => {
if (availableLanguageCodes.length > 0 && !availableLanguageCodes.includes(currentLanguage)) {
switchLanguage(availableLanguageCodes[0]);
if (availableLanguageCodes.length === 0) {
return;
}
const currentBase: string = getBaseLanguage(currentLanguage);
const isSupported: boolean = availableLanguageCodes.some(
(code: string): boolean => getBaseLanguage(code) === currentBase,
);
if (isSupported) {
return;
}
const englishCode: string | undefined = availableLanguageCodes.find(
(code: string): boolean => getBaseLanguage(code) === 'en',
);
switchLanguage(englishCode ?? availableLanguageCodes[0]);
}, [availableLanguageCodes, currentLanguage, switchLanguage]);

// `currentLanguage` may be region-qualified (e.g. "en-US") while `languages[].code` entries are
// bare (e.g. "en") — pass down whichever option's code shares its base language, so BaseLanguageSwitcher
// can find a match instead of falling back to the raw, unlabeled code.
const displayLanguage: string = useMemo(() => {
const match: LanguageOption | undefined = languages.find(
(option: LanguageOption): boolean => getBaseLanguage(option.code) === getBaseLanguage(currentLanguage),
);
return match?.code ?? currentLanguage;
}, [languages, currentLanguage]);

const handleLanguageChange = (language: string): void => {
if (language !== currentLanguage) {
if (language !== displayLanguage) {
switchLanguage(language);
}
Comment on lines 122 to 125

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor | ⚡ Quick win

Compare the selected locale with currentLanguage.

At Line 123, displayLanguage can differ from currentLanguage. For example, with configured locales en-US and en-GB, a current locale of en-AU displays en-US. When the user selects en-US, this condition prevents switchLanguage, so the concrete locale remains en-AU.

Proposed fix
 const handleLanguageChange = (language: string): void => {
-  if (language !== displayLanguage) {
+  if (language !== currentLanguage) {
     switchLanguage(language);
   }
 };
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const handleLanguageChange = (language: string): void => {
if (language !== currentLanguage) {
if (language !== displayLanguage) {
switchLanguage(language);
}
const handleLanguageChange = (language: string): void => {
if (language !== currentLanguage) {
switchLanguage(language);
}
};
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@packages/react/src/components/presentation/LanguageSwitcher/LanguageSwitcher.tsx`
around lines 122 - 125, Update handleLanguageChange to compare the selected
language against currentLanguage rather than displayLanguage, ensuring
switchLanguage runs when the displayed fallback differs from the actual locale.

};

return (
<BaseLanguageSwitcher
currentLanguage={currentLanguage}
currentLanguage={displayLanguage}
isLoading={isLoading}
languages={languages}
onLanguageChange={handleLanguageChange}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ const ThunderIDProvider: FC<PropsWithChildren<ThunderIDProviderProps>> = ({
...rest
}: PropsWithChildren<ThunderIDProviderProps>): ReactElement => {
const reRenderCheckRef: RefObject<boolean> = useRef(false);

const client: ThunderIDReactClient = useMemo(() => new ThunderIDReactClient(instanceId), [instanceId]);
const storageManagerRef: any = useRef<any>(null);
const {hasAuthParams, hasCalledForThisInstance} = useBrowserUrl();
Expand Down
Loading