Skip to content

Commit 23000d0

Browse files
authored
Merge pull request #15 from telerik/local-format-datefields
feat: localized field name based on format specifier
2 parents e8a7444 + a5b6a25 commit 23000d0

File tree

11 files changed

+509
-124
lines changed

11 files changed

+509
-124
lines changed

docs/cldr/api.md

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,55 @@ The names.
8686

8787
If the type is `"dayPeriods"` or `"eras"`, an object is returned. For the other types, the result is `Array`.
8888

89+
### dateFieldName
90+
91+
Returns a localized date field name based on a specific format specifier.
92+
93+
#### dateFieldName Parameters
94+
95+
##### options `Object`
96+
97+
The options that determine the returned date field name.
98+
99+
##### options.type `String`
100+
101+
The type of the date field name.
102+
103+
The supported values are:
104+
* `"era"`
105+
* `"year"`
106+
* `"quarter"`
107+
* `"month"`
108+
* `"week"`
109+
* `"day"`
110+
* `"weekday"`
111+
* `"dayperiod"`
112+
* `"hour"`
113+
* `"minute"`
114+
* `"second"`
115+
* `"zone"`
116+
117+
##### options.nameType `String`
118+
119+
The format name type.
120+
121+
The supported values are:
122+
* `"wide"`
123+
* `"narrow"`
124+
* `"short"`
125+
126+
##### locale `String`
127+
128+
The locale id.
129+
130+
#### dateFieldName Return Value
131+
132+
##### Returns `String`
133+
134+
The date field name.
135+
136+
If an information for the specified date field type is missing, a `undefined` is returned.
137+
89138
### numberSymbols
90139

91140
Returns the number symbols from the specified locale.

docs/cldr/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The following table provides the data formats that are required for number and d
2525
| Currency | `cldr/main/locale/currencies.json` and `cldr/supplemental/currencyData.json` |
2626
| Basic dates | `cldr/main/locale/ca-gregorian.json` |
2727
| Localized timezone | `cldr/main/locale/timeZoneNames.json` |
28+
| Localized date field names | `cldr/main/locale/dateFields.json` |
2829
| Numeric week day formatting | `cldr/supplemental/weekData.json` |
2930

3031
## Getting CLDR Data
@@ -43,6 +44,7 @@ const numbers = require("cldr-data/main/bg/numbers.json");
4344
const timeZoneNames = require("cldr-data/main/bg/timeZoneNames.json");
4445
const calendar = require("cldr-data/main/bg/ca-gregorian.json");
4546
const currencies = require("cldr-data/main/bg/currencies.json");
47+
const currencies = require("cldr-data/main/bg/dateFields.json");
4648
const weekData = require("cldr-data/supplemental/weekData.json");
4749
const currencyData = require("cldr-data/supplemental/currencyData.json");
4850
@@ -53,6 +55,7 @@ load(
5355
numbers,
5456
currencies,
5557
calendar,
58+
dateFields,
5659
timeZoneNames
5760
);
5861

