-
Notifications
You must be signed in to change notification settings - Fork 77
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: setL10n in useLocalization? #500
Comments
Thanks for filing the issue! I've been thinking about something similar: |
I'm thinking... And yeah, maybe it could be useful. On the client side, using the example from - <LocalizationProvider l10n={l10n}>
+ <LocalizationProvider l10n={l10n} setL10n={changeLocales}> Then we'll be able to use Just adding the prop Currently Also and most important IMHO... Currently is necessary to write a big boilerplate to use Fluent. What do you think about that, @stasm ? |
Yeah, I was thinking along the same lines. That's why I liked your idea in #475 to return a plain
In general, I'd like the API to be aligned with the That said, I guess the primary use-case of
I think most of that boilerplate is needed and in fact is what makes |
Here's how I imagined export function AppLocalizationProvider(props: AppLocalizationProviderProps) {
let [currentLocales, setCurrentLocales] = useState([DEFAULT_LOCALE]);
let [l10n, setL10n] = useState<ReactLocalization | null>(null);
useEffect(() => {
changeLocales(navigator.languages as Array<string>);
}, []);
async function changeLocales(userLocales: Array<string>) {
// Negotiate user locales × available locales
let currentLocales = …;
setCurrentLocales(currentLocales);
// Fetch translations and instantiate ReactLocalization.
let bundles = await ...;
let l10n = new ReactLocalization(bundles);
setL10n(l10n);
}
return <>
<LocalizationProvider l10n={l10n} changeLocales={changeLocales}>
{Children.only(props.children)}
</LocalizationProvider>
</>;
}
// Somewhere else:
function MyComponent() {
let {l10n, changeLocales} = useLocalization();
changeLocales(["fr-CA", "fr"]);
} |
If we expose async function changeLocales(userLocales: Array<string>) {
// Negotiate user locales × available locales
let currentLocales = …;
// Fetch translations and parse them into FluentBundles.
let bundles = await ...;
let l10n = new ReactLocalization(bundles);
return {currentLocales, l10n};
}
export function AppLocalizationProvider(props: AppLocalizationProviderProps) {
let [currentLocales, setCurrentLocales] = useState([DEFAULT_LOCALE]);
let [l10n, setL10n] = useState<ReactLocalization | null>(null);
useEffect(() => {
let {currentLocales, l10n} = changeLocales(navigator.languages as Array<string>);
setCurrentLocales(currentLocales);
setL10n(l10n);
}, []);
return <>
<LocalizationProvider l10n={l10n} setL10n={setL10n}>
{Children.only(props.children)}
</LocalizationProvider>
</>;
}
// Somewhere else:
function MyComponent() {
let {l10n, setL10n} = useLocalization();
let newL10n = changeLocales(["fr-CA", "fr"]);
setL10n(l10n);
} To solve this, we could expose Or, we could store |
I think that exposing I like to thinking with code, so I'm opening a PR following your first approach. Then I noticed that in order to this new feature be useful to use on a selector, we'll also need to provide a variable saying the current locale ( I think that, since we'll have both a setter and reader, we could call it following the React's PR with the proof of concept: #501 |
What about 2 hooks? 1 low-level ( |
Can we have
setL10n
inuseLocalization
so we can easily set the new value in a nested component?The text was updated successfully, but these errors were encountered: