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

Lazy load translations #781

Merged
merged 2 commits into from
Jan 23, 2025
Merged
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
2 changes: 1 addition & 1 deletion src/components/LanguageSelection/LanguageSelection.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ const LanguageSelection = ({heading = DEFAULT_HEADING, headingLevel = 2}) => {
// or if an update is being processed.
if (updatingLanguage || languageCode === locale) return;

const confirmationQuestion = formatMessageForLocale(languageCode, changeLanguagePrompt);
const confirmationQuestion = await formatMessageForLocale(languageCode, changeLanguagePrompt);

// only prompt to confirm if there is an active submission
if (submissionState.hasSubmission && !window.confirm(confirmationQuestion)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,11 @@ export const Functional = {
await userEvent.click(frysk_button);
// wait for PUT api call to have completed and loading state to be resolved
await waitFor(() => canvas.findByText(/^fy$/i));
await expect(args.onLanguageChangeDone).toHaveBeenCalledTimes(1); // change once
// change once
await waitFor(() => {
expect(args.onLanguageChangeDone).toHaveBeenCalled();
});
expect(args.onLanguageChangeDone).toHaveBeenCalledTimes(1);
},
};

Expand Down
27 changes: 17 additions & 10 deletions src/i18n.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
import ErrorMessage from 'components/Errors/ErrorMessage';
import Loader from 'components/Loader';

import messagesEN from './i18n/compiled/en.json';
import messagesNL from './i18n/compiled/nl.json';

// ensure flatpickr locales are included in bundle
flatpickr.l10ns.nl = Dutch;

Expand All @@ -24,22 +21,32 @@
currentLanguage.setValue(langCode);
};

const loadLocaleData = locale => {
const loadLocaleData = async locale => {
let localeToLoad;
switch (locale) {
case 'nl':
return messagesNL;
default:
return messagesEN;
case 'nl': {
localeToLoad = 'nl';
break;
}
case 'en':

Check warning on line 31 in src/i18n.jsx

View check run for this annotation

Codecov / codecov/patch

src/i18n.jsx#L31

Added line #L31 was not covered by tests
// in case (accidentally) a locale is set that we don't ship translations for yet,
// fall back to English.
// eslint-disable-next-line no-fallthrough
default: {
localeToLoad = 'en';
}
}
Comment on lines 26 to 38
Copy link
Member Author

Choose a reason for hiding this comment

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

this may look a bit over-engineered, but our backend does support things like nl_NL and nl_BE locales and doing things this way makes it future proof for languages we may add in the future that have regional variants.

const messages = await import(`./i18n/compiled/${localeToLoad}.json`);
return messages.default;
};

/*
Functionality to localize messages in a locale outside of the usual React lifecycle.
*/
const cache = createIntlCache();

const formatMessageForLocale = (locale, msg) => {
const messages = loadLocaleData(locale);
const formatMessageForLocale = async (locale, msg) => {
const messages = await loadLocaleData(locale);
const intl = createIntl({locale, messages}, cache);
return intl.formatMessage(msg);
};
Expand Down
9 changes: 9 additions & 0 deletions vite.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ export default defineConfig(({mode}) => ({
},
},
sentryVitePlugin({
silent: mode === 'development',
release: {
create: false,
inject: false,
},
sourcemaps: {
disable: true,
},
bundleSizeOptimizations: {
excludeDebugStatements: true,
excludeTracing: true,
Expand All @@ -156,6 +164,7 @@ export default defineConfig(({mode}) => ({
excludeReplayIframe: true,
excludeReplayWorker: true,
},
telemetry: false,
}),
// must be last!
codecovVitePlugin({
Expand Down
Loading