Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/src/packages/frontend/app/i18n-banner.tsx
Views: 687
/*1* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.2* License: MS-RSL – see LICENSE.md for details3*/45import { Button } from "antd";6import { useIntl } from "react-intl";78import {9I18N_MESSAGE,10I18N_TITLE,11I18NSelector,12} from "@cocalc/frontend/account/i18n-selector";13import {14CSS,15React,16redux,17useAsyncEffect,18useMemo,19useState,20useTypedRedux,21} from "@cocalc/frontend/app-framework";22import { useLocalizationCtx } from "@cocalc/frontend/app/localize";23import {24CloseX2,25HelpIcon,26HiddenXSSM,27Icon,28Text,29} from "@cocalc/frontend/components";30import { SiteName } from "@cocalc/frontend/customize";31import {32DEFAULT_LOCALE,33OTHER_SETTINGS_LOCALE_KEY,34} from "@cocalc/frontend/i18n";35import { once } from "@cocalc/util/async-utils";36import { KEEP_EN_LOCALE } from "@cocalc/util/consts/locale";37import { COLORS } from "@cocalc/util/theme";3839// no need to translate this message, since it only shows up when there is no locale set40export const I18N_HINT_ACCOUNT_SETTINGS = `You can also change the language in your "Account" settings.`;4142const I18N_BANNER_STYLE: CSS = {43width: "100%",44padding: "5px",45borderBottom: `1px solid ${COLORS.GRAY_D}`,46background: COLORS.BS_GREEN_LL,47} as const;4849export function useShowI18NBanner() {50const other_settings = useTypedRedux("account", "other_settings");51const i18n = other_settings?.get(OTHER_SETTINGS_LOCALE_KEY);5253return useMemo(() => {54// we show the banner, if the default locale is set and the browser langauge is not english55// user's can dismiss this, which sets the locale to "en-keep".56if (i18n === DEFAULT_LOCALE) {57if (!navigator.language.toLowerCase().startsWith("en")) {58return true;59}60}61}, [i18n]);62}6364export const I18NBanner: React.FC<{}> = () => {65const intl = useIntl();66const { setLocale } = useLocalizationCtx();6768const [loaded, setLoaded] = useState<boolean>(false);6970// wait until the account settings are loaded to show the banner71useAsyncEffect(async () => {72const store = redux.getStore("account");73if (!store.get("is_ready")) {74await once(store, "is_ready");75}76setLoaded(true);77}, []);7879function keep_english() {80redux81.getActions("account")82.set_other_settings(OTHER_SETTINGS_LOCALE_KEY, KEEP_EN_LOCALE);83setLocale(KEEP_EN_LOCALE);84}8586if (!loaded) return;8788return (89<div style={I18N_BANNER_STYLE}>90<Text strong>91<Icon name={"translation-outlined"} /> Use <SiteName /> in a different92language:93</Text>{" "}94<I18NSelector size="small" confirm={true} />{" "}95<Button size="small" type="primary" onClick={keep_english}>96Keep English97</Button>{" "}98<Text type="secondary">99<HiddenXSSM>{I18N_HINT_ACCOUNT_SETTINGS}</HiddenXSSM>{" "}100<HelpIcon title={intl.formatMessage(I18N_TITLE)}>101{intl.formatMessage(I18N_MESSAGE)}102</HelpIcon>{" "}103</Text>104<CloseX2 close={keep_english} />105</div>106);107};108109110