From 05ffc2c9df49a55513f2b627e7f8c21cc0cc2f9a Mon Sep 17 00:00:00 2001 From: macabeus Date: Wed, 8 Jul 2020 01:44:52 +0100 Subject: [PATCH] (@fluent/react) Add changeLocales and currentLocales on provider --- fluent-react/example/src/App.tsx | 5 +++++ fluent-react/example/src/LanguageSelector.tsx | 17 +++++++++++++++++ fluent-react/example/src/l10n.tsx | 19 ++++--------------- fluent-react/src/context.ts | 8 +++++++- fluent-react/src/localized.ts | 2 +- fluent-react/src/provider.ts | 17 +++++++++++++++-- fluent-react/src/use_localization.ts | 6 +++--- fluent-react/src/with_localization.ts | 2 +- 8 files changed, 53 insertions(+), 23 deletions(-) create mode 100644 fluent-react/example/src/LanguageSelector.tsx diff --git a/fluent-react/example/src/App.tsx b/fluent-react/example/src/App.tsx index 0e6f9e18f..5517914de 100644 --- a/fluent-react/example/src/App.tsx +++ b/fluent-react/example/src/App.tsx @@ -3,6 +3,7 @@ import { Localized } from "@fluent/react"; import { FluentDateTime } from "@fluent/bundle"; import { Hello } from "./Hello"; import { SignIn } from "./SignIn"; +import { LanguageSelector } from "./LanguageSelector"; export function App() { let [date] = useState(() => new Date()); @@ -37,5 +38,9 @@ export function App() { + +
+ + ; } diff --git a/fluent-react/example/src/LanguageSelector.tsx b/fluent-react/example/src/LanguageSelector.tsx new file mode 100644 index 000000000..ff626a39a --- /dev/null +++ b/fluent-react/example/src/LanguageSelector.tsx @@ -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 ( + + ); +} diff --git a/fluent-react/example/src/l10n.tsx b/fluent-react/example/src/l10n.tsx index 16e1172dd..74fcae3f7 100644 --- a/fluent-react/example/src/l10n.tsx +++ b/fluent-react/example/src/l10n.tsx @@ -8,7 +8,7 @@ import { ReactLocalization, LocalizationProvider } from "@fluent/react"; const ftl = require("../public/*.ftl"); const DEFAULT_LOCALE = "en-US"; -const AVAILABLE_LOCALES = { +export const AVAILABLE_LOCALES = { "en-US": "English", "pl": "Polish", }; @@ -33,7 +33,6 @@ interface AppLocalizationProviderProps { } export function AppLocalizationProvider(props: AppLocalizationProviderProps) { - let [currentLocales, setCurrentLocales] = useState([DEFAULT_LOCALE]); let [l10n, setL10n] = useState(null); useEffect(() => { @@ -46,7 +45,6 @@ export function AppLocalizationProvider(props: AppLocalizationProviderProps) { Object.keys(AVAILABLE_LOCALES), { defaultLocale: DEFAULT_LOCALE } ); - setCurrentLocales(currentLocales); let fetchedMessages = await Promise.all( currentLocales.map(fetchMessages) @@ -60,18 +58,9 @@ export function AppLocalizationProvider(props: AppLocalizationProviderProps) { return
Loading…
; } - return <> - + return ( + {Children.only(props.children)} - -
- - ; + ); } diff --git a/fluent-react/src/context.ts b/fluent-react/src/context.ts index 3af5e51cf..0ebd0c27d 100644 --- a/fluent-react/src/context.ts +++ b/fluent-react/src/context.ts @@ -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); diff --git a/fluent-react/src/localized.ts b/fluent-react/src/localized.ts index 33dfda189..cb75706ad 100644 --- a/fluent-react/src/localized.ts +++ b/fluent-react/src/localized.ts @@ -47,7 +47,7 @@ export interface LocalizedProps { */ export function Localized(props: LocalizedProps): ReactElement { const { id, attrs, vars, elems, children: child = null } = props; - const l10n = useContext(FluentContext); + const { l10n } = useContext(FluentContext); // Validate that the child element isn't an array if (Array.isArray(child)) { diff --git a/fluent-react/src/provider.ts b/fluent-react/src/provider.ts index 206d7cfb5..547ac1b20 100644 --- a/fluent-react/src/provider.ts +++ b/fluent-react/src/provider.ts @@ -1,4 +1,4 @@ -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"; @@ -6,6 +6,8 @@ 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); + } + return createElement( FluentContext.Provider, { - value: props.l10n + value: { + l10n: props.l10n, + changeLocales: changeLocales, + currentLocales: locales + } }, props.children ); diff --git a/fluent-react/src/use_localization.ts b/fluent-react/src/use_localization.ts index 72fe89db3..d31b0fd32 100644 --- a/fluent-react/src/use_localization.ts +++ b/fluent-react/src/use_localization.ts @@ -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[] } export const useLocalization: useLocalization = () => { - const l10n = useContext(FluentContext); + const { l10n, changeLocales, currentLocales } = useContext(FluentContext); - return { l10n }; + return { l10n, changeLocales, currentLocales }; }; diff --git a/fluent-react/src/with_localization.ts b/fluent-react/src/with_localization.ts index 31c0e6b16..c7bbf3433 100644 --- a/fluent-react/src/with_localization.ts +++ b/fluent-react/src/with_localization.ts @@ -16,7 +16,7 @@ export function withLocalization

( Inner: ComponentType

): ComponentType> { function WithLocalization(props: WithoutLocalizationProps

): ReactElement { - const l10n = useContext(FluentContext); + const { l10n } = useContext(FluentContext); // Re-bind getString to trigger a re-render of Inner. const getString = l10n.getString.bind(l10n); return createElement(Inner, { getString, ...props } as P);