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

(@fluent/react) Add changeLocales and currentLocales on provider #501

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

macabeus
Copy link
Contributor

@macabeus macabeus commented Jul 8, 2020

Closes this issue: #500

This PR aims to add on useLocalization a function to change the current locale, changeLocales. So on a nested component will can to updates the locale. It's useful for a "choice language" selector.

In addition to returning changeLocales on useLocalization, I also added to return currentLocales, in order to avoid two sources of truth. So a "choice language" selector will need just to read the current language from the provider.

I also updated the example adding the component "choice language".

Comment on lines +6 to +7
changeLocales: (_changeLocales: string[]) => undefined as void,
currentLocales: [] as string[],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since we have a setter and a reader, maybe we could call they as locales and setLocales?

Comment on lines +35 to +36
props.changeLocales(locales);
setLocales(locales);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There aren't any check if the prop.changeLocales was succeeded before to call setLocales. Maybe we could return a bool on changeLocales? Or use await props.changeLocales(locales) in order to fail on except?

@@ -5,9 +5,9 @@ import { ReactLocalization } from "./localization";
/*
* The `useLocalization` hook returns the FluentContext
*/
type useLocalization = () => { l10n: ReactLocalization }
type useLocalization = () => { l10n: ReactLocalization, changeLocales: (locales: string[]) => void, currentLocales: string[] }
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'm writing the type (locales: string[]) => void 2~3 times. Maybe we could to name this type and export/import from somewhere?

@macabeus macabeus marked this pull request as draft July 8, 2020 01:02
@macabeus macabeus marked this pull request as ready for review July 8, 2020 01:03
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.

1 participant