Skip to content

Commit

Permalink
fix(limel-date-picker): translate based on language property
Browse files Browse the repository at this point in the history
fix #241
  • Loading branch information
BregenzerK authored and adrianschmidt committed Sep 10, 2019
1 parent b902f5c commit 677fad7
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 33 deletions.
36 changes: 31 additions & 5 deletions src/components/date-picker/date-picker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@stencil/core';

import { Translations } from '../../global/translations';
import { DateType } from './date-type';
import { DateType, Languages } from './date-type';
import { DatePicker as DateDatePicker } from './pickers/DatePicker';
import { DatetimePicker } from './pickers/DatetimePicker';
import { MonthPicker } from './pickers/MonthPicker';
Expand Down Expand Up @@ -70,6 +70,13 @@ export class DatePicker {
@Prop()
public format: string;

/**
* Defines the localisation for translations and date formatting.
* Property `format` customizes the localized date format.
*/
@Prop()
public language: Languages = 'en';

@Prop({ context: 'translations' })
public translate: Translations;

Expand All @@ -90,20 +97,33 @@ export class DatePicker {
public componentWillLoad() {
switch (this.type) {
case 'date':
this.picker = new DateDatePicker(this.format, this.change);
this.picker = new DateDatePicker(
this.format,
this.language,
this.change
);
break;

case 'time':
this.picker = new TimePicker(this.format, this.change);
this.picker = new TimePicker(
this.format,
this.language,
this.change
);
break;

case 'week':
this.picker = new WeekPicker(this.format, this.change);
this.picker = new WeekPicker(
this.format,
this.language,
this.change
);
break;

case 'month':
this.picker = new MonthPicker(
this.format,
this.language,
this.change,
this.translate
);
Expand All @@ -112,21 +132,27 @@ export class DatePicker {
case 'quarter':
this.picker = new QuarterPicker(
this.format,
this.language,
this.change,
this.translate
);
break;
case 'year':
this.picker = new YearPicker(
this.format,
this.language,
this.change,
this.translate
);
break;

case 'datetime':
default:
this.picker = new DatetimePicker(this.format, this.change);
this.picker = new DatetimePicker(
this.format,
this.language,
this.change
);
break;
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/date-picker/date-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,5 @@ export type DateType =
| 'month'
| 'quarter'
| 'year';

export type Languages = 'en' | 'sv' | 'no' | 'fi' | 'da';
8 changes: 6 additions & 2 deletions src/components/date-picker/pickers/DatePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { BaseOptions } from 'flatpickr/dist/types/options';
import { Picker } from './Picker';

export class DatePicker extends Picker {
public constructor(dateFormat: string = 'L', change: EventEmitter) {
super(dateFormat, change);
public constructor(
dateFormat: string = 'L',
language: string,
change: EventEmitter
) {
super(dateFormat, language, change);
}

public getConfig(nativePicker: boolean): Partial<BaseOptions> {
Expand Down
8 changes: 6 additions & 2 deletions src/components/date-picker/pickers/DatetimePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { BaseOptions } from 'flatpickr/dist/types/options';
import { Picker } from './Picker';

export class DatetimePicker extends Picker {
public constructor(dateFormat: string = 'L - LT', change: EventEmitter) {
super(dateFormat, change);
public constructor(
dateFormat: string = 'L - LT',
language: string,
change: EventEmitter
) {
super(dateFormat, language, change);
}

public getConfig(nativePicker: boolean): Partial<BaseOptions> {
Expand Down
8 changes: 3 additions & 5 deletions src/components/date-picker/pickers/MonthPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export class MonthPicker extends Picker {

public constructor(
dateFormat: string = 'MM/YYYY',
language: string,
change: EventEmitter,
private translations: Translations
) {
super(dateFormat, change);
super(dateFormat, language, change);
this.handleChange = this.handleChange.bind(this);
this.handleReady = this.handleReady.bind(this);
this.handleOpen = this.handleOpen.bind(this);
Expand Down Expand Up @@ -87,12 +88,9 @@ export class MonthPicker extends Picker {
}

private getLocalizedHeading() {
const languageCodeDash = navigator.language.indexOf('-');
const languageCodeLength =
languageCodeDash > 0 ? languageCodeDash : navigator.language.length;
return this.translations.get(
'date-picker.month.heading',
navigator.language.substring(0, languageCodeLength)
this.language
);
}

Expand Down
23 changes: 20 additions & 3 deletions src/components/date-picker/pickers/Picker.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { EventEmitter } from '@stencil/core';
import flatpickr from 'flatpickr';
import FlatpickrLanguages from 'flatpickr/dist/l10n';
import { Instance } from 'flatpickr/dist/types/instance';
import { BaseOptions } from 'flatpickr/dist/types/options';
import 'moment/locale/da';
import 'moment/locale/fi';
import 'moment/locale/nb';
import 'moment/locale/sv';
import moment from 'moment/moment';
import { isAndroidDevice, isIOSDevice } from '../../../util/device';

export abstract class Picker {
protected dateFormat: string;
protected language: string = 'en';

protected flatpickr: Instance;

private nativePicker;

public constructor(dateFormat: string, protected change: EventEmitter) {
public constructor(
dateFormat: string,
language: string,
protected change: EventEmitter
) {
this.language = language;
const isMobile = isIOSDevice() || isAndroidDevice();
this.nativePicker = isMobile;

Expand All @@ -35,6 +45,7 @@ export abstract class Picker {
parseDate: this.nativePicker ? undefined : this.parseDate,
appendTo: container,
defaultDate: value,
locale: FlatpickrLanguages[this.language] || 'en',
};

config = { ...config, ...this.getConfig(this.nativePicker) };
Expand All @@ -53,7 +64,9 @@ export abstract class Picker {

public formatDate(date: Date) {
if (date) {
return moment(date).format(this.dateFormat);
return moment(date)
.locale(this.getMomentLang())
.format(this.dateFormat);
}
return '';
}
Expand All @@ -65,6 +78,10 @@ export abstract class Picker {
this.change.emit(date);
}

private getMomentLang() {
return this.language === 'no' ? 'nb' : this.language;
}

private getWeek(date) {
return moment(date).isoWeek();
}
Expand Down
8 changes: 3 additions & 5 deletions src/components/date-picker/pickers/QuarterPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ export class QuarterPicker extends Picker {

public constructor(
dateFormat: string = '[Q]Q YYYY',
language: string,
protected change: EventEmitter,
private translations: Translations
) {
super(dateFormat, change);
super(dateFormat, language, change);
this.handleChange = this.handleChange.bind(this);
this.handleReady = this.handleReady.bind(this);
this.handleOpen = this.handleOpen.bind(this);
Expand Down Expand Up @@ -92,12 +93,9 @@ export class QuarterPicker extends Picker {
}

private getLocalizedHeading() {
const languageCodeDash = navigator.language.indexOf('-');
const languageCodeLength =
languageCodeDash > 0 ? languageCodeDash : navigator.language.length;
return this.translations.get(
'date-picker.quarter.heading',
navigator.language.substring(0, languageCodeLength)
this.language
);
}

Expand Down
8 changes: 6 additions & 2 deletions src/components/date-picker/pickers/TimePicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ import { BaseOptions } from 'flatpickr/dist/types/options';
import { Picker } from './Picker';

export class TimePicker extends Picker {
public constructor(dateFormat: string = 'LT', change: EventEmitter) {
super(dateFormat, change);
public constructor(
dateFormat: string = 'LT',
language: string,
change: EventEmitter
) {
super(dateFormat, language, change);
}

public getConfig(): Partial<BaseOptions> {
Expand Down
3 changes: 2 additions & 1 deletion src/components/date-picker/pickers/WeekPicker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ import { Picker } from './Picker';
export class WeekPicker extends Picker {
public constructor(
dateFormat: string = '[w] W GGGG',
language: string,
change: EventEmitter
) {
super(dateFormat, change);
super(dateFormat, language, change);
}

public getConfig(nativePicker: boolean): Partial<BaseOptions> {
Expand Down
11 changes: 3 additions & 8 deletions src/components/date-picker/pickers/YearPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ export class YearPicker extends Picker {

public constructor(
dateFormat: string = 'YYYY',
language: string,
change: EventEmitter,
private translations: Translations
) {
super(dateFormat, change);
super(dateFormat, language, change);
this.handleChange = this.handleChange.bind(this);
this.handleReady = this.handleReady.bind(this);
}
Expand Down Expand Up @@ -61,13 +62,7 @@ export class YearPicker extends Picker {
}

private getLocalizedHeading() {
const languageCodeDash = navigator.language.indexOf('-');
const languageCodeLength =
languageCodeDash > 0 ? languageCodeDash : navigator.language.length;
return this.translations.get(
'date-picker.year.heading',
navigator.language.substring(0, languageCodeLength)
);
return this.translations.get('date-picker.year.heading', this.language);
}

private renderYearPicker(fp): any {
Expand Down

0 comments on commit 677fad7

Please sign in to comment.