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: make i18n consistently functional #98

Merged
merged 2 commits into from
Oct 3, 2019
Merged

Conversation

amcgee
Copy link
Member

@amcgee amcgee commented Oct 3, 2019

Fixes #95

There were a few bugs in the way we did i18n generation and handled the singleton runtime. This solves those issues for now, though there are still improvements to be made.

I removed the i18n.changeLanguage('en') and moment.locale('en') calls from the i18n template (yay everything living in one repo!), since it's superfluous and also wrong. This call was injected multiple places in the running code (one for each library or module that used the i18n setup), causing non-deterministic language selection.

I also created a singular place to set the language and default namespace - the AuthBoundary in app-shell-adapter - with a new useLocale hook. This has the added benefit of defaulting to the browser-defined locale, rather than always to english, and then overrides that with the DHIS2 user's locale once a session is established.

We also only load the moment locale for the current locale, rather than loading all of them (possibly multiple times) from the generated locales/index.js files. This fragments the bundle a bit (each locale gets its own chunk generated by webpack for the dynamic import) but should reduce the main bundle size if we have loads of different languages (the dream).

The tradeoff I had to make here was this - previously, since every component had its own instance of d2-i18n and therefore i18next (yeah, not exactly size-efficient) we would call setDefaultNamespace on that instance and rely on that to look up the correct string for each call to i18n.t. When we switched to using a single instance of i18n this broke down. As a stopgap measure I've set the namespace to 'default', since we don't really use namespaces anyway (all strings from all languages for the whole app are included in the js bundle...). The keys are merged, so if there are different translations for a particular english string one will clobber the other, but this is probably OK for now.

The other possible downside of this is that if we use cli-app-scripts to build external libraries (currently we only do this for app-shell-adapter which is internal) and change the behavior in the future (re-adding namespaces) we'll still have some strings injected to the default namespace until we upgrade and re-build that external library.

In this example my browser locale is Spanish, the Admin user is set to French, and the System user is set to English.

Screen Recording 2019-10-03 at 11 29 40 AM


I went halfway down the road of building a babel plugin to inject the namespace into the string key of i18n.t calls, but I stopped because it might not be the right approach - we may instead want to stub out the import '@dhis2/d2-i18n' module and replace it with something we control, something that has a fixedT with a set namespace. Here's my half-complete plugin, worth further investigation...


Side note: the headerbar, which has very few strings and is present in every application, is only in English...

@netlify
Copy link

netlify bot commented Oct 3, 2019

Deploy preview for dhis2-app-platform ready!

Built with commit dd8d62e

https://deploy-preview-98--dhis2-app-platform.netlify.com

@amcgee amcgee requested a review from varl October 3, 2019 10:05
@amcgee amcgee unassigned varl Oct 3, 2019
@amcgee amcgee added the bug Something isn't working label Oct 3, 2019
Copy link
Contributor

@varl varl left a comment

Choose a reason for hiding this comment

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

LGTM, very nice to get rid of the moment and i18n setDefaultNamespace/changeLanguage from the template.

@amcgee amcgee merged commit 291980a into master Oct 3, 2019
@amcgee amcgee deleted the fix/functional-i18n branch October 3, 2019 12:35
dhis2-bot added a commit that referenced this pull request Oct 3, 2019
## [1.5.3](v1.5.2...v1.5.3) (2019-10-03)

### Bug Fixes

* make i18n consistently functional ([#98](#98)) ([291980a](291980a))
@dhis2-bot
Copy link
Contributor

🎉 This PR is included in version 1.5.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Development

Successfully merging this pull request may close these issues.

i18n runtime is broken
3 participants