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

Added formatting to the localization hooks. #251

Merged
merged 2 commits into from
May 6, 2024

Conversation

alexasselin008
Copy link
Member

Localization now follow the ICU Message Format standard. This means we can add complex formatting to the json messages, and they will automatically be converted to a function accepting a variables object.

The messages are built at 3 different times:

  • I added a webpack loader that converts the json file when storybook runs
  • I added a jest resolved that converts the json file when jest runs
  • I added a esbuild/tsup transformer that converts the json file when the code is built.

Example of strings added to en-US.json file

"Button.spinnerAriaLabel": "Loading",
"Input.charactersLeft": "{charLeft, plural, =0 {No characters left} one {# character left} other {# characters left}}."

Converts into:

"Button.spinnerAriaLabel": `Loading`,
"Input.charactersLeft": (args, formatter) => `${formatter.plural(args.charLeft, { "=0": `No characters left`, one: () => `${formatter.number(args.charLeft)} character left`, other: () => `${formatter.number(args.charLeft)} characters left` })}.`

So it can be used like this:

    const stringFormatter = useLocalizedString();
...    
    stringFormatter.format("Button.spinnerAriaLabel") // Loading
...
    stringFormatter.format("Input.charactersLeft", { charLeft: 3 }) // 3 characters left.

Copy link

changeset-bot bot commented Apr 26, 2024

⚠️ No Changeset found

Latest commit: 5c115ab

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

netlify bot commented Apr 26, 2024

Deploy Preview for wl-hopper ready!

Name Link
🔨 Latest commit 5c115ab
🔍 Latest deploy log https://app.netlify.com/sites/wl-hopper/deploys/662c13ac14e7df0008d7d2bf
😎 Deploy Preview https://deploy-preview-251--wl-hopper.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

Copy link
Contributor

View Storybook

@alexasselin008 alexasselin008 merged commit 49f0615 into main May 6, 2024
8 checks passed
@alexasselin008 alexasselin008 deleted the feature/localization_advanced_function branch May 6, 2024 13:44
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.

3 participants