-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #189 from unicode-org/third-formatToParts
(third) Implement formatToParts
- Loading branch information
Showing
10 changed files
with
236 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import {Message} from "../impl/model.js"; | ||
import {FormattingContext, formatToParts, OpaquePart, RuntimeValue} from "../impl/runtime.js"; | ||
|
||
// We want to pass it into the translation and get it back out unformatted, in | ||
// the correct position in the sentence, via formatToParts. | ||
class SomeUnstringifiableClass {} | ||
|
||
// TODO(stasm): This is generic enough that it could be in impl/runtime.ts. | ||
class WrappedValue extends RuntimeValue<SomeUnstringifiableClass> { | ||
formatToString(ctx: FormattingContext): string { | ||
throw new Error("Method not implemented."); | ||
} | ||
*formatToParts(ctx: FormattingContext): IterableIterator<OpaquePart> { | ||
yield {type: "opaque", value: this.value}; | ||
} | ||
} | ||
|
||
console.log("==== English ===="); | ||
|
||
{ | ||
// "Ready? Then {$submitButton}!" | ||
let message: Message = { | ||
lang: "en", | ||
id: "submit", | ||
phrases: {}, | ||
selectors: [ | ||
{ | ||
expr: null, | ||
default: {type: "StringLiteral", value: "default"}, | ||
}, | ||
], | ||
variants: [ | ||
{ | ||
keys: [{type: "StringLiteral", value: "default"}], | ||
value: [ | ||
{type: "StringLiteral", value: "Ready? Then "}, | ||
{ | ||
type: "VariableReference", | ||
name: "submitButton", | ||
}, | ||
{type: "StringLiteral", value: "!"}, | ||
], | ||
}, | ||
], | ||
}; | ||
console.log( | ||
Array.of( | ||
...formatToParts(message, { | ||
submitButton: new WrappedValue(new SomeUnstringifiableClass()), | ||
}) | ||
) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
declare namespace Intl { | ||
interface ListFormatOptions { | ||
// I added `string` to avoid having to validate the exact values of options. | ||
localeMatcher?: string | "best fit" | "lookup"; | ||
type?: string | "conjunction" | "disjunction | unit"; | ||
style?: string | "long" | "short" | "narrow"; | ||
} | ||
|
||
type ListFormatPartTypes = "literal" | "element"; | ||
|
||
interface ListFormatPart { | ||
type: ListFormatPartTypes; | ||
value: string; | ||
} | ||
|
||
interface ListFormat { | ||
format(value?: Array<unknown>): string; | ||
formatToParts(value?: Array<unknown>): ListFormatPart[]; | ||
} | ||
|
||
var ListFormat: { | ||
new (locales?: string | string[], options?: ListFormatOptions): ListFormat; | ||
(locales?: string | string[], options?: ListFormatOptions): ListFormat; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.