From ed49e9ff6633d448e076c758e15697dfd58a4f64 Mon Sep 17 00:00:00 2001 From: thomasrosen Date: Sat, 23 Jan 2021 21:18:18 +0100 Subject: [PATCH] added locale chooser --- src/App.css | 5 +++++ src/App.js | 41 ++++++++++++++++++++++++++++++++++++----- src/l10n.js | 19 +++++++++++++------ 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/src/App.css b/src/App.css index 15312d1..f9cf92c 100644 --- a/src/App.css +++ b/src/App.css @@ -22,6 +22,11 @@ footer{ margin: 10vmin 0 0 0; } +.locale_chooser{ + margin: -8vmin 10vmin 10vmin 10vmin; + text-align: center; +} + .App { text-align: center; diff --git a/src/App.js b/src/App.js index 0b94df4..52554c9 100644 --- a/src/App.js +++ b/src/App.js @@ -10,11 +10,9 @@ import purpleBG from './purpleBG.png' import empty_1x1 from './empty_1x1.png' import 'intl-pluralrules' -import { AppLocalizationProvider } from './l10n.js' +import { AppLocalizationProvider, locales } from './l10n.js' import { withLocalization, Localized } from './Localized.js' -const userLocales = ['de'] || navigator.languages -// const userLocales = navigator.languages const frameSize = 1080 @@ -348,8 +346,41 @@ const AppLocalized = withLocalization(App) function AppWrapper() { - return - + const [userLocales, setUserLocales] = useState(navigator.languages) + const [currentLocale, setCurrentLocale] = useState(null) + + const handleLanguageChange = useCallback(event => { + setUserLocales([event.target.dataset.locale]) + }, [setUserLocales]) + + const handleCurrentLocalesChange = useCallback(currentLocales => { + setCurrentLocale(currentLocales.length > 0 ? currentLocales[0] : '') + }, [setCurrentLocale]) + + return + <> + + +
+ { + Object.entries(locales) + .map(([locale, name]) => { + return + }) + } +
+
} export default withLocalization(AppWrapper) diff --git a/src/l10n.js b/src/l10n.js index a4673ce..090892a 100644 --- a/src/l10n.js +++ b/src/l10n.js @@ -7,10 +7,13 @@ import { ReactLocalization, LocalizationProvider } from '@fluent/react' import { FluentBundle, FluentResource } from '@fluent/bundle' import { negotiateLanguages } from '@fluent/langneg' -const _supportedLocales_ = [ - 'de', - 'en', -] + +export const locales = { + de: 'Deutsch', + en: 'English', +} + +const _supportedLocales_ = Object.keys(locales) const _defaultLocale_ = 'en' @@ -46,16 +49,20 @@ async function createMessagesGenerator(currentLocales) { } } -export function AppLocalizationProvider({ userLocales, children }){ +export function AppLocalizationProvider({ userLocales, children, onLocaleChange }){ const [bundles, setBundles] = useState(getDefaultBundles()) - useEffect(event => { + useEffect(() => { async function loadBundles() { const currentLocales = negotiateLanguages( userLocales, _supportedLocales_, { defaultLocale: _defaultLocale_ } ) + + if (!!onLocaleChange) { + onLocaleChange(currentLocales) + } const generateBundles = await createMessagesGenerator(currentLocales) setBundles( new ReactLocalization(generateBundles()) )