CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/app/i18n-banner.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Button } from "antd";
7
import { useIntl } from "react-intl";
8
9
import {
10
I18N_MESSAGE,
11
I18N_TITLE,
12
I18NSelector,
13
} from "@cocalc/frontend/account/i18n-selector";
14
import {
15
CSS,
16
React,
17
redux,
18
useAsyncEffect,
19
useMemo,
20
useState,
21
useTypedRedux,
22
} from "@cocalc/frontend/app-framework";
23
import { useLocalizationCtx } from "@cocalc/frontend/app/localize";
24
import {
25
CloseX2,
26
HelpIcon,
27
HiddenXSSM,
28
Icon,
29
Text,
30
} from "@cocalc/frontend/components";
31
import { SiteName } from "@cocalc/frontend/customize";
32
import {
33
DEFAULT_LOCALE,
34
OTHER_SETTINGS_LOCALE_KEY,
35
} from "@cocalc/frontend/i18n";
36
import { once } from "@cocalc/util/async-utils";
37
import { KEEP_EN_LOCALE } from "@cocalc/util/consts/locale";
38
import { COLORS } from "@cocalc/util/theme";
39
40
// no need to translate this message, since it only shows up when there is no locale set
41
export const I18N_HINT_ACCOUNT_SETTINGS = `You can also change the language in your "Account" settings.`;
42
43
const I18N_BANNER_STYLE: CSS = {
44
width: "100%",
45
padding: "5px",
46
borderBottom: `1px solid ${COLORS.GRAY_D}`,
47
background: COLORS.BS_GREEN_LL,
48
} as const;
49
50
export function useShowI18NBanner() {
51
const other_settings = useTypedRedux("account", "other_settings");
52
const i18n = other_settings?.get(OTHER_SETTINGS_LOCALE_KEY);
53
54
return useMemo(() => {
55
// we show the banner, if the default locale is set and the browser langauge is not english
56
// user's can dismiss this, which sets the locale to "en-keep".
57
if (i18n === DEFAULT_LOCALE) {
58
if (!navigator.language.toLowerCase().startsWith("en")) {
59
return true;
60
}
61
}
62
}, [i18n]);
63
}
64
65
export const I18NBanner: React.FC<{}> = () => {
66
const intl = useIntl();
67
const { setLocale } = useLocalizationCtx();
68
69
const [loaded, setLoaded] = useState<boolean>(false);
70
71
// wait until the account settings are loaded to show the banner
72
useAsyncEffect(async () => {
73
const store = redux.getStore("account");
74
if (!store.get("is_ready")) {
75
await once(store, "is_ready");
76
}
77
setLoaded(true);
78
}, []);
79
80
function keep_english() {
81
redux
82
.getActions("account")
83
.set_other_settings(OTHER_SETTINGS_LOCALE_KEY, KEEP_EN_LOCALE);
84
setLocale(KEEP_EN_LOCALE);
85
}
86
87
if (!loaded) return;
88
89
return (
90
<div style={I18N_BANNER_STYLE}>
91
<Text strong>
92
<Icon name={"translation-outlined"} /> Use <SiteName /> in a different
93
language:
94
</Text>{" "}
95
<I18NSelector size="small" confirm={true} />{" "}
96
<Button size="small" type="primary" onClick={keep_english}>
97
Keep English
98
</Button>{" "}
99
<Text type="secondary">
100
<HiddenXSSM>{I18N_HINT_ACCOUNT_SETTINGS}</HiddenXSSM>{" "}
101
<HelpIcon title={intl.formatMessage(I18N_TITLE)}>
102
{intl.formatMessage(I18N_MESSAGE)}
103
</HelpIcon>{" "}
104
</Text>
105
<CloseX2 close={keep_english} />
106
</div>
107
);
108
};
109
110