-
Notifications
You must be signed in to change notification settings - Fork 78
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from "react"; | ||
import { useLocalization } from "@fluent/react"; | ||
import { AVAILABLE_LOCALES } from "./l10n"; | ||
|
||
export function LanguageSelector() { | ||
const { changeLocales, currentLocales } = useLocalization() | ||
|
||
return ( | ||
<select | ||
onChange={event => changeLocales([event.target.value])} | ||
value={currentLocales[0]}> | ||
{Object.entries(AVAILABLE_LOCALES).map( | ||
([code, name]) => <option key={code} value={code}>{name}</option> | ||
)} | ||
</select> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
import { createContext } from "react"; | ||
import { ReactLocalization } from "./localization"; | ||
|
||
export let FluentContext = createContext(new ReactLocalization([], null)); | ||
const defaultValue = { | ||
l10n: new ReactLocalization([], null), | ||
changeLocales: (_changeLocales: string[]) => undefined as void, | ||
currentLocales: [] as string[], | ||
}; | ||
|
||
export let FluentContext = createContext(defaultValue); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import { createElement, ReactNode, ReactElement } from "react"; | ||
import { createElement, ReactNode, ReactElement, useState } from "react"; | ||
import PropTypes from "prop-types"; | ||
import { FluentContext } from "./context"; | ||
import { ReactLocalization } from "./localization"; | ||
|
||
interface LocalizationProviderProps { | ||
children?: ReactNode; | ||
l10n: ReactLocalization; | ||
changeLocales: (locales: string[]) => void; | ||
initialLocales: string[]; | ||
} | ||
|
||
/* | ||
|
@@ -27,10 +29,21 @@ interface LocalizationProviderProps { | |
export function LocalizationProvider( | ||
props: LocalizationProviderProps | ||
): ReactElement { | ||
let [locales, setLocales] = useState(props.initialLocales); | ||
|
||
function changeLocales(locales: string[]) { | ||
props.changeLocales(locales); | ||
setLocales(locales); | ||
Comment on lines
+35
to
+36
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There aren't any check if the |
||
} | ||
|
||
return createElement( | ||
FluentContext.Provider, | ||
{ | ||
value: props.l10n | ||
value: { | ||
l10n: props.l10n, | ||
changeLocales: changeLocales, | ||
currentLocales: locales | ||
} | ||
}, | ||
props.children | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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[] } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm writing the type |
||
export const useLocalization: useLocalization = () => { | ||
const l10n = useContext(FluentContext); | ||
const { l10n, changeLocales, currentLocales } = useContext(FluentContext); | ||
|
||
return { l10n }; | ||
return { l10n, changeLocales, currentLocales }; | ||
}; |
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.
Since we have a setter and a reader, maybe we could call they as
locales
andsetLocales
?