We have to wait until Angular 6 for angular-i18n to support translations in code for error messages and such.
For those that are using angular-i18n (instead of ngx-translate for instance) what are you doing in the meantime to handle translations in code? It occurs to me that if there are not many strings then a simple language service with methods to get translations by language code and an id would work, but I am interested in something more elegant and "angular".
I do not know what the promised code translations support will look like but any temporary solution would ideally be easily converted to the angular-i18n way when it goes live.
What are people out there doing to handle this issue? Any ideas?
It has been a long time since my last response and it can be simplified.
if our .html is like
We have in our .ts a simple ViewChildren, a variable "yet" and a variable "translation"
then we can write some like
Update The same idea: a component implementation you have a component
In any other component
Example in stackblitz
This polyfill seems like the best way to go right now:
https://github.com/ngx-translate/i18n-polyfill
It allows you to wrap anything you want to translate in an
i18n()
function (this API is likely to be preserved in a future release of Angular - see my notes at the bottom of this answer).The polyfill is mainly written by Olivier Combe, a member of the Angular team responsible for i18n:
For Angular 5, you'll need version 0.2.0 when you install:
npm install @ngx-translate/i18n-polyfill@0.2.0 --save
For Angular 6, get the latest version - currently 1.0.0:
npm install @ngx-translate/i18n-polyfill@1.0.0 --save
I got the polyfill working for both JIT and AOT compilation, for Angular 5 (it will also work for Angular 6). Here's what you need to do to translate to a single language (this is a good way to get this working - you can then get multiple languages working later, which I explain further down):
app.module.ts
Add the following imports to your root Angular module:
add the following constant, and specify the providers in your root module:
*.ts
In the .ts file where you want to provide a translation, add this:
This demonstrates that you can even include interpolations in the messages that you want to translate.
You can use i18n definitions (i.e. using specifying the translation 'source' id, meaning, description) like this:
You'll still need to extract the messages, and you can use the ngx-extractor tool to do this. This is included when you install the polyfill, and I've added an example below on its usage inside an npm script. See also the readme on the polyfill page.
Multiple languages
To support switching between multiple languages, you'll need a factory provider for your translations. There are details on the readme of the polyfill page. You'll need something like this in your root module (or for AOT compilation, replace the return value for
localeFactory
with a function that detects which AOT compiled language variant of your app is currently running):Message extraction and xliffmerge
All of this is compatible with xliffmerge, which is a great tool for automatically merging any new translations you add, without overwriting existing translations. Xliffmerge can also automatically perform translations using Google translate (you'll need a Google translate API key). For this to work, I do the extraction and merging/translation in the following order, before I do the actual AOT build:
The AOT build for a specific language version of the site looks like this:
Current status of this polyfill:
This is mainly written by Olivier Combe, a member of the Angular team responsible for i18n. At this stage this it's a 'speculative' polyfill for translating variables or strings in the .ts file. It's likely to be replaced by an API built into Angular which will be very similar, so upgrading later should be reasonably manageable. Here's the diclaimer from the Github page:
There's been some discussion around support in forthcoming minor versions of Angular 6 for translations of variables/strings in code.
Here's a quote from Olivier Combe (from March this year), from the following discussion on Github:
https://github.com/angular/angular/issues/11405
I have a "bizarro" work-around We can have two components
app-text.component.ts
and app-translation.component.ts
Then, in a component we can have some like