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

feat: enhance formatMessage from useTranslation with direct string support #4121

Closed
wants to merge 1 commit into from

Conversation

tujoworker
Copy link
Member

This PR adds support for formatting a custom string arguments:

const { myGroup, formatMessage } = useTranslation()

formatMessage('myGroup.stringWithArgument', {
  myArg: 'dynamic-value',
})

From before, formatMessage did support dot-notated keys of an object:

formatMessage('myGroup.subString')

Copy link

vercel bot commented Oct 14, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
eufemia ✅ Ready (Inspect) Visit Preview 💬 Add feedback Oct 15, 2024 0:51am

Copy link

codesandbox-ci bot commented Oct 14, 2024

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Copy link
Contributor

@langz langz left a comment

Choose a reason for hiding this comment

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

💯

langz added a commit that referenced this pull request Oct 15, 2024
This PR relies on #4121 as of now.

This lets form components consume the internal translations as well as
extending the translations with your own strings:

```tsx
import { Form } from '@dnb/eufemia/extensions/forms'

const myTranslations = {
  'en-GB': {
    Custom: {
      translation: 'My translation with a {myKey}',
    },
  },
}

function MyComponent() {
  const { Custom, formatMessage } = Form.useTranslation<myTranslations>()

  // Use it directly
  const str1 = formatMessage(Custom.translation, {
    myKey: 'value!',
  })

  // Or use it with a key
  const str2 = formatMessage('Field.errorRequired', {
    myKey: 'value!',
  })

  return <>MyComponent</>
}

<Form.Handler locale="en-GB" translations={myTranslations}>
  <MyComponent />
</Form.Handler>
```

---------

Co-authored-by: Anders <anderslangseth@gmail.com>
@tujoworker tujoworker closed this Oct 15, 2024
@tujoworker tujoworker deleted the feat/get-translation-value branch October 15, 2024 12:50
tujoworker pushed a commit that referenced this pull request Oct 18, 2024
## [10.53.0](v10.52.1...v10.53.0) (2024-10-18)

### 🐛 Bug Fixes

* **Forms:** keep Iterate.Array in sync with the data context and call onChange when `countPath` changes ([#4147](#4147)) ([095ddaa](095ddaa))
* **Forms:** UI alignments of the ChildrenWithAge block ([#4137](#4137)) ([3db7201](3db7201))
* **Upload:** handle file extensions with case insensitivity ([#4138](#4138)) ([fd9d9ba](fd9d9ba))

### ✨ Features

* add `renderMessage` to useTranslation hook for line-break support ([#4125](#4125)) ([5b07bec](5b07bec))
* **Forms:** add `country` prop to Field.PostalCodeAndCity to support other countries than Norway ([#4109](#4109)) ([5095bea](5095bea))
* **Forms:** add `Form.useTranslation` hook ([#4123](#4123)) ([6f40922](6f40922)), closes [#4121](#4121)
* **Forms:** add support for `minItems`/`maxItems` error messages to Field.ArraySelection ([#4120](#4120)) ([29ebe55](29ebe55))
* **Forms:** add support to set a custom `width` to fields and values ([#4142](#4142)) ([07b257f](07b257f))
* **Forms:** add validation to Field.BankAccountNumber ([#4119](#4119)) ([f70ba3d](f70ba3d))
* **Forms:** warn when a field path is declared more than one time ([#4127](#4127)) ([ca15887](ca15887))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants