Skip to content

Commit

Permalink
feat: add option to override locale
Browse files Browse the repository at this point in the history
  • Loading branch information
json-derulo committed Feb 20, 2023
1 parent bae651a commit 3dd0d32
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Added

* Add option to override locale

## 0.1.0 - 2023-02-19

### Added
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,11 @@ loading any locale date. This package re-implements some Angular built-in pipes

## Feature Roadmap

* Number pipe(s): decimal, currency, percentage
* Performance: Prepare Intl.* object with default options, only construct new object when necessary
* Country pipe
* Number pipe(s)
* Relative time
* Migration Schematics for usages of Angular pipes

## Chore Roadmap

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,22 @@ describe('DatePipe', () => {
});
testUnit = TestBed.inject(IntlDatePipe);

expect(testUnit.transform('2023-02-19', { dateStyle: 'medium' })).toEqual('Feb 19, 2023');
expect(testUnit.transform('2023-02-19', {dateStyle: 'medium'})).toEqual('Feb 19, 2023');
});
});

it('should respect locale option', () => {
TestBed.configureTestingModule({
providers: [
IntlDatePipe,
{
provide: INTL_LOCALES,
useValue: 'en-US',
},
],
});
testUnit = TestBed.inject(IntlDatePipe);

expect(testUnit.transform('2023-02-19', {locale: 'de-DE'})).toEqual('19.2.2023');
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {Inject, Optional, Pipe, PipeTransform} from '@angular/core';
import {INTL_LOCALES} from "../locale";
import {INTL_DATE_PIPE_DEFAULT_OPTIONS} from "./intl-date-pipe-default-options";
import {IntlPipeOptions} from "../intl-pipe-options";

export type IntlDatePipeOptions = Partial<Intl.DateTimeFormatOptions>;
export type IntlDatePipeOptions = Partial<Intl.DateTimeFormatOptions> & IntlPipeOptions;

@Pipe({
name: 'intlDate',
Expand All @@ -24,8 +25,10 @@ export class IntlDatePipe implements PipeTransform {
return null;
}

const {locale, ...intlOptions} = options ?? {};

try {
return new Intl.DateTimeFormat(this.locale ?? undefined, {...this.defaultOptions, ...options}).format(date);
return new Intl.DateTimeFormat(locale ?? this.locale ?? undefined, {...this.defaultOptions, ...intlOptions}).format(date);
} catch (e) {
console.error('Error while transforming the date', e);
return date.toString();
Expand Down
3 changes: 3 additions & 0 deletions projects/angular-ecmascript-intl/src/lib/intl-pipe-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface IntlPipeOptions {
locale?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,19 @@ describe('IntlLanguagePipe', () => {
expect(testUnit.transform('de', {type: 'region'})).toEqual('Deutsch');
});
});

it('should respect locale option', () => {
TestBed.configureTestingModule({
providers: [
IntlLanguagePipe,
{
provide: INTL_LOCALES,
useValue: 'en-US',
},
],
});
testUnit = TestBed.inject(IntlLanguagePipe);

expect(testUnit.transform('en-US', {locale: 'de-DE'})).toEqual('Englisch (Vereinigte Staaten)');
});
});
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import {Inject, Optional, Pipe, PipeTransform} from '@angular/core';
import {INTL_LOCALES} from "../locale";
import {INTL_LANGUAGE_PIPE_DEFAULT_OPTIONS} from "./intl-language-pipe-default-options";
import {IntlPipeOptions} from "../intl-pipe-options";

export type IntlLanguagePipeOptions = Partial<Intl.DisplayNamesOptions>;
export type IntlLanguagePipeOptions = Partial<Intl.DisplayNamesOptions> & IntlPipeOptions;

@Pipe({
name: 'intlLanguage',
Expand All @@ -19,10 +20,12 @@ export class IntlLanguagePipe implements PipeTransform {
return null;
}

const {locale, ...intlOptions} = options ?? {};

try {
return new Intl.DisplayNames(this.locale ?? undefined, {
...this.defaultOptions, ...options,
type: 'language'
return new Intl.DisplayNames(locale ?? this.locale ?? undefined, {
...this.defaultOptions, ...intlOptions,
type: 'language',
}).of(value) ?? null;
} catch (e) {
console.error('Error while transforming the language', e);
Expand Down
14 changes: 11 additions & 3 deletions projects/angular-intl-demo/src/app/date/date.component.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
<div class="fields-container">
<mat-form-field>
<mat-label>Locale</mat-label>
<mat-select [(ngModel)]="locale">
<mat-option [value]="undefined">Browser default</mat-option>
<mat-option *ngFor="let language of languages" [value]="language">{{language}}</mat-option>
</mat-select>
</mat-form-field>

<mat-form-field>
<mat-label>Date style</mat-label>
<mat-select [(ngModel)]="dateStyle">
<mat-option [value]="undefined">-</mat-option>
<mat-option [value]="undefined">Browser default</mat-option>
<mat-option [value]="'short'">short</mat-option>
<mat-option [value]="'medium'">medium</mat-option>
<mat-option [value]="'long'">long</mat-option>
Expand All @@ -13,7 +21,7 @@
<mat-form-field>
<mat-label>Time style</mat-label>
<mat-select [(ngModel)]="timeStyle">
<mat-option [value]="undefined">-</mat-option>
<mat-option [value]="undefined">Browser default</mat-option>
<mat-option [value]="'short'">short</mat-option>
<mat-option [value]="'medium'">medium</mat-option>
<mat-option [value]="'long'">long</mat-option>
Expand All @@ -26,4 +34,4 @@
</mat-slide-toggle>
</div>

<p>{{selectedDate | intlDate: { dateStyle, timeStyle, hour12 } }}</p>
<p>{{selectedDate | intlDate: {dateStyle, timeStyle, hour12, locale} }}</p>
5 changes: 4 additions & 1 deletion projects/angular-intl-demo/src/app/date/date.component.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Component } from '@angular/core';
import {Component} from '@angular/core';
import {languages} from '../languages';

@Component({
selector: 'app-date',
templateUrl: './date.component.html',
styleUrls: ['./date.component.scss']
})
export class DateComponent {
languages = languages;
selectedDate = new Date();
dateStyle: Intl.DateTimeFormatOptions['dateStyle'];
timeStyle: Intl.DateTimeFormatOptions['timeStyle'];
hour12: Intl.DateTimeFormatOptions['hour12'];
locale?: string;
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,27 @@
<div class="fields-container">
<mat-form-field>
<mat-label>Language</mat-label>
<mat-label>Language to transform</mat-label>
<mat-select [(ngModel)]="selectedLanguage">
<mat-option *ngFor="let language of languages" [value]="language">{{language}}</mat-option>
</mat-select>
</mat-form-field>

<mat-form-field>
<mat-label>Locale</mat-label>
<mat-select [(ngModel)]="locale">
<mat-option [value]="undefined">Browser default</mat-option>
<mat-option *ngFor="let language of languages" [value]="language">{{language}}</mat-option>
</mat-select>
</mat-form-field>

<mat-form-field>
<mat-label>Language display</mat-label>
<mat-select [(ngModel)]="languageDisplay">
<mat-option [value]="undefined">-</mat-option>
<mat-option [value]="undefined">Browser default</mat-option>
<mat-option [value]="'dialect'">dialect</mat-option>
<mat-option [value]="'standard'">standard</mat-option>
</mat-select>
</mat-form-field>
</div>

<p>{{selectedLanguage | intlLanguage: {languageDisplay} }}</p>
<p>{{selectedLanguage | intlLanguage: {languageDisplay, locale} }}</p>
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Component} from '@angular/core';
import {languages} from "./languages";
import {languages} from "../languages";
import {IntlLanguagePipeOptions} from "projects/angular-ecmascript-intl/src/lib/language/intl-language.pipe";

@Component({
Expand All @@ -11,4 +11,5 @@ export class LanguageComponent {
languages = languages;
selectedLanguage = 'de-DE';
languageDisplay?: IntlLanguagePipeOptions['languageDisplay'];
locale?: string;
}

0 comments on commit 3dd0d32

Please sign in to comment.