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

Create versions of makeT and makeBasicT that only error #45

Open
robyoder opened this issue Mar 11, 2025 · 0 comments
Open

Create versions of makeT and makeBasicT that only error #45

robyoder opened this issue Mar 11, 2025 · 0 comments

Comments

@robyoder
Copy link
Collaborator

We use React Context in B5X to hold onto T and its localized messages. Context objects themselves have to be created outside the React component tree, but if they're used with useContext and haven't been mounted in the component tree as a Provider, the caller will only get the initial value passed to createContext. So a common pattern is to pass createContext functions that throw errors so that the caller quickly realizes they're missing a crucial step. Another option is passing something like undefined, but then every caller has to check for that every time.

This bit us recently in B5X where there were two locale providers and we tried to useContext with the wrong one. Because we called createContext with makeT() or equivalent, we had a working T, but that made it a lot harder to realize what the problem was, since it never got its messages loaded and would fall back to basic string interpolation.

What I'd like to do is have something like const LocaleContext = createContext(makeErrorT("LocaleProvider does not exist in your component tree.")) so that any use of T(), T.$(), etc. errors with that message. I could create these functions myself, but that feels like a little too much coupling to t-i18n internals, and they seem generally useful for other projects too.

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

No branches or pull requests

1 participant