hodcroftlab / covariants

Real-time updates and information about key SARS-CoV-2 variants, plus the scripts that generate this information.
https://covariants.org/
GNU Affero General Public License v3.0
317 stars 111 forks source link

Add localization (internationalization) #364

Closed ivan-aksamentov closed 1 year ago

ivan-aksamentov commented 1 year ago

Resolves: https://github.com/hodcroftlab/covariants/discussions/292 https://github.com/hodcroftlab/covariants/issues/311

This adds internationalization (i18n). Simply speaking the text in the application is now translated to multiple languages, and you can now switch between these languages.

What's new:

Implementation:

Hardcoded strings

Strings hardcoded in the app code (like text of buttons or of nav bar links) are now translated using react-i18next (1, 2). Also dates and numbers are translated using other handy packages. All translatable strings need to be surrounded by t() function, which comes from useTranslationSafe() hook.

The usual string interpolation in these strings does not work (interpolation will happen before the string is extracted for translation)! So it needs to be replaced with string interpolaton of i18next format:

"Hello, ${name}!" -> t("Hello, {{name}}!", { name: "Covariants" })

i.e. interpolated variables should be additionally set using a dict, passed as the second param to the t-function. Try to pick variable names that are different from keys in the dict (i.e. don't use JS shorthand syntax), because any refactor that leads to variable rename will break the interpolation (the variable in the template string will not be renamed by the refactoring tools), and the app will be showing uninterpolated content without any warning.

Maintenance procedure for hardcoded strings:

Markdown content

Markdown content files have been moved from content/ to web/src/content/${locale}. For now there is only en/ directory for English locale. Other locales should be just copies of the en/ directory, but with content in the corresponding language.

The names of directories should be the same as here, or here, or as the letter code in the language selection dropdown in the app.

Markdown content now should only be loaded using <MdxContent filepath="${filepath}"/>, where filepath is the path relative to web/src/content/en (or any other locale than en, they are mirrors).

If localized md file is missing, the English version will be loaded as a fallback.

Maintenance procedure for markdown content:

I haven't figured how to automatically bulk-translate markdown yet. It's MDX actually, because we use React tags there. So this will likely need to be done manually. Just keep md files in web/src/content/${locale} for each locale in sync with English locale in web/src/content/en. "Just". It's a large upfront work, and it also requires expert knowledge every time something changes in English version. Typically you need many experts, per language, because rare human knows more than a handful of these.

Further work:

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
covariants ✅ Ready (Inspect) Visit Preview Jun 13, 2023 10:32am
emmahodcroft commented 1 year ago

List of files that contain large chunks of text that would need to be translated if translation were made possible:

covariants/content/: Faq.md Home.md NameTable.md PerAreaIntro.md PerClusterIntro.md PerCountryCasesIntro.md RegionBreakdownInfo.md SharedMutations.md VariantsPageIntro.md

Every variant page in the clusters folder (currently 48)

Each of the country intros in PerCountryIntro folder

I think this is all the pages...?