Skip to content

Commit

Permalink
New feature: added the location of arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed May 6, 2016
1 parent 5735be9 commit 79caa2a
Show file tree
Hide file tree
Showing 8 changed files with 491 additions and 60 deletions.
37 changes: 37 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,43 @@ For example, to get the local currency, add in the template:
{{ b | localecurrency:true:'1.2-2' }}
```
and include `LocaleCurrencyPipe` in the component.
#### List
Now you can localize an array of objects or an array of arrays:
```
*ngFor="let item of items | translatearray: {'keyname': {'pipe': '', 'format': '', 'symbolDisplay': bool, 'digitInfo': ''}
```
where the parameters of `translatearray` are in Json format.

For example, to get the localized array, add in the template:
```Html
<!--Array of Data objects-->
<md-card *ngFor="let item of DATA | translatearray: {'name': '',
'position': {
'pipe': 'translate'
},
'salary': {
'pipe': 'localecurrency',
'symbolDisplay': true,
'digitInfo': '1.0-0'
},
'startDate': {
'pipe': 'localedate',
'format': 'mediumDate'
}
}">
<md-card-title>{{ item.name }}</md-card-title>
<md-card-content>
<md-list>
<md-list-item>
<h3 md-line>{{ item.position }}</h3>
<p md-line>{{ item.salary }}</p>
<p md-line>{{ item.startDate }}</p>
</md-list-item>
</md-list>
</md-card-content>
</md-card>
```
and include `TranslateArrayPipe` in the component.

## First scenario
> You need to localize dates and numbers, but no messages.
Expand Down
4 changes: 3 additions & 1 deletion angular2localization.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Exports services & pipes.
export * from './src/services/localization.service';
export * from './src/services/locale.service';
export * from './src/services/locale-number';
export * from './src/pipes/translate.pipe';
export * from './src/pipes/locale-date.pipe';
export * from './src/pipes/locale-number.pipe';
export * from './src/pipes/locale-number.pipe';
export * from './src/pipes/translate-array.pipe';
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular2localization",
"version": "0.5.3",
"version": "0.6.0",
"description": "An Angular 2 library to translate messages, dates and numbers",
"main": "",
"scripts": {},
Expand Down
4 changes: 3 additions & 1 deletion src/pipes/locale-date.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {LocaleService} from '../services/locale.service';
* @see Angular 2 DatePipe for further information
*/
@Injectable() export class LocaleDatePipe implements PipeTransform {

static ALIASES: { [key: string]: String } = {
'medium': 'yMMMdjms',
'short': 'yMdjm',
Expand Down Expand Up @@ -128,5 +129,6 @@ import {LocaleService} from '../services/locale.service';

}

supports(obj: any): boolean { return isDate(obj) || isNumber(obj); }
private supports(obj: any): boolean { return isDate(obj) || isNumber(obj); }

}
64 changes: 9 additions & 55 deletions src/pipes/locale-number.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,64 +7,12 @@
*/

import {Injectable, Pipe, PipeTransform} from '@angular/core';
import {isNumber, isPresent, isBlank, NumberWrapper, RegExpWrapper} from '@angular/common/src/facade/lang';
import {BaseException} from '@angular/common/src/facade/exceptions';
import {NumberFormatter, NumberFormatStyle} from '@angular/common/src/facade/intl';
import {InvalidPipeArgumentException} from '@angular/common/src/pipes/invalid_pipe_argument_exception';
import {NumberFormatStyle} from '@angular/common/src/facade/intl';

// Services.
import {LocaleNumber} from '../services/locale-number';
import {LocaleService} from '../services/locale.service';

/**
* LocaleNumber superclass.
*/
@Injectable() export class LocaleNumber {

constructor() { }

static format(defaultLocale: string, value: number, style: NumberFormatStyle, digits: string, currency: string = null, currencyAsSymbol: boolean = false): string {

if (isBlank(value)) return null;

if (!isNumber(value)) {

throw new InvalidPipeArgumentException(LocaleNumber, value);

}

var minInt = 1, minFraction = 0, maxFraction = 3;
var re = RegExpWrapper.create('^(\\d+)?\\.((\\d+)(\\-(\\d+))?)?$');

if (isPresent(digits)) {

var parts = RegExpWrapper.firstMatch(re, digits);

if (isBlank(parts)) {
throw new BaseException(`${digits} is not a valid digit info for number pipes`);
}
if (isPresent(parts[1])) { // Min integer digits.
minInt = NumberWrapper.parseIntAutoRadix(parts[1]);
}
if (isPresent(parts[3])) { // Min fraction digits.
minFraction = NumberWrapper.parseIntAutoRadix(parts[3]);
}
if (isPresent(parts[5])) { // Max fraction digits.
maxFraction = NumberWrapper.parseIntAutoRadix(parts[5]);
}

}

return NumberFormatter.format(value, defaultLocale, style, {
minimumIntegerDigits: minInt,
minimumFractionDigits: minFraction,
maximumFractionDigits: maxFraction,
currency: currency,
currencyAsSymbol: currencyAsSymbol
});

}
}

/**
* 'localedecimal' pipe function.
*/
Expand Down Expand Up @@ -148,7 +96,9 @@ import {LocaleService} from '../services/locale.service';
}

return this.localeDecimal;

}

}

/**
Expand Down Expand Up @@ -230,7 +180,9 @@ import {LocaleService} from '../services/locale.service';
}

return this.localePercent;

}

}

/**
Expand Down Expand Up @@ -326,5 +278,7 @@ import {LocaleService} from '../services/locale.service';
}

return this.localeCurrency;

}
}

}
Loading

0 comments on commit 79caa2a

Please sign in to comment.