Skip to content

Commit

Permalink
fix for default language
Browse files Browse the repository at this point in the history
  • Loading branch information
denysoblohin-okta committed Aug 23, 2024
1 parent 94cd3db commit b69c77d
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
12 changes: 10 additions & 2 deletions src/v3/src/components/Widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,18 @@ import {
extractPageTitle,
getLanguageCode,
getLanguageDirection,
getOdyLanguageCode,
getOdysseyTranslationOverrides,
isAndroidOrIOS,
isAuthClientSet,
isConfigRegisterFlow,
isConsentStep,
isOauth2Enabled,
loadDefaultLanguage,
loadLanguage,
SessionStorage,
triggerEmailVerifyCallback,
unloadLanguage,
} from '../../util';
import { getEventContext } from '../../util/getEventContext';
import { stylisPlugins } from '../../util/stylisPlugins';
Expand Down Expand Up @@ -135,8 +138,7 @@ export const Widget: FunctionComponent<WidgetProps> = (widgetProps) => {
const { stateHandle, unsetStateHandle } = useStateHandle(widgetProps);
const [odyTranslationOverrides, setOdyTranslationOverrides] = useState<
TranslationOverrides<string> | undefined>();
// Odyssey language codes use '_' instead of '-' (e.g. zh-CN -> zh_CN)
const odyLanguageCode: string = languageCode.replace('-', '_');
const odyLanguageCode = getOdyLanguageCode(languageCode);

const { theme, tokens } = useMemo(() => {
const { themeOverride, tokensOverride } = createThemeAndTokens(
Expand All @@ -149,10 +151,16 @@ export const Widget: FunctionComponent<WidgetProps> = (widgetProps) => {
};
}, [brandColors, customTheme, languageDirection]);

// init default language
if (!Bundles.isLoaded(languageCode)) {
loadDefaultLanguage();
}

// on unmount, remove the language
useEffect(() => () => {
if (Bundles.isLoaded(languageCode)) {
Bundles.remove();
unloadLanguage(languageCode);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
Expand Down
26 changes: 22 additions & 4 deletions src/v3/src/util/languageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,29 @@ import { OdysseyI18nResourceKeys, odysseyI18nResourceKeysList } from '@okta/odys
// eslint-disable-next-line import/no-extraneous-dependencies
import i18next from 'i18next';

import { LanguageCode } from '../../../types';
import Bundles from '../../../util/Bundles';
import { WidgetProps } from '../types';
import { getLanguageCode, getSupportedLanguages } from './settingsUtils';
import { getDefaultLanguage, getLanguageCode, getSupportedLanguages } from './settingsUtils';

export const loadDefaultLanguage = () => {
const defaultLanguage = getDefaultLanguage();
if (!Bundles.currentLanguage && !i18next.hasResourceBundle(defaultLanguage, 'login')) {
i18next.addResourceBundle(defaultLanguage, 'login', Bundles.login);
i18next.addResourceBundle(defaultLanguage, 'country', Bundles.country);
}
};

export const getOdyLanguageCode = (languageCode: LanguageCode): string => {
// Odyssey language codes use '_' instead of '-' (e.g. zh-CN -> zh_CN)
return languageCode.replace('-', '_');
};

export const loadLanguage = async (widgetProps: WidgetProps): Promise<void> => {
const { i18n = {}, assets: { baseUrl, rewrite } = {} } = widgetProps;
const languageCode = getLanguageCode(widgetProps);
const supportedLanguages = getSupportedLanguages(widgetProps);

// Odyssey language codes use '_' instead of '-' (e.g. zh-CN -> zh_CN)
const odyLanguageCode: string = languageCode.replace('-', '_');

// NOTE: If assets.baseUrl equals "/", SIW will incorrectly try to load language files
// from URL http://labels/json/login_xx.json
// Remove trailing slashes to match Gen2 behavior
Expand All @@ -40,10 +51,17 @@ export const loadLanguage = async (widgetProps: WidgetProps): Promise<void> => {
rewrite: rewrite ?? ((val) => val),
}, supportedLanguages);

const odyLanguageCode: string = getOdyLanguageCode(languageCode);
i18next.addResourceBundle(odyLanguageCode, 'login', Bundles.login);
i18next.addResourceBundle(odyLanguageCode, 'country', Bundles.country);
};

export const unloadLanguage = (languageCode: LanguageCode) => {
const odyLanguageCode: string = getOdyLanguageCode(languageCode);
i18next.removeResourceBundle(odyLanguageCode, 'login');
i18next.removeResourceBundle(odyLanguageCode, 'country');
};

export const getOdysseyTranslationOverrides = (): Partial<OdysseyI18nResourceKeys> => (
odysseyI18nResourceKeysList
.reduce((overrides: Partial<OdysseyI18nResourceKeys>,
Expand Down
2 changes: 1 addition & 1 deletion src/v3/src/util/locUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const loc = (
&& Object.keys(Bundles.login).findIndex((k) => k.startsWith(`${key}_`)) > 0;
const localizedText: string = odysseyTranslate(`${bundleName}:${key}`, {
...paramsObj,
count: hasPluralForms ? count : undefined,
...(hasPluralForms ? { count } : {}),
interpolation: {
prefix: '{',
suffix: '}',
Expand Down
4 changes: 4 additions & 0 deletions src/v3/src/util/settingsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export const getSupportedLanguages = (widgetProps: WidgetProps): string[] => {
);
};

export const getDefaultLanguage = (): LanguageCode => {
return config.defaultLanguage as LanguageCode;
};

export const getLanguageCode = (widgetProps: WidgetProps): LanguageCode => {
const { language } = widgetProps;
const supportedLanguages = getSupportedLanguages(widgetProps);
Expand Down

0 comments on commit b69c77d

Please sign in to comment.