src/cldr.d.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,50 @@ export interface DateFormatNameOptions {
3232
*/
3333
export function dateFormatNames(locale: string, options: DateFormatNameOptions): any;
3434

35+
/**
36+
* Settings for the dateFieldName function.
37+
*/
38+
export interface DateFieldNameOptions {
39+
/**
40+
* Specifies the type of names.
41+
*/
42+
type: 'era' |
43+
'year' |
44+
'quarter' |
45+
'month' |
46+
'week' |
47+
'day' |
48+
'weekday' |
49+
'dayperiod' |
50+
'hour' |
51+
'minute' |
52+
'second' |
53+
'millisecond' |
54+
'zone';
55+
56+
/**
57+
* Specifies the names form.
58+
*/
59+
nameType?: 'wide' | 'narrow' | 'short';
60+
}
61+
62+
/**
63+
* Returns a localized date field name based on a specific format specifier.
64+
*
65+
* @param options The dateFieldName options.
66+
* @param locale The optional locale id. If not specified, the `"en"` locale id is used.
67+
* @returns The localized date field name determined by the dateFieldName options.
68+
*
69+
* @example
70+
* ```
71+
* dateFieldName({ type: 'day' }); //returns 'day';
72+
* dateFieldName({ type: 'day', nameType: 'wide' }); //returns 'day';
73+
* dateFieldName({ type: 'month', nameType: 'short' }); //returns 'mo.';
74+
* dateFieldName({ type: 'month', nameType: 'wide' }); //returns 'month';
75+
* ```
76+
*/
77+
export function dateFieldName(options: DateFieldNameOptions, locale?: string): string;
78+
3579
/**
3680
* Returns the first day index starting from Sunday based on the specified locale.
3781
*

src/cldr.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export { default as load } from './cldr/load';
2+
export { default as dateFieldName } from './cldr/date-field-name';
23
export { default as dateFormatNames } from './cldr/date-format-names';
34
export { cldr, localeInfo } from './cldr/info';
45
export { currencyDisplays, currencyDisplay, currencyFractionOptions, territoryCurrencyCode, localeCurrency } from './cldr/currency';
56
export { default as firstDay } from './cldr/first-day';
6-
export { default as numberSymbols } from './cldr/number-symbols';
7+
export { default as numberSymbols } from './cldr/number-symbols';

src/cldr/date-field-name.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { localeInfo } from './info';
2+
3+
export default function(options, locale = "en") {
4+
const info = localeInfo(locale);
5+
const dateFields = info.calendar.dateFields || {};
6+
const fieldNameInfo = dateFields[options.type] || {};
7+
8+
return fieldNameInfo[options.nameType] || fieldNameInfo['wide'];
9+
}

src/cldr/default-data.js

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,60 @@ const defaultData = {
576576
"1-alt-variant": "CE"
577577
}
578578
}
579+
},
580+
"dateFields": {
581+
"era": {
582+
"wide": "era"
583+
},
584+
"year": {
585+
"wide": 'year',
586+
"short": 'yr.',
587+
"narrow": 'yr.'
588+
},
589+
"quarter": {
590+
"wide": 'quarter',
591+
"short": 'qtr.',
592+
"narrow": 'qtr.'
593+
},
594+
"month": {
595+
"wide": 'month',
596+
"short": 'mo.',
597+
"narrow": 'mo.'
598+
},
599+
"week": {
600+
"wide": 'week',
601+
"short": 'wk.',
602+
"narrow": 'wk.'
603+
},
604+
"day": {
605+
"wide": 'day',
606+
"short": 'day',
607+
"narrow": 'day'
608+
},
609+
"weekday": {
610+
"wide": 'day of the week'
611+
},
612+
"dayperiod": {
613+
"wide": 'AM/PM'
614+
},
615+
"hour": {
616+
"wide": 'hour',
617+
"short": 'hr.',
618+
"narrow": 'hr.'
619+
},
620+
"minute": {
621+
"wide": 'minute',
622+
"short": 'min.',
623+
"narrow": 'min.'
624+
},
625+
"second": {
626+
"wide": 'second',
627+
"short": 'sec.',
628+
"narrow": 'sec.'
629+
},
630+
"zone": {
631+
"wide": 'time zone'
632+
}
579633
}
580634
}
581635
},
@@ -601,4 +655,4 @@ const defaultData = {
601655
}
602656
}
603657
};
604-
export default defaultData;
658+
export default defaultData;

src/cldr/load-dates.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,24 @@ function loadCalendarNames(locale, calendar) {
6767
localeCalendar.eras = getEraNames(calendar.eras);
6868
}
6969

70+
function loadCalendarDateFields(locale, fields) {
71+
const localeCalendar = cldr[locale].calendar;
72+
const dateFields = {};
73+
74+
for (let field in fields) {
75+
const [ fieldName, formatType = 'wide' ] = field.split('-');
76+
const fieldInfo = dateFields[fieldName] || {};
77+
const displayName = fields[field].displayName;
78+
79+
if (!displayName) { continue; }
80+
81+
fieldInfo[formatType] = displayName;
82+
dateFields[fieldName] = fieldInfo;
83+
}
84+
85+
localeCalendar.dateFields = dateFields;
86+
}
87+
7088
function getPredefinedFormat(paths, calendar) {
7189
const result = [];
7290

@@ -108,6 +126,8 @@ export default function loadCalendarInfo(locale, info) {
108126
} else if (field === "calendars" && info[field].gregorian) {
109127
loadCalendarPatterns(locale, info[field].gregorian);
110128
loadCalendarNames(locale, info[field].gregorian);
129+
} else if (field === "fields") {
130+
loadCalendarDateFields(locale, info.fields);
111131
}
112132
}
113-
}
133+
}

src/dates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export { default as formatDate } from './dates/format-date';
22
export { default as parseDate } from './dates/parse-date';
3-
export { default as splitDateFormat } from './dates/split-date-format';
3+
export { default as splitDateFormat } from './dates/split-date-format';

src/dates/format-date.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,4 @@ export default function formatDate(date, format, locale = "en") {
194194

195195
return result;
196196
});
197-
}
197+
}

0 commit comments

Comments
 (0)