synacor / preact-i18n

Simple localization for Preact.
BSD 3-Clause "New" or "Revised" License
206 stars 18 forks source link

Sample of using preact-i8n in combo with express #14

Closed GoldenEagle007 closed 6 years ago

GoldenEagle007 commented 7 years ago

Hi, I am looking for direction of what is the best way to use preact-i18n in preact but the locale is filtered at express end first. So the scenario is User hits the url, express would check the language in the header, based on that it will load the translation file (like shown in bullet 2 of getting started) and feed it to IntlProvider encompassing the preact component. The idea is not to load all the translation file at the client end but only the one user needs.

If there is a sample/tutorial you can point to that would be really helpful. Thanks

billneff79 commented 7 years ago

To solve the use case of not loading all translation files, the typical pattern would be to create chunks in the final build for each language file, using something like webpacks' bundle-loader, and then load the appropriate chunk asynchronously at runtime (using something like split points) depending on which locale the end-user needs to see. All of that can be handled in the client without the need for express to sit in front of it and would ensure that only one locale file is downloaded, not all of them.

billneff79 commented 7 years ago

With that said, this is a common case and we should probably add documentation to the readme to that effect

GoldenEagle007 commented 7 years ago

Thanks @billneff79 you make it sound so easy :-) . So would I have chunks for each language + route combination. If I have 3 languages and 2 routes that would mean 6 chunks. Is that correct ?

I agree we need more documentation on this as it would help in easy internationalization of apps developed using preact. I will try your above suggestion and see how far I go.