How to implement multiple languages in one page
See original GitHub issueI’m submitting a … (check one with “x”)
[ ] bug report => check the FAQ and search github for a similar issue or PR before submitting
[x] support request => check the FAQ and search github for a similar issue before submitting
[ ] feature request
My use case is that I need the web site localized in all of the supported languages, but I also need part of an app (invoice editor) to be localized in different locale, depending what language the invoice is made with. (Think US based worker, that speaks french and is using the app in french, but she is sending an invoice to someone in Spain, and wants to preview the invoice in Spanish.) In this case, there area actually multiple selected locales at the same time in play: the ui should be in french, but the are containing the invoice preview should display spanish.
Any ideas how to tackle this one? Can I have multiple instances of TranslateService? We’re using AOT.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:6
Top Results From Across the Web
9 tips for designing a multi language website - Weglot
9 tips for designing a multi language website · 1. Use global templates for consistent branding · 2. Make the language switcher really...
Read more >Create pages in multiple languages - Knowledge Base
Hover over a page, then click the More dropdown menu and select Create multi-language variation. · In the dialog box, click the Variation ......
Read more >Easily Create a Multi Language Website (Tutorial, Tips + ...
Want to create a multi language website? Read this post for a detailed step-by-step tutorial plus the best practices to make your site...
Read more >How to Build a Multilingual Website to Reach a Wider Audience
The website supports languages such as Japanese, Chinese, Russian and Italian, along with multiple language support in the same country. Once ...
Read more >How to serve a page with content in multiple languages
How to serve a page with content in multiple languages ? ... To change the language, just simply set the lang attribute. We...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I ended up doing it like this:
Notice the /assets/i18n/custom. part, that configures the loader to read from /assets/i18n/custom.en.json for an example.
And in your app module:
After this you can
{{ 'translationkey' | translatecustom }}
in your component, and injectCustomTranslationService
to your components to use different language etc.Note that you need to adapt this solution if you use different loader + the usual ymmv and code is provided as is and may contain bugs etc.
It would be EXTREMELY nice to see proper support for multiple TranslationServices and the option to specify which one is used in pipe / directive etc. Think of named services, where the current is default and used if nothing is specified.
The solution given here may break in future releases.
I also used in my case the approach proposed by @swftvsn, however for the
CustomTranslationService
example is a bit outdated and the constructor is expecting now some more parameters. I ended up with something like:The last parameter is for the default language, which I however don’t know upfront. Later you can then just call
CustomTranslationService.use(lang)
and use the service like you are used to. Of course the loader, compiler etc can be replaced as well with custom ones. Maybe this update saves somebody some time 🤷♂️