| .. | .. |
|---|
| 2 | 2 | import { ModuleWithProviders, NgModule, Inject, Injectable, Directive, ElementRef, HostListener, Input, Renderer } from '@angular/core'; |
|---|
| 3 | 3 | declare var navigator:any; |
|---|
| 4 | 4 | |
|---|
| 5 | +declare global { |
|---|
| 6 | + interface String { |
|---|
| 7 | + capitalize(): string; |
|---|
| 8 | + cap(): string; |
|---|
| 9 | + } |
|---|
| 10 | +} |
|---|
| 11 | + |
|---|
| 12 | +String.prototype.capitalize = function() { |
|---|
| 13 | + return this[0].toUpperCase() + this.slice(1); |
|---|
| 14 | +} |
|---|
| 15 | +String.prototype.cap = String.prototype.capitalize; |
|---|
| 16 | + |
|---|
| 5 | 17 | // Use as reference: https://github.com/ngx-translate/core/tree/master/src |
|---|
| 6 | 18 | @Injectable() |
|---|
| 7 | 19 | export class LocaleService { |
|---|
| .. | .. |
|---|
| 50 | 62 | * It works similar to MessageFormat in Java |
|---|
| 51 | 63 | */ |
|---|
| 52 | 64 | _format(str: string, ...params: string[]) { |
|---|
| 53 | | - |
|---|
| 54 | | - return str.replace(/\{(\d+)\}/g, function(match, index) { |
|---|
| 55 | | - return params[index]; |
|---|
| 65 | + var ocur = 0; |
|---|
| 66 | + return str.replace(/\{(\d*)\}/g, function(match, index) { |
|---|
| 67 | + var idx = index === '' ? ocur : +index; |
|---|
| 68 | + ocur += 1; |
|---|
| 69 | + return params[idx]; |
|---|
| 56 | 70 | }); |
|---|
| 57 | 71 | }; |
|---|
| 58 | 72 | |
|---|
| .. | .. |
|---|
| 98 | 112 | } |
|---|
| 99 | 113 | // Enviar evento cuando el idioma cambia al $rootScope |
|---|
| 100 | 114 | |
|---|
| 101 | | - if (arguments.length === 1) return trans_msg; |
|---|
| 115 | + if (params.length === 0) return trans_msg; |
|---|
| 102 | 116 | return this._format(trans_msg, ...params); |
|---|
| 103 | 117 | } |
|---|
| 118 | + |
|---|
| 104 | 119 | |
|---|
| 105 | 120 | } |
|---|
| 106 | 121 | |
|---|
| .. | .. |
|---|
| 134 | 149 | } |
|---|
| 135 | 150 | |
|---|
| 136 | 151 | |
|---|
| 152 | + |
|---|
| 153 | + |
|---|