Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix overwrite context keys from example i18n setup #467

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion login-workflow/example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const App = (): JSX.Element => {
const [showChangePasswordDialog, setShowChangePasswordDialog] = useState(false);

const [isLoading, setIsLoading] = useState(true);

i18nAppInstance.addResourceBundle('en', 'bluiCommon', { ACTIONS: { CREATE_ACCOUNT: 'Register now!' } }, true, true);
// handle initialization of auth data on first load
useEffect(() => {
const initialize = async (): Promise<void> => {
Expand Down
18 changes: 18 additions & 0 deletions login-workflow/example/src/translations/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,24 @@ void i18next
app: {
...AppDictionaries.english.translation,
},
bluiRegistration: {
SELF_REGISTRATION: {
INSTRUCTIONS: `Test To register for an Eaton account, enter the required information below. You will need to verify your email address to continue.`,
VERIFY_EMAIL: {
MESSAGE:
'Test A verification code has been sent to the email address you provided. Click the link or enter the code below to continue. This code is valid for 30 minutes.',
RESEND: 'Test Resend Verification Email',
VERIFICATION_CODE_PROMPT: "Test Didn't receive an email?",
VERIFICATION: 'Test Verification Code',
CODE_VALIDATOR_ERROR: 'Test You must provide a valid code',
},
},
},
bluiAuth: {
HEADER: {
FORGOT_PASSWORD: 'Test Forgot Password',
},
},
},
fr: {
app: {
Expand Down
113 changes: 55 additions & 58 deletions login-workflow/src/contexts/AuthContext/i18nAuthInstance.ts
Original file line number Diff line number Diff line change
@@ -1,71 +1,68 @@
import i18next from 'i18next';
import { AuthDictionaries } from './AuthDictionaries';
import { SharedDictionaries } from '../SharedDictionaries';
import { initReactI18next } from 'react-i18next';

void i18next
.use(initReactI18next) // passes i18n down to react-i18next
.init(
{
fallbackLng: 'en',
ns: ['bluiAuth', 'bluiCommon'],
defaultNS: 'bluiAuth',
load: 'languageOnly',
detection: {
order: ['querystring', 'localStorage', 'navigator'],
caches: ['localStorage'],
export const i18nAuthInstance = i18next.createInstance(
{
fallbackLng: 'en',
ns: ['bluiAuth', 'bluiCommon'],
defaultNS: 'bluiAuth',
load: 'languageOnly',
detection: {
order: ['querystring', 'localStorage', 'navigator'],
caches: ['localStorage'],
},
react: { useSuspense: false },
interpolation: { escapeValue: false },
resources: {
en: {
bluiAuth: {
...AuthDictionaries.english.translation,
},
bluiCommon: {
...SharedDictionaries.english.translation,
},
},
react: { useSuspense: false },
interpolation: { escapeValue: false },
resources: {
en: {
bluiAuth: {
...AuthDictionaries.english.translation,
},
bluiCommon: {
...SharedDictionaries.english.translation,
},
fr: {
bluiAuth: {
...AuthDictionaries.french.translation,
},
fr: {
bluiAuth: {
...AuthDictionaries.french.translation,
},
bluiCommon: {
...SharedDictionaries.french.translation,
},
bluiCommon: {
...SharedDictionaries.french.translation,
},
},
es: {
bluiAuth: {
...AuthDictionaries.spanish.translation,
},
es: {
bluiAuth: {
...AuthDictionaries.spanish.translation,
},
bluiCommon: {
...SharedDictionaries.spanish.translation,
},
bluiCommon: {
...SharedDictionaries.spanish.translation,
},
zh: {
bluiAuth: {
...AuthDictionaries.chinese.translation,
},
bluiCommon: {
...SharedDictionaries.chinese.translation,
},
},
zh: {
bluiAuth: {
...AuthDictionaries.chinese.translation,
},
bluiCommon: {
...SharedDictionaries.chinese.translation,
},
},
pt: {
bluiAuth: {
...AuthDictionaries.portuguese.translation,
},
pt: {
bluiAuth: {
...AuthDictionaries.portuguese.translation,
},
bluiCommon: {
...SharedDictionaries.portuguese.translation,
},
bluiCommon: {
...SharedDictionaries.portuguese.translation,
},
},
},
// We must provide a function as second parameter, otherwise i18next errors
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(err, _t) => {
// eslint-disable-next-line no-console
if (err) return console.log(err);
}
);
},
// We must provide a function as second parameter, otherwise i18next errors
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(err, _t) => {
// eslint-disable-next-line no-console
if (err) return console.log(err);
}
);

export default i18next;
export default { i18nAuthInstance };
24 changes: 12 additions & 12 deletions login-workflow/src/contexts/AuthContext/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React, { useEffect } from 'react';
import { AuthContextProviderProps } from './types';
import { AuthContext } from './context';
import { I18nextProvider } from 'react-i18next';
import i18nAuthInstance from './i18nAuthInstance';
import { i18nAuthInstance } from './i18nAuthInstance';
import { ErrorContext } from '../ErrorContext';
import { AuthDictionaries } from './AuthDictionaries';
import { SharedDictionaries } from '../SharedDictionaries';
Expand All @@ -22,24 +22,24 @@ export const AuthContextProvider: React.FC<
const { language, i18n = i18nInstance, errorConfig } = props;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is a little bizarre...not sure if it's causing your issue, but you're doing what looks like a double destructuring of the i18n prop. You set i18nInstance to the passed prop or the default auth instance...and then you destructure the i18n prop again on line 22. I don't think causes any errors, it's just very strange.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

App translations were breaking because app routes were part of auth routes( earlier change password was part of auth context this might be the reason for moving app routes inside auth). Fixed it now.


if (props.i18n) {
i18n.addResourceBundle('zh', 'bluiAuth', AuthDictionaries.chinese.translation, true, true);
i18n.addResourceBundle('zh', 'bluiCommon', SharedDictionaries.chinese.translation, true, true);
i18n.addResourceBundle('en', 'bluiAuth', AuthDictionaries.english.translation, true, true);
i18n.addResourceBundle('en', 'bluiCommon', SharedDictionaries.english.translation, true, true);
i18n.addResourceBundle('fr', 'bluiAuth', AuthDictionaries.french.translation, true, true);
i18n.addResourceBundle('fr', 'bluiCommon', SharedDictionaries.french.translation, true, true);
i18n.addResourceBundle('pt', 'bluiAuth', AuthDictionaries.portuguese.translation, true, true);
i18n.addResourceBundle('pt', 'bluiCommon', SharedDictionaries.portuguese.translation, true, true);
i18n.addResourceBundle('es', 'bluiAuth', AuthDictionaries.spanish.translation, true, true);
i18n.addResourceBundle('es', 'bluiCommon', SharedDictionaries.spanish.translation, true, true);
i18n.addResourceBundle('zh', 'bluiAuth', AuthDictionaries.chinese.translation, true, false);
i18n.addResourceBundle('zh', 'bluiCommon', SharedDictionaries.chinese.translation, true, false);
i18n.addResourceBundle('en', 'bluiAuth', AuthDictionaries.english.translation, true, false);
i18n.addResourceBundle('en', 'bluiCommon', SharedDictionaries.english.translation, true, false);
i18n.addResourceBundle('fr', 'bluiAuth', AuthDictionaries.french.translation, true, false);
i18n.addResourceBundle('fr', 'bluiCommon', SharedDictionaries.french.translation, true, false);
i18n.addResourceBundle('pt', 'bluiAuth', AuthDictionaries.portuguese.translation, true, false);
i18n.addResourceBundle('pt', 'bluiCommon', SharedDictionaries.portuguese.translation, true, false);
i18n.addResourceBundle('es', 'bluiAuth', AuthDictionaries.spanish.translation, true, false);
i18n.addResourceBundle('es', 'bluiCommon', SharedDictionaries.spanish.translation, true, false);
}

useEffect(() => {
void i18n.changeLanguage(language);
}, [i18n, language]);

return (
<I18nextProvider i18n={i18nInstance}>
<I18nextProvider i18n={i18n}>
<AuthContext.Provider value={{ ...authContextProps }}>
<ErrorContext.Provider value={errorConfig}>{children}</ErrorContext.Provider>
</AuthContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,71 +1,68 @@
import i18next from 'i18next';
import { RegistrationDictionaries } from './RegistrationDictionaries';
import { SharedDictionaries } from '../SharedDictionaries';
import { initReactI18next } from 'react-i18next';

void i18next
.use(initReactI18next) // passes i18n down to react-i18next
.init(
{
fallbackLng: 'en',
ns: ['bluiRegistration', 'bluiCommon'],
defaultNS: 'bluiRegistration',
load: 'languageOnly',
detection: {
order: ['querystring', 'localStorage', 'navigator'],
caches: ['localStorage'],
export const i18nRegistrationInstance = i18next.createInstance(
{
fallbackLng: 'en',
ns: ['bluiRegistration', 'bluiCommon'],
defaultNS: 'bluiRegistration',
load: 'languageOnly',
detection: {
order: ['querystring', 'localStorage', 'navigator'],
caches: ['localStorage'],
},
react: { useSuspense: false },
interpolation: { escapeValue: false },
resources: {
en: {
bluiRegistration: {
...RegistrationDictionaries.english.translation,
},
bluiCommon: {
...SharedDictionaries.english.translation,
},
},
react: { useSuspense: false },
interpolation: { escapeValue: false },
resources: {
en: {
bluiRegistration: {
...RegistrationDictionaries.english.translation,
},
bluiCommon: {
...SharedDictionaries.english.translation,
},
fr: {
bluiRegistration: {
...RegistrationDictionaries.french.translation,
},
fr: {
bluiRegistration: {
...RegistrationDictionaries.french.translation,
},
bluiCommon: {
...SharedDictionaries.french.translation,
},
bluiCommon: {
...SharedDictionaries.french.translation,
},
},
es: {
bluiRegistration: {
...RegistrationDictionaries.spanish.translation,
},
es: {
bluiRegistration: {
...RegistrationDictionaries.spanish.translation,
},
bluiCommon: {
...SharedDictionaries.spanish.translation,
},
bluiCommon: {
...SharedDictionaries.spanish.translation,
},
zh: {
bluiRegistration: {
...RegistrationDictionaries.chinese.translation,
},
bluiCommon: {
...SharedDictionaries.chinese.translation,
},
},
zh: {
bluiRegistration: {
...RegistrationDictionaries.chinese.translation,
},
bluiCommon: {
...SharedDictionaries.chinese.translation,
},
},
pt: {
bluiRegistration: {
...RegistrationDictionaries.portuguese.translation,
},
pt: {
bluiRegistration: {
...RegistrationDictionaries.portuguese.translation,
},
bluiCommon: {
...SharedDictionaries.portuguese.translation,
},
bluiCommon: {
...SharedDictionaries.portuguese.translation,
},
},
},
// We must provide a function as second parameter, otherwise i18next errors
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(err, _t) => {
// eslint-disable-next-line no-console
if (err) return console.log(err);
}
);
},
// We must provide a function as second parameter, otherwise i18next errors
// eslint-disable-next-line @typescript-eslint/no-unused-vars
(err, _t) => {
// eslint-disable-next-line no-console
if (err) return console.log(err);
}
);

export default i18next;
export default { i18nRegistrationInstance };
24 changes: 12 additions & 12 deletions login-workflow/src/contexts/RegistrationContext/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import React, { useEffect } from 'react';
import { RegistrationContextProviderProps } from './types';
import { RegistrationContext } from './context';
import { I18nextProvider } from 'react-i18next';
import i18nRegistrationInstance from './i18nRegistrationInstance';
import { i18nRegistrationInstance } from './i18nRegistrationInstance';
import { ErrorContext } from '../ErrorContext';
import { SharedDictionaries } from '../SharedDictionaries';
import { RegistrationDictionaries } from './RegistrationDictionaries';
Expand All @@ -20,24 +20,24 @@ export const RegistrationContextProvider: React.FC<React.PropsWithChildren<Regis
const { language, i18n = i18nInstance, errorConfig } = props;

if (props.i18n) {
i18n.addResourceBundle('zh', 'bluiRegistration', RegistrationDictionaries.chinese.translation, true, true);
i18n.addResourceBundle('zh', 'bluiCommon', SharedDictionaries.chinese.translation, true, true);
i18n.addResourceBundle('en', 'bluiRegistration', RegistrationDictionaries.english.translation, true, true);
i18n.addResourceBundle('en', 'bluiCommon', SharedDictionaries.english.translation, true, true);
i18n.addResourceBundle('fr', 'bluiRegistration', RegistrationDictionaries.french.translation, true, true);
i18n.addResourceBundle('fr', 'bluiCommon', SharedDictionaries.french.translation, true, true);
i18n.addResourceBundle('pt', 'bluiRegistration', RegistrationDictionaries.portuguese.translation, true, true);
i18n.addResourceBundle('pt', 'bluiCommon', SharedDictionaries.portuguese.translation, true, true);
i18n.addResourceBundle('es', 'bluiRegistration', RegistrationDictionaries.spanish.translation, true, true);
i18n.addResourceBundle('es', 'bluiCommon', SharedDictionaries.spanish.translation, true, true);
i18n.addResourceBundle('zh', 'bluiRegistration', RegistrationDictionaries.chinese.translation, true, false);
i18n.addResourceBundle('zh', 'bluiCommon', SharedDictionaries.chinese.translation, true, false);
i18n.addResourceBundle('en', 'bluiRegistration', RegistrationDictionaries.english.translation, true, false);
i18n.addResourceBundle('en', 'bluiCommon', SharedDictionaries.english.translation, true, false);
i18n.addResourceBundle('fr', 'bluiRegistration', RegistrationDictionaries.french.translation, true, false);
i18n.addResourceBundle('fr', 'bluiCommon', SharedDictionaries.french.translation, true, false);
i18n.addResourceBundle('pt', 'bluiRegistration', RegistrationDictionaries.portuguese.translation, true, false);
i18n.addResourceBundle('pt', 'bluiCommon', SharedDictionaries.portuguese.translation, true, false);
i18n.addResourceBundle('es', 'bluiRegistration', RegistrationDictionaries.spanish.translation, true, false);
i18n.addResourceBundle('es', 'bluiCommon', SharedDictionaries.spanish.translation, true, false);
}

useEffect(() => {
void i18n.changeLanguage(language);
}, [i18n, language]);

return (
<I18nextProvider i18n={i18nInstance}>
<I18nextProvider i18n={i18n}>
<RegistrationContext.Provider value={registrationContextProps}>
<ErrorContext.Provider value={errorConfig}>{children}</ErrorContext.Provider>
</RegistrationContext.Provider>
Expand Down
4 changes: 2 additions & 2 deletions login-workflow/src/testUtils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import {
RegistrationContextProviderProps,
RegistrationWorkflowContextProps,
} from '../contexts';
import i18nAuthInstance from '../contexts/AuthContext/i18nAuthInstance';
import i18nRegistrationInstance from '../contexts/RegistrationContext/i18nRegistrationInstance';
import { i18nAuthInstance } from '../contexts/AuthContext/i18nAuthInstance';
import { i18nRegistrationInstance } from '../contexts/RegistrationContext/i18nRegistrationInstance';

export const authContextProviderProps: AuthContextProviderProps = {
language: 'en',
Expand Down