Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bs-moment): add localization tests #2466

Merged
merged 4 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 46 additions & 42 deletions demo/src/ng-api-doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,19 @@ export const ngdoc: any = {
"properties": []
},
"LocaleData": {
"fileName": "src/datepicker/models/index.ts",
"fileName": "src/bs-moment/locale/locale.class.ts",
"className": "LocaleData",
"description": "",
"methods": [],
"properties": []
},
"TimeUnit": {
"fileName": "src/bs-moment/types.ts",
"className": "TimeUnit",
"description": "",
"methods": [],
"properties": []
},
"ButtonCheckboxDirective": {
"fileName": "src/buttons/button-checkbox.directive.ts",
"className": "ButtonCheckboxDirective",
Expand Down Expand Up @@ -489,9 +496,14 @@ export const ngdoc: any = {
"fileName": "src/datepicker/bs-datepicker.component.ts",
"className": "BsDatepickerComponent",
"description": "",
"selector": "bs-datepicker",
"selector": "bs-datepicker,[bsDatepicker]",
"exportAs": "bsDatepicker",
"inputs": [
{
"name": "bsValue",
"type": "Date",
"description": ""
},
{
"name": "container",
"defaultValue": "body",
Expand All @@ -503,6 +515,12 @@ export const ngdoc: any = {
"type": "boolean",
"description": "<p>Returns whether or not the popover is currently being shown</p>\n"
},
{
"name": "outsideClick",
"defaultValue": "true",
"type": "boolean",
"description": ""
},
{
"name": "placement",
"defaultValue": "bottom",
Expand All @@ -514,25 +532,20 @@ export const ngdoc: any = {
"defaultValue": "click",
"type": "string",
"description": "<p>Specifies events that should trigger. Supports a space separated list of\nevent names.</p>\n"
},
{
"name": "value",
"type": "Date",
"description": ""
}
],
"outputs": [
{
"name": "bsValueChange",
"description": ""
},
{
"name": "onHidden",
"description": "<p>Emits an event when the popover is hidden</p>\n"
},
{
"name": "onShown",
"description": "<p>Emits an event when the popover is shown</p>\n"
},
{
"name": "valueChange",
"description": ""
}
],
"properties": [],
Expand Down Expand Up @@ -561,8 +574,14 @@ export const ngdoc: any = {
"fileName": "src/datepicker/bs-daterangepicker.component.ts",
"className": "BsDaterangepickerComponent",
"description": "",
"selector": "bs-daterangepicker",
"selector": "bs-daterangepicker,[bsDaterangepicker]",
"exportAs": "bsDaterangepicker",
"inputs": [
{
"name": "bsValue",
"type": "Date[]",
"description": ""
},
{
"name": "container",
"defaultValue": "body",
Expand All @@ -574,6 +593,12 @@ export const ngdoc: any = {
"type": "boolean",
"description": "<p>Returns whether or not the popover is currently being shown</p>\n"
},
{
"name": "outsideClick",
"defaultValue": "true",
"type": "boolean",
"description": ""
},
{
"name": "placement",
"defaultValue": "bottom",
Expand All @@ -585,25 +610,20 @@ export const ngdoc: any = {
"defaultValue": "click",
"type": "string",
"description": "<p>Specifies events that should trigger. Supports a space separated list of\nevent names.</p>\n"
},
{
"name": "value",
"type": "Date[]",
"description": ""
}
],
"outputs": [
{
"name": "bsValueChange",
"description": ""
},
{
"name": "onHidden",
"description": "<p>Emits an event when the popover is hidden</p>\n"
},
{
"name": "onShown",
"description": "<p>Emits an event when the popover is shown</p>\n"
},
{
"name": "valueChange",
"description": ""
}
],
"properties": [],
Expand Down Expand Up @@ -967,13 +987,6 @@ export const ngdoc: any = {
"methods": [],
"properties": []
},
"TimeUnit": {
"fileName": "src/datepicker/models/index.ts",
"className": "TimeUnit",
"description": "",
"methods": [],
"properties": []
},
"BsNavigationEvent": {
"fileName": "src/datepicker/models/index.ts",
"className": "BsNavigationEvent",
Expand Down Expand Up @@ -1040,28 +1053,19 @@ export const ngdoc: any = {
"properties": [],
"methods": []
},
"BsDatepickerDayViewComponent": {
"fileName": "src/datepicker/themes/bs/bs-datepicker-day-view.component.ts",
"className": "BsDatepickerDayViewComponent",
"BsDatepickerDayDecoratorComponent": {
"fileName": "src/datepicker/themes/bs/bs-datepicker-day-decorator.directive.ts",
"className": "BsDatepickerDayDecoratorComponent",
"description": "",
"selector": "bs-datepicker-day-view",
"selector": "[bsDatepickerDayDecorator]",
"inputs": [
{
"name": "day",
"type": "DayViewModel",
"description": ""
}
],
"outputs": [
{
"name": "onHover",
"description": ""
},
{
"name": "onSelect",
"description": ""
}
],
"outputs": [],
"properties": [],
"methods": []
},
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"demo.docs": "node scripts/docs/get-doc.js",
"demo.serve": "run-s build link demo.docs demo.build lite-server",
"demo.gh-pages": "run-s build demo.docs demo.build demo.deploy",
"demo.build": "ng build -prod --aot && npm run generate-bs4",
"demo.build": "ng build -prod --aot --build-optimizer && npm run generate-bs4",
"demo.deploy": "gh-pages -d demo/dist",
"demo-beta-deploy": "gh-pages -d demo/dist -r [email protected]:valorkin/ngx-bootstrap.git -b gh-pages",
"link": "ngm link -p src --here",
Expand Down Expand Up @@ -55,8 +55,7 @@
"url": "https://github.com/valor-software/ngx-bootstrap/issues"
},
"homepage": "https://github.com/valor-software/ngx-bootstrap#readme",
"dependencies": {
},
"dependencies": {},
"peerDependencies": {
"@angular/common": "^2.3.1 || >=4.0.0",
"@angular/compiler": "^2.3.1 || >=4.0.0",
Expand Down
8 changes: 4 additions & 4 deletions src/bs-moment/format-functions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Locale } from './locale/locale.class';
import { DateFormatterFn } from '../datepicker/models/index';
import { zeroFill } from './utils';
import { isFunction } from './utils/type-checks';
import { DateFormatterFn } from '../datepicker/models/index';

export let formatFunctions: {[key:string]: (date: Date, locale: Locale) => string} = {};
export let formatFunctions: { [key: string]: (date: Date, locale: Locale) => string } = {};
export let formatTokenFunctions: { [key: string]: DateFormatterFn } = {};

export const formattingTokens = /(\[[^\[]*\])|(\\)?([Hh]mm(ss)?|Mo|MM?M?M?|Do|DDDo|DD?D?D?|ddd?d?|do?|w[o|w]?|W[o|W]?|Qo?|YYYYYY|YYYYY|YYYY|YY|gg(ggg?)?|GG(GGG?)?|e|E|a|A|hh?|HH?|kk?|mm?|ss?|S{1,9}|x|X|zz?|ZZ?|.)/g;
Expand All @@ -13,14 +13,14 @@ export const formattingTokens = /(\[[^\[]*\])|(\\)?([Hh]mm(ss)?|Mo|MM?M?M?|Do|DD
// ordinal: 'Mo'
// callback: function () { this.month() + 1 }
export function addFormatToken(token: string,
padded: {[key: number]: any},
padded: { [key: number]: any },
ordinal: string,
callback: DateFormatterFn): void {
let func: DateFormatterFn = callback;
if (token) {
formatTokenFunctions[token] = func;
}
if (padded as {[key: number]: any}) {
if (padded as { [key: number]: any }) {
let key = padded[0] as string;
formatTokenFunctions[key] = function (date: Date, format: string, locale?: Locale): string {
return zeroFill(func.apply(null, arguments), padded[1] as number, padded[2] as boolean);
Expand Down
2 changes: 1 addition & 1 deletion src/bs-moment/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

import { formatFunctions, makeFormatFunction } from './format-functions';
import './locale';
import './units';
import { Locale } from './locale/locale.class';
import { getLocale } from './locale/locales.service';
import './units';
import { isDateValid } from './utils/type-checks';

export function formatDate(date: Date, format: string, locale = 'en'): string {
Expand Down
33 changes: 18 additions & 15 deletions src/bs-moment/i18n/ar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
// author : Ahmed Elkhatib
// author : forabi https://github.com/forabi

import { LocaleData } from '../locale/locale.class';

const symbolMap: { [key: string]: string } = {
'1': '١',
'2': '٢',
Expand Down Expand Up @@ -31,22 +33,23 @@ const numberMap: { [key: string]: string } = {
const pluralForm = function (n: number): number {
return n === 0 ? 0 : n === 1 ? 1 : n === 2 ? 2 : n % 100 >= 3 && n % 100 <= 10 ? 3 : n % 100 >= 11 ? 4 : 5;
};
const plurals = {
const plurals: any = {
s: ['أقل من ثانية', 'ثانية واحدة', ['ثانيتان', 'ثانيتين'], '%d ثوان', '%d ثانية', '%d ثانية'],
m: ['أقل من دقيقة', 'دقيقة واحدة', ['دقيقتان', 'دقيقتين'], '%d دقائق', '%d دقيقة', '%d دقيقة'],
h: ['أقل من ساعة', 'ساعة واحدة', ['ساعتان', 'ساعتين'], '%d ساعات', '%d ساعة', '%d ساعة'],
d: ['أقل من يوم', 'يوم واحد', ['يومان', 'يومين'], '%d أيام', '%d يومًا', '%d يوم'],
M: ['أقل من شهر', 'شهر واحد', ['شهران', 'شهرين'], '%d أشهر', '%d شهرا', '%d شهر'],
y: ['أقل من عام', 'عام واحد', ['عامان', 'عامين'], '%d أعوام', '%d عامًا', '%d عام']
};
const pluralize = function (u) {
return function (number, withoutSuffix, string, isFuture) {
const f = pluralForm(number);
let str = plurals[u][pluralForm(number)];
const pluralize = function (u: string) {
return function (num: number, withoutSuffix: boolean/*, string, isFuture*/) {
const f = pluralForm(num);
let str = plurals[u][pluralForm(num)];
if (f === 2) {
str = str[withoutSuffix ? 0 : 1];
}
return str.replace(/%d/i, number);

return str.replace(/%d/i, num);
};
};
const months = [
Expand All @@ -64,9 +67,9 @@ const months = [
'كانون الأول ديسمبر'
];

export const arLocale = {
export const ar: LocaleData = {
abbr: 'ar',
months: months,
months,
monthsShort: months,
weekdays: 'الأحد_الإثنين_الثلاثاء_الأربعاء_الخميس_الجمعة_السبت'.split('_'),
weekdaysShort: 'أحد_إثنين_ثلاثاء_أربعاء_خميس_جمعة_سبت'.split('_'),
Expand All @@ -81,10 +84,10 @@ export const arLocale = {
LLLL: 'dddd D MMMM YYYY HH:mm'
},
meridiemParse: /ص|م/,
isPM: function (input) {
return 'م' === input;
isPM(input: string): boolean {
return input === 'م';
},
meridiem: function (hour, minute, isLower) {
meridiem(hour: number, minute: number, isLower: boolean): string {
if (hour < 12) {
return 'ص';
} else {
Expand Down Expand Up @@ -114,13 +117,13 @@ export const arLocale = {
y: pluralize('y'),
yy: pluralize('y')
},
preparse: function (string) {
return string.replace(/[١٢٣٤٥٦٧٨٩٠]/g, function (match) {
preparse(str: string): string {
return str.replace(/[١٢٣٤٥٦٧٨٩٠]/g, function (match) {
return numberMap[match];
}).replace(/،/g, ',');
},
postformat: function (string) {
return string.replace(/\d/g, function (match) {
postformat(str: string): string {
return str.replace(/\d/g, function (match) {
return symbolMap[match];
}).replace(/,/g, '،');
},
Expand Down
18 changes: 14 additions & 4 deletions src/bs-moment/locale/locale.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ export interface LocaleData {
week?: { dow: number, doy: number };

dayOfMonthOrdinalParse?: RegExp;
ordinal?: (num: number) => string;
postformat?: (num: string) => string;
meridiemParse?: RegExp;

ordinal?(num: number, token?: string): string;
postformat?(num: string): string;
}

export class Locale {
Expand All @@ -57,7 +59,7 @@ export class Locale {
}
}

set (config: LocaleData): void {
set(config: LocaleData): void {
for (const i in config) {
if (!config.hasOwnProperty(i)) {
continue;
Expand Down Expand Up @@ -99,7 +101,7 @@ export class Locale {
return (this._monthsShort as string[])[getMonth(date)];
}
let key = MONTHS_IN_FORMAT.test(format) ? 'format' : 'standalone';
return ((this._monthsShort as any)[key] as string[])[getMonth(date)];
return ((this._monthsShort as any)[key] as string[])[getMonth(date)];
}

// Days of week
Expand Down Expand Up @@ -141,6 +143,14 @@ export class Locale {
return this._week.doy;
}

meridiem(hours: number, minutes: number, isLower: boolean): string {
if (hours > 11) {
return isLower ? 'pm' : 'PM';
}

return isLower ? 'am' : 'AM';
}

ordinal(num: number, token?: string): string {
return this._ordinal.replace('%d', num.toString(10));
}
Expand Down
4 changes: 3 additions & 1 deletion src/bs-moment/locale/locale.defaults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export const defaultLocaleWeek = {
doy : 6 // The week that contains Jan 1st is the first week of the year.
};

export const defaultLocaleMeridiemParse = /[ap]\.?m?\.?/i;

export const baseConfig: LocaleData = {
// calendar: defaultCalendar,
// longDateFormat: defaultLongDateFormat,
Expand All @@ -28,5 +30,5 @@ export const baseConfig: LocaleData = {
weekdaysMin: defaultLocaleWeekdaysMin,
weekdaysShort: defaultLocaleWeekdaysShort,

// meridiemParse: defaultLocaleMeridiemParse
meridiemParse: defaultLocaleMeridiemParse
};
Loading