Skip to content

Commit

Permalink
Added Collator & Intl support
Browse files Browse the repository at this point in the history
  • Loading branch information
robisim74 committed May 12, 2016
1 parent 5ecd8f4 commit 08c8c5f
Show file tree
Hide file tree
Showing 9 changed files with 554 additions and 107 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ Now you can localize a list simply. For example:
</md-card-content>
</md-card>
```
#### Sorting & search
`LocalizationService` has the following methods for sorting and filtering by locales:
* `sort(list: Array<any>, keyName: any, order?: string, extension?: string, options?: any): Array<any>`
* `sortAsync(list: Array<any>, keyName: any, order?: string, extension?: string, options?: any): Observable<Array<any>>`
* `search(s: string, list: Array<any>, keyNames: any[], options: any = { usage: 'search' }): Array<any>`
* `searchAsync(s: string, list: Array<any>, keyNames: any[], options: any = { usage: 'search' }): Observable<any>`

For more information see the internal documentation. N.B. This feature is not supported by all browsers, even with the use of `Intl.js`.

### Advanced use
If you want, you can avoid including `get lang()`, `get defaultLocale()` or `get currency()` by extending the `Locale` superclass in components:
Expand Down Expand Up @@ -338,6 +346,8 @@ Just add one script tag in your `index.html`:
```
When specifying the `features`, you have to specify what locale, or locales to load.

N.B. When a feature is not supported, however, for example in older browsers, now `angular2localization` does not generate an error in the browser, but returns the value without performing operations.

