rsanchez
2017-03-14 38b0782c887f046426c31766901906c614d73140
securis/src/main/webapp/src/app/common/i18n.ts
....@@ -5,13 +5,14 @@
55 // Use as reference: https://github.com/ngx-translate/core/tree/master/src
66 @Injectable()
77 export class LocaleService {
8
- get URL_TPL() {return 'src/lang/messages-{0}.json'};
8
+ get URL_TPL() {return 'src/lang/messages_{0}.json'};
99
10
+ private _devLang : string = 'en';
1011 private _currentLang : string = null;
1112 private _messages : any = null;
1213 private _elements = new Array<ElementRef>();
1314 private constructor(private http: Http, @Inject('INITIAL_LANG') initLang: string) {
14
- this._currentLang = initLang || this.getBrowserLang();
15
+ this.lang = initLang || this.getBrowserLang();
1516 }
1617
1718 get lang() : string {
....@@ -21,7 +22,7 @@
2122 set lang(newLang: string) {
2223 this._currentLang = newLang;
2324 this.http.get(this._format(this.URL_TPL, newLang)).subscribe((data) => {
24
- this._messages = data;
25
+ this._messages = data.json();
2526 this.reloadTexts();
2627 });
2728 }
....@@ -48,7 +49,7 @@
4849 /**
4950 * It works similar to MessageFormat in Java
5051 */
51
- _format(str: string, ...params: Array<string>) {
52
+ _format(str: string, ...params: string[]) {
5253
5354 return str.replace(/\{(\d+)\}/g, function(match, index) {
5455 return params[index];
....@@ -78,7 +79,7 @@
7879 * $L.get('hello'); // This returns "hola"
7980 * $L.get('Hello {0}!!', 'John'); // This returns: "Hola John!!" if language is spanish
8081 */
81
- get(msg: string) : string {
82
+ get(msg: string, ...params: string[] ) : string {
8283 if (msg == null) {
8384 return '';
8485 }
....@@ -90,16 +91,15 @@
9091 trans_msg = this._messages[msg];
9192 } else if (this._messages[msg.toLowerCase()]) {
9293 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);
9596 trans_msg = msg;
9697 }
9798 }
9899 // Enviar evento cuando el idioma cambia al $rootScope
99100
100101 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);
103103 }
104104
105105 }
....@@ -108,6 +108,7 @@
108108 @Directive({ selector: '[i18n]' })
109109 export class I18nDirective {
110110 constructor(private el: ElementRef, private renderer: Renderer, private $L: LocaleService) {
111
+ console.log(el);
111112 }
112113
113114 ngAfterViewChecked() {
....@@ -124,7 +125,6 @@
124125 })
125126 export class LocaleServiceModule {
126127 static withConfig(initLang?: string): ModuleWithProviders {
127
- console.log('Init lang with ' + initLang);
128128 return {
129129 ngModule: LocaleServiceModule,
130130 providers: [