-
Notifications
You must be signed in to change notification settings - Fork 79
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
(@fluent/react) Add useTranslate #475
Conversation
fluent-react/example/src/Hello.tsx
Outdated
</Localized> | ||
<input | ||
type="text" | ||
placeholder={tAttributes('type-name').placeholder} |
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.
I don't know if adding a function tAttributes
is the best way, but I think that it's enough.
At the first moment I tried adding a parameter on t
function to return the attributes (t('type-name'), undefined, { attributes: true })
), but it's very weird and bad to type.
fluent-react/src/useTranslate.ts
Outdated
if (!l10n) { | ||
// Return fallbacks functions | ||
const t: TFunction = () => ""; | ||
const tAttributes: TAttributesFunction = () => ({}); | ||
|
||
return { t, tAttributes }; | ||
} |
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.
I'm returning an empty string and empty object because I'm following the same approach of <Localized />
, but maybe would be better to return the id
.
On the case of tAttributes
, we could use Proxy to return the attribute name. For example:
tAttributes('invalid-key').foo // should return `"foo"`
Thank you for the PR, @macabeus! I've been thinking about adding a hook-based API too, and it's great to see a contribution in this area. I'd like to spend a bit of time discussing the API design and the use-cases, and then I'll be happy to do a code review. CC'ing @Gregoor who re-wrote The API I had in mind was a bit different, and I wonder what you think about it. Rather than return let l10n = useLocalization();
alert(l10n.getString("hello")); Or even: let l10n = useContext(FluentContext);
alert(l10n.getString("hello")); What I like in this approach is that it mostly relies on abstractions already built into React and into I also have a suggestion regarding the In fluent.js/fluent-dom/src/localization.js Lines 215 to 235 in cff95f6
Perhaps we could add a similar method to That said, I might suggest startig with just |
Thank you for commenting on the PR. And yeah, I also want to discuss more about the API =]
I didn't notice about I like the mindset of creating a new feature with few new line of codes, but:
Yeah, adding a new method that returns everything would be nice. Looks like a better approach following the fluent mindset. But I think that the name |
I think That's why I'd suggest to only focus on the
Hmm, that's an interesting point. I think you're right that it might be safer to return a plain object, like so: let {l10n} = useLocalization(); In the future, we might want to add more properties, for example: let {l10n, changeLocales} = useLocalization(); I'd like to think about this a little bit and see what's possible. Let's not block this PR on this; both approaches will be fine :) |
@stasm Okay. I just changed the PR to Also I changed the example to just replace |
49135fe
to
0f58ae6
Compare
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.
Thanks for your patience, @macabeus, a few other things kept me busy ealier this week. I think the PR is close to being ready, I just have a few comments which I'd like to see addressed first. Let me know what you think :)
@stasm I made the changes that you proposed. |
e984276
to
6cd2071
Compare
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.
This looks great, thanks! I really like how it ended up being not a lot of code, since we mostly rely on React's Context API. And I like how useLocalization
allows us to control our API. Great work!
@macabeus Let me know when you're ready for getting this merged. I'll also update the docs on the wiki once this is released. Thanks again! |
Co-Authored-By: Staś Małolepszy <stas@duzodobrze.pl>
@stasm It's fine to me to merge =] |
Closes this issue: #467
Add
useTranslate
custom hook, that uses theFluentContext
to return two functions to get the translated messages from Fluent's bundle.t
: get the root message from the given idtAttributes
: get the attributes message from the given idThis PR also updates one example file on
@fluent/react
in order to show how to use that.