## Boilerplates
[Angular 2 Localization with an ASP.NET CORE MVC Service](https://damienbod.com/2016/04/29/angular-2-localization-with-an-asp-net-core-mvc-service/) @damienbod

Expand Down
1 change: 1 addition & 0 deletions angular2localization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './src/services/localization.service';
export * from './src/services/locale.service';
export * from './src/services/locale';
export * from './src/services/locale-number';
export * from './src/services/Intl-support';
export * from './src/pipes/translate.pipe';
export * from './src/pipes/locale-date.pipe';
export * from './src/pipes/locale-number.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.7.0",
"version": "0.7.1",
"description": "An Angular 2 library to translate messages, dates and numbers",
"main": "",
"scripts": {},
Expand Down
79 changes: 44 additions & 35 deletions src/pipes/locale-date.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ import {InvalidPipeArgumentException} from '@angular/common/src/pipes/invalid_pi

// Services.
import {LocaleService} from '../services/locale.service';
import {IntlSupport} from '../services/Intl-support';

/**
* 'localedate' pipe function.
*/
@Pipe({
name: 'localedate',
pure: true
name: 'localedate',
pure: true
})

/**
Expand Down Expand Up @@ -66,53 +67,61 @@ import {LocaleService} from '../services/locale.service';
*/
@Injectable() export class LocaleDatePipe implements PipeTransform {

static ALIASES: { [key: string]: String } = {
'medium': 'yMMMdjms',
'short': 'yMdjm',
'fullDate': 'yMMMMEEEEd',
'longDate': 'yMMMMd',
'mediumDate': 'yMMMd',
'shortDate': 'yMd',
'mediumTime': 'jms',
'shortTime': 'jm'
};
static ALIASES: { [key: string]: String } = {
'medium': 'yMMMdjms',
'short': 'yMdjm',
'fullDate': 'yMMMMEEEEd',
'longDate': 'yMMMMd',
'mediumDate': 'yMMMd',
'shortDate': 'yMd',
'mediumTime': 'jms',
'shortTime': 'jm'
};

constructor(public locale: LocaleService) { }
constructor(public locale: LocaleService) { }

/**
* LocaleDatePipe transform method.
*
* @param value The date to be localized
* @param defaultLocale The default locale
* @param pattern The format of the date
* @return The locale date
*/
transform(value: any, defaultLocale: string, pattern: string = 'mediumDate'): string {
/**
* LocaleDatePipe transform method.
*
* @param value The date to be localized
* @param defaultLocale The default locale
* @param pattern The format of the date
* @return The locale date
*/
transform(value: any, defaultLocale: string, pattern: string = 'mediumDate'): string {

if (isBlank(value)) return null;
if (isBlank(value)) return null;

if (!this.supports(value)) {
if (!this.supports(value)) {

throw new InvalidPipeArgumentException(LocaleDatePipe, value);
throw new InvalidPipeArgumentException(LocaleDatePipe, value);

}
}

if (isNumber(value)) {
if (isNumber(value)) {

value = DateWrapper.fromMillis(value);
value = <Date>DateWrapper.fromMillis(value);

}
}

if (StringMapWrapper.contains(LocaleDatePipe.ALIASES, pattern)) {
// Checks for support for Intl.
if (IntlSupport.DateTimeFormat(defaultLocale) == true) {

pattern = <string>StringMapWrapper.get(LocaleDatePipe.ALIASES, pattern);
if (StringMapWrapper.contains(LocaleDatePipe.ALIASES, pattern)) {

}
pattern = <string>StringMapWrapper.get(LocaleDatePipe.ALIASES, pattern);

}

return DateFormatter.format(value, defaultLocale, pattern);
return DateFormatter.format(value, defaultLocale, pattern);

}
}

// Returns the date without localization.
return value;

}

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

}
143 changes: 81 additions & 62 deletions src/pipes/locale-number.pipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import {Injectable, Pipe, PipeTransform} from '@angular/core';
import {NumberFormatStyle} from '@angular/common/src/facade/intl';

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

/**
* 'localedecimal' pipe function.
*/
@Pipe({
name: 'localedecimal',
pure: true
name: 'localedecimal',
pure: true
})

/**
Expand Down Expand Up @@ -64,34 +65,40 @@ import {LocaleService} from '../services/locale.service';
* @author Roberto Simonetti
* @see Angular 2 DecimalPipe for further information
*/
@Injectable() export class LocaleDecimalPipe extends LocaleNumber implements PipeTransform {
@Injectable() export class LocaleDecimalPipe implements PipeTransform {

constructor(public locale: LocaleService) { }

/**
* LocaleDecimalPipe transform method.
*
* @param value The number to be localized
* @param defaultLocale The default locale
* @param digits The format of the number
* @return The locale decimal
*/
transform(value: any, defaultLocale: string, digits: string = null): string {

constructor(public locale: LocaleService) {
super();
}
// Checks for support for Intl.
if (IntlSupport.NumberFormat(defaultLocale) == true) {

/**
* LocaleDecimalPipe transform method.
*
* @param value The number to be localized
* @param defaultLocale The default locale
* @param digits The format of the number
* @return The locale decimal
*/
transform(value: any, defaultLocale: string, digits: string = null): string {
return LocaleNumber.format(defaultLocale, value, NumberFormatStyle.Decimal, digits);

return LocaleNumber.format(defaultLocale, value, NumberFormatStyle.Decimal, digits);
}

}
// Returns the number without localization.
return value;

}

}

/**
* 'localepercent' pipe function.
*/
@Pipe({
name: 'localepercent',
pure: true
name: 'localepercent',
pure: true
})

/**
Expand Down Expand Up @@ -133,34 +140,40 @@ import {LocaleService} from '../services/locale.service';
* @author Roberto Simonetti
* @see Angular 2 PercentPipe for further information
*/
@Injectable() export class LocalePercentPipe extends LocaleNumber implements PipeTransform {
@Injectable() export class LocalePercentPipe implements PipeTransform {

constructor(public locale: LocaleService) { }

/**
* LocalePercentPipe transform method.
*
* @param value The number to be localized
* @param defaultLocale The default locale
* @param digits The format of the number
* @return The locale percent
*/
transform(value: any, defaultLocale: string, digits: string = null): string {

// Checks for support for Intl.
if (IntlSupport.NumberFormat(defaultLocale) == true) {

constructor(public locale: LocaleService) {
super();
}
return LocaleNumber.format(defaultLocale, value, NumberFormatStyle.Percent, digits);

/**
* LocalePercentPipe transform method.
*
* @param value The number to be localized
* @param defaultLocale The default locale
* @param digits The format of the number
* @return The locale percent
*/
transform(value: any, defaultLocale: string, digits: string = null): string {
}

return LocaleNumber.format(defaultLocale, value, NumberFormatStyle.Percent, digits);
// Returns the number without localization.
return value;

}
}

}

/**
* 'localecurrency' pipe function.
*/
@Pipe({
name: 'localecurrency',
pure: true
name: 'localecurrency',
pure: true
})

/**
Expand Down Expand Up @@ -211,30 +224,36 @@ import {LocaleService} from '../services/locale.service';
* @author Roberto Simonetti
* @see Angular 2 CurrencyPipe for further information
*/
@Injectable() export class LocaleCurrencyPipe extends LocaleNumber implements PipeTransform {

constructor(public locale: LocaleService) {
super();
}

/**
* LocaleCurrencyPipe transform method.
*
* @param value The number to be localized
* @param defaultLocale The default locale
* @param currency The current currency
* @param symbolDisplay Indicates whether to use the currency symbol
* @param digits The format of the number
* @return The locale currency
*/
transform(value: any,
defaultLocale: string,
currency: string,
symbolDisplay: boolean = false,
digits: string = null): string {

return LocaleNumber.format(defaultLocale, value, NumberFormatStyle.Currency, digits, currency, symbolDisplay);

}
@Injectable() export class LocaleCurrencyPipe implements PipeTransform {

constructor(public locale: LocaleService) { }

/**
* LocaleCurrencyPipe transform method.
*
* @param value The number to be localized
* @param defaultLocale The default locale
* @param currency The current currency
* @param symbolDisplay Indicates whether to use the currency symbol
* @param digits The format of the number
* @return The locale currency
*/
transform(value: any,
defaultLocale: string,
currency: string,
symbolDisplay: boolean = false,
digits: string = null): string {

// Checks for support for Intl.
if (IntlSupport.NumberFormat(defaultLocale) == true) {

return LocaleNumber.format(defaultLocale, value, NumberFormatStyle.Currency, digits, currency, symbolDisplay);

}

// Returns the number without localization & currency.
return value + " " + currency;

}

}
Loading

0 comments on commit 08c8c5f

Please sign in to comment.