| .. | .. |
|---|
| 5 | 5 | // Use as reference: https://github.com/ngx-translate/core/tree/master/src |
|---|
| 6 | 6 | @Injectable() |
|---|
| 7 | 7 | export class LocaleService { |
|---|
| 8 | | - get URL_TPL() {return 'src/lang/messages-{0}.json'}; |
|---|
| 8 | + get URL_TPL() {return 'src/lang/messages_{0}.json'}; |
|---|
| 9 | 9 | |
|---|
| 10 | + private _devLang : string = 'en'; |
|---|
| 10 | 11 | private _currentLang : string = null; |
|---|
| 11 | 12 | private _messages : any = null; |
|---|
| 12 | 13 | private _elements = new Array<ElementRef>(); |
|---|
| 13 | 14 | private constructor(private http: Http, @Inject('INITIAL_LANG') initLang: string) { |
|---|
| 14 | | - this._currentLang = initLang || this.getBrowserLang(); |
|---|
| 15 | + this.lang = initLang || this.getBrowserLang(); |
|---|
| 15 | 16 | } |
|---|
| 16 | 17 | |
|---|
| 17 | 18 | get lang() : string { |
|---|
| .. | .. |
|---|
| 21 | 22 | set lang(newLang: string) { |
|---|
| 22 | 23 | this._currentLang = newLang; |
|---|
| 23 | 24 | this.http.get(this._format(this.URL_TPL, newLang)).subscribe((data) => { |
|---|
| 24 | | - this._messages = data; |
|---|
| 25 | + this._messages = data.json(); |
|---|
| 25 | 26 | this.reloadTexts(); |
|---|
| 26 | 27 | }); |
|---|
| 27 | 28 | } |
|---|
| .. | .. |
|---|
| 48 | 49 | /** |
|---|
| 49 | 50 | * It works similar to MessageFormat in Java |
|---|
| 50 | 51 | */ |
|---|
| 51 | | - _format(str: string, ...params: Array<string>) { |
|---|
| 52 | + _format(str: string, ...params: string[]) { |
|---|
| 52 | 53 | |
|---|
| 53 | 54 | return str.replace(/\{(\d+)\}/g, function(match, index) { |
|---|
| 54 | 55 | return params[index]; |
|---|
| .. | .. |
|---|
| 78 | 79 | * $L.get('hello'); // This returns "hola" |
|---|
| 79 | 80 | * $L.get('Hello {0}!!', 'John'); // This returns: "Hola John!!" if language is spanish |
|---|
| 80 | 81 | */ |
|---|
| 81 | | - get(msg: string) : string { |
|---|
| 82 | + get(msg: string, ...params: string[] ) : string { |
|---|
| 82 | 83 | if (msg == null) { |
|---|
| 83 | 84 | return ''; |
|---|
| 84 | 85 | } |
|---|
| .. | .. |
|---|
| 90 | 91 | trans_msg = this._messages[msg]; |
|---|
| 91 | 92 | } else if (this._messages[msg.toLowerCase()]) { |
|---|
| 92 | 93 | trans_msg = this._messages[msg.toLowerCase()]; |
|---|
| 93 | | - } else { |
|---|
| 94 | | - this._currentLang !== 'es' && console.error("Missing i18 key: " + msg); |
|---|
| 94 | + } else { |
|---|
| 95 | + (this._currentLang !== this._devLang) && console.error("Missing i18 key: " + msg); |
|---|
| 95 | 96 | trans_msg = msg; |
|---|
| 96 | 97 | } |
|---|
| 97 | 98 | } |
|---|
| 98 | 99 | // Enviar evento cuando el idioma cambia al $rootScope |
|---|
| 99 | 100 | |
|---|
| 100 | 101 | if (arguments.length === 1) return trans_msg; |
|---|
| 101 | | - var params = Array.prototype.slice.call(arguments, 1); |
|---|
| 102 | | - return this._format.apply(trans_msg, params); |
|---|
| 102 | + return this._format(trans_msg, ...params); |
|---|
| 103 | 103 | } |
|---|
| 104 | 104 | |
|---|
| 105 | 105 | } |
|---|
| .. | .. |
|---|
| 108 | 108 | @Directive({ selector: '[i18n]' }) |
|---|
| 109 | 109 | export class I18nDirective { |
|---|
| 110 | 110 | constructor(private el: ElementRef, private renderer: Renderer, private $L: LocaleService) { |
|---|
| 111 | + console.log(el); |
|---|
| 111 | 112 | } |
|---|
| 112 | 113 | |
|---|
| 113 | 114 | ngAfterViewChecked() { |
|---|
| .. | .. |
|---|
| 124 | 125 | }) |
|---|
| 125 | 126 | export class LocaleServiceModule { |
|---|
| 126 | 127 | static withConfig(initLang?: string): ModuleWithProviders { |
|---|
| 127 | | - console.log('Init lang with ' + initLang); |
|---|
| 128 | 128 | return { |
|---|
| 129 | 129 | ngModule: LocaleServiceModule, |
|---|
| 130 | 130 | providers: [ |
|---|