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: allow custom languages in IntlProvider #158

Merged
merged 5 commits into from
Oct 11, 2023
Merged

Conversation

Benehiko
Copy link
Contributor

Adds custom language support to the IntlProvider. This allows users of ory/elements to specify and provide their own custom translations without needing to add it to the ory/elements repository.

Related Issue or Design Document

Checklist

  • I have read the contributing guidelines and signed the CLA.
  • I have referenced an issue containing the design document if my change introduces a new feature.
  • I have read the security policy.
  • I confirm that this pull request does not address a security vulnerability.
    If this pull request addresses a security vulnerability,
    I confirm that I got approval (please contact [email protected]) from the maintainers to push the changes.
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added the necessary documentation within the code base (if appropriate).

Further comments

@Benehiko Benehiko self-assigned this Oct 11, 2023
Copy link
Member

@zepatrik zepatrik left a comment

Choose a reason for hiding this comment

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

This is already possible & documented:

elements/README.md

Lines 359 to 377 in 4bdbac1

Custom translations can be provided using the `IntlProvider` from `react-intl`.
For reference, it is best to start with the auto-generated English defaults, as
they include all keys. More information on the Kratos messages can be found
[in the docs](https://www.ory.sh/docs/kratos/concepts/ui-user-interface#ui-message-codes).
```tsx
import { IntlProvider } from "react-intl"
const Main = () => {
return (
<IntlProvider locale={customMessageLocale} messages={customMessages}>
<Router>
<Route path="/" component={Dashboard} />
{/* ... */}
</Router>
</IntlProvider>
)
}
```

We could improve the docs to show how the default translations can be extended instead:

import { IntlProvider } from "react-intl"
import { locales } from "@ory/elements"

const customMessages = {
  ...locales,
  de: {
    ...locales.de,
    "login.title": "Login",
  },
}

const Main = () => {
  return (
    <IntlProvider locale={customMessageLocale} messages={customMessages}>
      <Router>
        <Route path="/" component={Dashboard} />
        {/* ... */}
      </Router>
    </IntlProvider>
  )
}

@Benehiko
Copy link
Contributor Author

This is already possible & documented:

elements/README.md

Lines 359 to 377 in 4bdbac1

Custom translations can be provided using the `IntlProvider` from `react-intl`.
For reference, it is best to start with the auto-generated English defaults, as
they include all keys. More information on the Kratos messages can be found
[in the docs](https://www.ory.sh/docs/kratos/concepts/ui-user-interface#ui-message-codes).
```tsx
import { IntlProvider } from "react-intl"
const Main = () => {
return (
<IntlProvider locale={customMessageLocale} messages={customMessages}>
<Router>
<Route path="/" component={Dashboard} />
{/* ... */}
</Router>
</IntlProvider>
)
}
```

We could improve the docs to show how the default translations can be extended instead:

import { IntlProvider } from "react-intl"
import { locales } from "@ory/elements"

const customMessages = {
  ...locales,
  de: {
    ...locales.de,
    "login.title": "Login",
  },
}

const Main = () => {
  return (
    <IntlProvider locale={customMessageLocale} messages={customMessages}>
      <Router>
        <Route path="/" component={Dashboard} />
        {/* ... */}
      </Router>
    </IntlProvider>
  )
}

I agree that we do support it, but it requires the user implementing the library to directly use react-intl.

In an optimal path scenario I can see more users wanting to only use a copy paste example with defaults and the option to override some labels, such as the login ID -> Email for example.

The path to reach this use case in the current method isn't as obvious and a bit confusing, especially if you have no idea which keys do what. What if we introduce breaking changes later on inside the translations? or special formatting like we do here https://github.com/ory/elements/blob/main/src/react-components/provider.tsx#L50-L52

I'm not against the current way and I think supporting it makes sense to give users a choice and the ability to customize the translation layer themselves. But a happy path must exist and i don't think the current implementation is that happy path.

@@ -82,6 +82,91 @@ have explicitly told our React app to use through the `VITE_ORY_SDK_URL` export.
Now you can see Ory Elements in action by opening <http://localhost:3000> in
your browser!

## Internalization (i18n)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the README to be more clear on this and moved it to the top under getting started.

@Benehiko Benehiko requested a review from zepatrik October 11, 2023 09:47
Copy link
Member

@zepatrik zepatrik 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 looks quite a bit more useful now 👍

@Benehiko Benehiko merged commit e4394b8 into main Oct 11, 2023
6 checks passed
@Benehiko Benehiko deleted the custom-languages branch October 11, 2023 10:02
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.

2 participants