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

Add ICU prettifier #173

Merged
merged 6 commits into from
Oct 24, 2022
Merged

Add ICU prettifier #173

merged 6 commits into from
Oct 24, 2022

Conversation

samhh
Copy link
Member

@samhh samhh commented Oct 23, 2022

Closes #129. For now the text content must be supplied as per the example instead of via stdin; a change here could be covered by #110.

$ cat x.json
{
  "x": {
    "message": "{hasTags, boolean, true {{type, select, overLimit {{upperLimit, number}+ best free {formattedListOfTags} photos on Unsplash} belowLimit {{photoTotal, number} best free {formattedListOfTags} photos on Unsplash}}} false {{type, select, overLimit {{upperLimit, number}+ best free photos on Unsplash} belowLimit {{photoTotal, number} best free photos on Unsplash}}}} {another, select, a {{foo}} b {bar}}"
  }
}
$ intlc prettify $(cat x.json | jq -r .x.message)
{hasTags, boolean, 
	true {{type, select, 
		overLimit {{upperLimit, number}+ best free {formattedListOfTags} photos on Unsplash}
		belowLimit {{photoTotal, number} best free {formattedListOfTags} photos on Unsplash}
	}}
	false {{type, select, 
		overLimit {{upperLimit, number}+ best free photos on Unsplash}
		belowLimit {{photoTotal, number} best free photos on Unsplash}
	}}
} {another, select, 
	a {{foo}}
	b {bar}
}

There isn't really a satisfying way to format ICU messages across newlines. In this example the space between the hasTags and another interpolations needs to be preserved exactly as-is. Equally formatting in callbacks would be ambiguous, whereas the newlines and indentation inserted here aren't because it's within the syntactic structure of the interpolations.

I think this is okay because as per the feature request, and the part of the example I borrowed from there, we'll mostly be using this for interpolation-heavy, flattened messages.

In terms of implementation, the bulk of the change is refactoring the ICU compiler to be a little more abstract. Following that the change to support formatting is straightforward and centralised, essentially just using Reader local to increment a count according to how deeply nested into "cases" in the AST we are and inserting formatting accordingly.

@samhh samhh requested a review from a team as a code owner October 23, 2022 15:59
@samhh samhh requested a review from OliverJAsh October 23, 2022 15:59
Copy link
Member

@OliverJAsh OliverJAsh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This will be useful. Should we add this to the docs?

@samhh samhh merged commit 6c591ac into master Oct 24, 2022
@samhh samhh deleted the prettify branch October 24, 2022 11:18
@samhh samhh mentioned this pull request Oct 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ICU formatter
2 participants