This note records one round of internationalization-related adjustments in this blog.
The original problem was not that the site had no i18n support. The real problem was that i18n was initialized too late.
The page first rendered in a default language, and only after the browser started did it read the user’s real language from localStorage or a language detector. That caused obvious language flicker.
In this project, the clearest example was the bottom navigation: the first screen could appear in Chinese even though the user’s actual preference was English.
The common i18n flow usually looks like this:
This causes several issues:
So the key question is not whether the app can switch languages later. The key is whether the language is already correct on the first paint.
The first step was to initialize the language as early as possible at the layout level.
In practice, that means executing a script early in head and writing the user’s language to:
document.documentElement.langThe purpose is to keep the language context consistent before and after hydration, so the browser does not paint the wrong language first.
Only switching language on the client is still not enough.
A more complete solution is to generate two route sets directly:
/cn/intro/en/intro/cn/journey/en/journey/cn/knowledge/en/knowledgeOnce you do this, the English pages are built as English HTML, and the Chinese pages are built as Chinese HTML.
Benefits:
Once a project has explicit locale routes, the root path / should do one lightweight job:
There was also one small detail handled here: if the root route defaults to English, then its loading copy, script defaults, and noscript fallback should all be English as well, instead of mixing Chinese and English.