I was able to fix this problem. Here is an update on how to solve this issue:
1. Create a file global.d.ts
and add the interface extension definitions there.
export { };
declare global
{
interface String
{
/**
* Translates the given string according to the culture provided.
* @param cultureName The culture name of the translation target.
* @returns The translated string.
*/
translate(cultureName: string): string;
}
}
2. Create another file with the interface extension methods definition. In my case I named it string.extensions.ts
/**
* Translates the given string according to the culture provided.
* @param cultureName The culture name of the translation target.
* @returns The translated string.
*/
String.prototype.translate = function(cultureName: string): string
{
const inputValue = this;
// Do the translation here
const translatedValue = 'Willkommen bei meiner App'; // Only for example
return translatedValue ;
};
3. In your app boot file, in my case its main.ts
, add a reference to the global.d.ts
and the file containing your extension methods
definitions.
/// <reference path="app/core/extensions/global.d.ts" />
//...
import './app/core/extensions/string.extensions';
You just need to import this file once in your project (main.ts
) and then you can use it anywhere in the code.
4. Example use in my AppComponent
import {Component} from '@angular/core';
@Component({
selector: 'my-app',
template: `
Original Value: <h3>{{ originalValue }}</h3>
Translated Value: <h3>{{ translatedValue }}</h3>
`
})
export class AppComponent {
private originalValue:string;
private translatedValue:string;
constructor() {
this.originalValue = 'Welcome to my App';
this.translatedValue = 'Welcome to my App'.translate('de-DE');
}
}
That's all you need to do to solve this annoying problem.
Here is a link to working plunker: Plunker Demo