When it comes to Angular 2 i18n, there are two main platforms that come to mind: The official method, and ng2-translate. Personally, I lean towards following the recommendations in the official documentation. The process of translating HTML strings seems straightforward to me. However, one aspect still puzzles me: how do you translate TypeScript strings?
Consider this component utilizing the built-in i18n solution:
import { Component } from '@angular/core';
@Component({
moduleId: module.id,
selector: 'hello',
template: `
<h1 i18n>Hello {{name}}!</h1>
<button (click)="notify()"></button>
`
})
export class HelloComponent {
private name = 'John Doe';
notify() {
/* How can I translate this? */
alert(`Hello ${this.name}!`);
}
}
The string within the HTML element will be translated. But what about the text displayed in the alert message? If there isn't a direct way to handle this, what would be the recommended approach to achieve translation for such scenarios?