Skip to content

Commit 06f195c

Browse files
authored
(@fluent/bundle) Document NUMBER, DATETIME (#469)
1 parent d768f7c commit 06f195c

File tree

2 files changed

+83
-12
lines changed

2 files changed

+83
-12
lines changed

fluent-bundle/src/builtins.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,34 @@ const NUMBER_ALLOWED = [
4242
"maximumSignificantDigits",
4343
];
4444

45+
/**
46+
* The implementation of the `NUMBER()` builtin available to translations.
47+
*
48+
* Translations may call the `NUMBER()` builtin in order to specify formatting
49+
* options of a number. For example:
50+
*
51+
* pi = The value of π is {NUMBER($pi, maximumFractionDigits: 2)}.
52+
*
53+
* The implementation expects an array of `FluentValues` representing the
54+
* positional arguments, and an object of named `FluentValues` representing the
55+
* named parameters.
56+
*
57+
* The following options are recognized:
58+
*
59+
* unitDisplay
60+
* currencyDisplay
61+
* useGrouping
62+
* minimumIntegerDigits
63+
* minimumFractionDigits
64+
* maximumFractionDigits
65+
* minimumSignificantDigits
66+
* maximumSignificantDigits
67+
*
68+
* Other options are ignored.
69+
*
70+
* @param args The positional arguments passed to this `NUMBER()`.
71+
* @param opts The named argments passed to this `NUMBER()`.
72+
*/
4573
export function NUMBER(
4674
args: Array<FluentValue>,
4775
opts: Record<string, FluentValue>
@@ -79,6 +107,40 @@ const DATETIME_ALLOWED = [
79107
"timeZoneName",
80108
];
81109

110+
/**
111+
* The implementation of the `DATETIME()` builtin available to translations.
112+
*
113+
* Translations may call the `DATETIME()` builtin in order to specify
114+
* formatting options of a number. For example:
115+
*
116+
* now = It's {DATETIME($today, month: "long")}.
117+
*
118+
* The implementation expects an array of `FluentValues` representing the
119+
* positional arguments, and an object of named `FluentValues` representing the
120+
* named parameters.
121+
*
122+
* The following options are recognized:
123+
*
124+
* dateStyle
125+
* timeStyle
126+
* fractionalSecondDigits
127+
* dayPeriod
128+
* hour12
129+
* weekday
130+
* era
131+
* year
132+
* month
133+
* day
134+
* hour
135+
* minute
136+
* second
137+
* timeZoneName
138+
*
139+
* Other options are ignored.
140+
*
141+
* @param args The positional arguments passed to this `DATETIME()`.
142+
* @param opts The named argments passed to this `DATETIME()`.
143+
*/
82144
export function DATETIME(
83145
args: Array<FluentValue>,
84146
opts: Record<string, FluentValue>

fluent-bundle/src/types.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ export type FluentFunction = (
1616
* them, which can then be used in the `toString` method together with a proper
1717
* `Intl` formatter.
1818
*/
19-
export class FluentType<T> {
19+
export abstract class FluentType<T> {
2020
/** The wrapped native value. */
2121
public value: T;
2222

2323
/**
2424
* Create a `FluentType` instance.
2525
*
26-
* @param value - JavaScript value to wrap.
26+
* @param value The JavaScript value to wrap.
2727
*/
2828
constructor(value: T) {
2929
this.value = value;
@@ -42,12 +42,8 @@ export class FluentType<T> {
4242
* Formatted values are suitable for use outside of the `FluentBundle`.
4343
* This method can use `Intl` formatters available through the `scope`
4444
* argument.
45-
*
46-
* @abstract
4745
*/
48-
toString(scope: Scope): string {
49-
throw new Error("Subclasses of FluentType must implement toString.");
50-
}
46+
abstract toString(scope: Scope): string;
5147
}
5248

5349
/**
@@ -56,7 +52,7 @@ export class FluentType<T> {
5652
export class FluentNone extends FluentType<string> {
5753
/**
5854
* Create an instance of `FluentNone` with an optional fallback value.
59-
* @param value - The fallback value of this `FluentNone`.
55+
* @param value The fallback value of this `FluentNone`.
6056
*/
6157
constructor(value = "???") {
6258
super(value);
@@ -72,14 +68,21 @@ export class FluentNone extends FluentType<string> {
7268

7369
/**
7470
* A `FluentType` representing a number.
71+
*
72+
* A `FluentNumber` instance stores the number value of the number it
73+
* represents. It may also store an option bag of options which will be passed
74+
* to `Intl.NumerFormat` when the `FluentNumber` is formatted to a string.
7575
*/
7676
export class FluentNumber extends FluentType<number> {
77-
/** Options passed to Intl.NumberFormat. */
77+
/** Options passed to `Intl.NumberFormat`. */
7878
public opts: Intl.NumberFormatOptions;
7979

8080
/**
8181
* Create an instance of `FluentNumber` with options to the
8282
* `Intl.NumberFormat` constructor.
83+
*
84+
* @param value The number value of this `FluentNumber`.
85+
* @param opts Options which will be passed to `Intl.NumberFormat`.
8386
*/
8487
constructor(value: number, opts: Intl.NumberFormatOptions = {}) {
8588
super(value);
@@ -102,16 +105,22 @@ export class FluentNumber extends FluentType<number> {
102105

103106
/**
104107
* A `FluentType` representing a date and time.
108+
*
109+
* A `FluentDateTime` instance stores the number value of the date it
110+
* represents, as a numerical timestamp in milliseconds. It may also store an
111+
* option bag of options which will be passed to `Intl.DateTimeFormat` when the
112+
* `FluentDateTime` is formatted to a string.
105113
*/
106114
export class FluentDateTime extends FluentType<number> {
107-
/** Options passed to Intl.DateTimeFormat. */
115+
/** Options passed to `Intl.DateTimeFormat`. */
108116
public opts: Intl.DateTimeFormatOptions;
109117

110118
/**
111119
* Create an instance of `FluentDateTime` with options to the
112120
* `Intl.DateTimeFormat` constructor.
113-
* @param value - timestamp in milliseconds
114-
* @param opts
121+
*
122+
* @param value The number value of this `FluentDateTime`, in milliseconds.
123+
* @param opts Options which will be passed to `Intl.DateTimeFormat`.
115124
*/
116125
constructor(value: number, opts: Intl.DateTimeFormatOptions = {}) {
117126
super(value);

0 commit comments

Comments
 (0)