-
Notifications
You must be signed in to change notification settings - Fork 5
Enable type-safe internationalization (and translation key autocompletability) #199
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
Conversation
@@ -20,7 +17,7 @@ const primaryLinks: { | |||
i18nKey: "nav_link_health", | |||
href: "/health", | |||
}, | |||
]; | |||
] as const; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ℹ️ You can't pass in a string
type to t()
. This makes it so i18nKey
is "nav_link_home" | "nav_link_health"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great. At first I was concerned that this approach doesn't work for content bundles that are stored separately from the source code, but after skimming this article looks like there's a way to handle that.
I'll think on this a bit longer but so far I'm supportive of it.
@@ -122,6 +123,7 @@ npm run test-watch -- pages | |||
|
|||
- [TypeScript](https://www.typescriptlang.org/) is used for type checking. | |||
- `npm run ts:check` - Type checks all files | |||
- `npm run i18n-types` - Updates the i18n TypeScript declaration. You only need to run this if you've added a new English locale file (JSON files in `public/locales/en`). This runs automatically when you start the development server or build the application. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely a "gotcha" but I think since we should be adding new files relatively rarely it's worth it.
some non-blocking thoughts for discussion: thought: out of scope for this PR but i'm wondering how this solution would/wouldn't need to change once we add in external content management systems for managing the translation bundles e.g. like Phrase or Locize or something. another thought: there may/may not be value in an ADR outline the pros/cons of this type-checking approach vs an approach using i18next's missing key error functionality to do a unit test that checks for non-existent keys. i don't feel strongly about it, i feel like the design decision here to rely on static type checking is reasonably "obvious" enough to not require an ADR. final thought: i wonder if there's a way to automatically check for unused keys so that we can help with cleanup of deprecated content keys. |
Ticket
Resolves #194
Changes
i18n-types
NPM script for generating a TypeScript declaration of the JSON English locale file(s)Context for reviewers
I'm interested to get feedback on this proposal. It comes with some tradeoffs...
Pros
Cons
npm run i18n-types
.I think the benefits of type-safe i18n is worth this added potential for confusion, but I'm curious to get people's thoughts on that.
Testing
CleanShot.2023-08-18.at.19.10.49.mp4
Example of the test coverage to help teams identify that the file needs to be updated: