Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

👋 Hi there,

I've been working on extracting argument types from an ICU-formatted message string and wanted to share it here. It would be great if this was supported by this package directly but if there is no interest, this can just exist as a reference if anyone else would like something like this in their projects.

Gist can be found here.

You must be logged in to vote

Replies: 3 comments · 4 replies

Comment options

This is pretty cool! Does this handle escaping properly?

You must be logged in to vote
4 replies
@offirgolan
Comment options

Not atm, I'll look into supporting this

@longlho
Comment options

There also things like rich text & escaping of those token as well. Idk how much u wanna spent on this cause we're also working on MessageFormat v2 from a spec perspective in Unicode org. It's a long way out though

@offirgolan
Comment options

I've mainly been working on this as an experiment so no worries if this can't be used. I've taken a naive stab at escaping which seems to work but there could be other edge cases I'm missing. Rich text works unless there's some oddity I'm not accounting for. I've updated the gist to include that with some more examples.

You can play with it here.

@longlho
Comment options

Nice! Yeah if u wanna look at our parser's test suite and get them working that'd basically it. That's our baseline tests for the rust parser as well

Comment options

@offirgolan this is incredible work -- I'd love to see you continue to refine it and publish it as a package in some form.

You must be logged in to vote
0 replies
Comment options

👋 Hi there, its taken me a while to come back to this but I ended up completely overhauling the original implementation and created a types-only NPM package (icu-message-types).

import type { ICUMessageArguments, ICUMessageTags } from 'icu-message-types';

// Extract argument types
type Args0 = ICUMessageArguments<'Hello, {firstName} {lastName}!'>;
// Result: { firstName: string | number | boolean; lastName: string | number | boolean }

type Args1 = ICUMessageArguments<`{theme, select,
  light {The interface will be bright}
  dark {The interface will be dark}
  other {The interface will use default colors}
}`>;
// Result: { theme: 'light' | 'dark' | ({} & string) | ({} & number) | boolean | null }

// Extract tag names
type Tags = ICUMessageTags<'Click <link>here</link> to continue'>;
// Result: 'link'

Message Arguments

Format TypeScript Type Example
string string | number | boolean | null {name}
number number | `${number}` | null {count, number, ...}
date Date | number | `${number}` | null {date, date, short}
time Date | number | `${number}` | null {time, time, medium}
plural number | `${number}` | null {count, plural, one {...} other {...}}
selectordinal number | `${number}` | null {position, selectordinal, one {#st} other {#th}}
select union | string | number | boolean | null {theme, select, light {...} dark {...} other {...}}

Additional Features

  • Enhanced Value Types: Non-formatted arguments accept string | number | boolean | null for more flexible usage
  • String Number Support: Numeric formats accept both number and template literal `${number}` types
  • Comprehensive Select Matching: Select arguments with other clauses support string, number, boolean, and null
  • Literal Type Transformation: Select keys are intelligently transformed (e.g., '123' becomes '123' | 123, 'true' becomes 'true' | true)
  • Escaped content: Properly handles quoted/escaped text that shouldn't be parsed as arguments
  • Nested messages: Supports complex nested structures
  • Whitespace handling: Automatically strips whitespace, new lines, and tabs for improved parsing
You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.