Skip to content

Commit 4407277

Browse files
authored
Merge pull request #7875 from sagemathinc/i18n-20240924
I18n: util, project state, vbar, ...
2 parents 11e90f5 + 5d4ba2e commit 4407277

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+3355
-351
lines changed

src/packages/frontend/account/actions.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { fromJS } from "immutable";
77
import { join } from "path";
8-
import { once } from "@cocalc/util/async-utils";
8+
99
import { alert_message } from "@cocalc/frontend/alerts";
1010
import { AccountClient } from "@cocalc/frontend/client/account";
1111
import { appBasePath } from "@cocalc/frontend/customize/app-base-path";
@@ -14,6 +14,7 @@ import { track_conversion } from "@cocalc/frontend/misc";
1414
import { deleteRememberMe } from "@cocalc/frontend/misc/remember-me";
1515
import track from "@cocalc/frontend/user-tracking";
1616
import { webapp_client } from "@cocalc/frontend/webapp-client";
17+
import { once } from "@cocalc/util/async-utils";
1718
import { define, required } from "@cocalc/util/fill";
1819
import { encode_path } from "@cocalc/util/misc";
1920
import { Actions } from "@cocalc/util/redux/Actions";

src/packages/frontend/account/other-settings.tsx

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,13 +381,21 @@ export function OtherSettings(props: Readonly<Props>): JSX.Element {
381381

382382
function render_vertical_fixed_bar_options(): Rendered {
383383
const selected = getValidVBAROption(props.other_settings.get(VBAR_KEY));
384+
const options = Object.fromEntries(
385+
Object.entries(VBAR_OPTIONS).map(([k, v]) => [k, intl.formatMessage(v)]),
386+
);
384387
return (
385-
<LabeledRow label="Vertical Project Bar">
388+
<LabeledRow
389+
label={intl.formatMessage({
390+
id: "account.other-settings.vbar.title",
391+
defaultMessage: "Vertical Project Bar",
392+
})}
393+
>
386394
<div>
387395
<SelectorInput
388396
style={{ marginBottom: "10px" }}
389397
selected={selected}
390-
options={VBAR_OPTIONS}
398+
options={options}
391399
on_change={(value) => {
392400
on_change(VBAR_KEY, value);
393401
track("flyout", { aspect: "layout", how: "account", value });
@@ -397,7 +405,7 @@ export function OtherSettings(props: Readonly<Props>): JSX.Element {
397405
type="secondary"
398406
ellipsis={{ expandable: true, symbol: "more" }}
399407
>
400-
{VBAR_EXPLANATION}
408+
{intl.formatMessage(VBAR_EXPLANATION)}
401409
</Paragraph>
402410
</div>
403411
</LabeledRow>

src/packages/frontend/account/store.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55

66
import { List, Map } from "immutable";
77
import { reduce } from "lodash";
8-
import { Store } from "@cocalc/util/redux/Store";
8+
99
import { store as customizeStore } from "@cocalc/frontend/customize";
1010
import { make_valid_name } from "@cocalc/util/misc";
11+
import { Store } from "@cocalc/util/redux/Store";
1112
import { get_total_upgrades } from "@cocalc/util/upgrades";
1213
import { AccountState } from "./types";
1314

src/packages/frontend/app/i18n-banner.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import {
1515
CSS,
1616
React,
1717
redux,
18+
useAsyncEffect,
1819
useMemo,
20+
useState,
1921
useTypedRedux,
2022
} from "@cocalc/frontend/app-framework";
2123
import { useLocalizationCtx } from "@cocalc/frontend/app/localize";
@@ -31,6 +33,7 @@ import {
3133
DEFAULT_LOCALE,
3234
OTHER_SETTINGS_LOCALE_KEY,
3335
} from "@cocalc/frontend/i18n";
36+
import { once } from "@cocalc/util/async-utils";
3437
import { KEEP_EN_LOCALE } from "@cocalc/util/consts/locale";
3538
import { COLORS } from "@cocalc/util/theme";
3639

@@ -63,13 +66,26 @@ export const I18NBanner: React.FC<{}> = () => {
6366
const intl = useIntl();
6467
const { setLocale } = useLocalizationCtx();
6568

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+
6680
function keep_english() {
6781
redux
6882
.getActions("account")
6983
.set_other_settings(OTHER_SETTINGS_LOCALE_KEY, KEEP_EN_LOCALE);
7084
setLocale(KEEP_EN_LOCALE);
7185
}
7286

87+
if (!loaded) return;
88+
7389
return (
7490
<div style={I18N_BANNER_STYLE}>
7591
<Text strong>

src/packages/frontend/app/localize.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ function loadAntdLocale(locale: Locale): Promise<AntdLocale> {
158158
return import("antd/locale/tr_TR");
159159
case "he":
160160
return import("antd/locale/he_IL");
161+
case "hi":
162+
return import("antd/locale/hi_IN");
163+
case "hu":
164+
return import("antd/locale/hu_HU");
165+
case "ar":
166+
return import("antd/locale/ar_EG");
161167
default:
162168
unreachable(locale);
163169
throw new Error(`Unknown locale '${locale}.`);

src/packages/frontend/codemirror/extensions/ai-formula.tsx

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
useState,
1010
useTypedRedux,
1111
} from "@cocalc/frontend/app-framework";
12+
import { Localize, useLocalizationCtx } from "@cocalc/frontend/app/localize";
1213
import type { Message } from "@cocalc/frontend/client/types";
1314
import {
1415
HelpIcon,
@@ -27,6 +28,7 @@ import track from "@cocalc/frontend/user-tracking";
2728
import { webapp_client } from "@cocalc/frontend/webapp-client";
2829
import { isFreeModel } from "@cocalc/util/db-schema/llm-utils";
2930
import { unreachable } from "@cocalc/util/misc";
31+
import { Locale } from "@cocalc/util/i18n";
3032

3133
type Mode = "tex" | "md";
3234

@@ -36,23 +38,34 @@ interface Opts {
3638
mode: Mode;
3739
text?: string;
3840
project_id: string;
41+
locale?: Locale;
3942
}
4043

4144
export async function ai_gen_formula({
4245
mode,
4346
text = "",
4447
project_id,
48+
locale,
4549
}: Opts): Promise<string> {
4650
return await show_react_modal((cb) => (
47-
<AiGenFormula mode={mode} text={text} project_id={project_id} cb={cb} />
51+
<Localize>
52+
<AiGenFormula
53+
mode={mode}
54+
text={text}
55+
project_id={project_id}
56+
locale={locale}
57+
cb={cb}
58+
/>
59+
</Localize>
4860
));
4961
}
5062

5163
interface Props extends Opts {
5264
cb: (err?: string, result?: string) => void;
5365
}
5466

55-
function AiGenFormula({ mode, text = "", project_id, cb }: Props) {
67+
function AiGenFormula({ mode, text = "", project_id, locale, cb }: Props) {
68+
const { setLocale } = useLocalizationCtx();
5669
const is_cocalc_com = useTypedRedux("customize", "is_cocalc_com");
5770
const [model, setModel] = useLanguageModelSetting(project_id);
5871
const [input, setInput] = useState<string>(text);
@@ -62,6 +75,12 @@ function AiGenFormula({ mode, text = "", project_id, cb }: Props) {
6275
const [error, setError] = useState<string | undefined>(undefined);
6376
const [tokens, setTokens] = useState<number>(0);
6477

78+
useEffect(() => {
79+
if (typeof locale === "string") {
80+
setLocale(locale);
81+
}
82+
}, [locale]);
83+
6584
useAsyncEffect(
6685
debounce(
6786
async () => {

src/packages/frontend/codemirror/extensions/edit-selection.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55

66
import * as CodeMirror from "codemirror";
77

8+
import { redux } from "@cocalc/frontend/app-framework";
89
import {
910
commands as EDIT_COMMANDS,
1011
FONT_FACES,
1112
} from "@cocalc/frontend/editors/editor-button-bar";
13+
import { getLocale } from "@cocalc/frontend/i18n";
1214
import { markdown_to_html } from "@cocalc/frontend/markdown";
1315
import { open_new_tab, sagews_canonical_mode } from "@cocalc/frontend/misc";
1416
import { defaults, required, startswith } from "@cocalc/util/misc";
@@ -369,7 +371,15 @@ CodeMirror.defineExtension(
369371

370372
case "ai_formula":
371373
if (project_id != null) {
372-
src = await ai_gen_formula({ mode, text: src, project_id });
374+
const account_store = redux.getStore("account");
375+
const locale = getLocale(account_store.get("other_settings"));
376+
377+
src = await ai_gen_formula({
378+
mode,
379+
text: src,
380+
project_id,
381+
locale,
382+
});
373383
}
374384
done = true;
375385
break;

src/packages/frontend/components/project-state.tsx

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
* License: MS-RSL – see LICENSE.md for details
44
*/
55

6-
import { React, useTypedRedux } from "../app-framework";
6+
import { useIntl } from "react-intl";
7+
8+
import { React, useTypedRedux } from "@cocalc/frontend/app-framework";
9+
import { IntlMessage, isIntlMessage } from "@cocalc/frontend/i18n";
10+
import { ProjectStatus } from "@cocalc/frontend/todo-types";
11+
import { ComputeState } from "@cocalc/util/compute-states";
12+
import { KUCALC_COCALC_COM } from "@cocalc/util/db-schema/site-defaults";
713
import { COMPUTE_STATES } from "@cocalc/util/schema";
8-
import { ProjectStatus } from "../todo-types";
914
import { Gap } from "./gap";
1015
import { Icon } from "./icon";
11-
import { KUCALC_COCALC_COM } from "@cocalc/util/db-schema/site-defaults";
1216

1317
interface Props {
1418
state?: ProjectStatus;
@@ -18,6 +22,7 @@ interface Props {
1822
export const ProjectState: React.FC<Props> = (props: Props) => {
1923
const { state, show_desc } = props;
2024

25+
const intl = useIntl();
2126
const kucalc = useTypedRedux("customize", "kucalc");
2227
const showCoCalcCom = kucalc === KUCALC_COCALC_COM;
2328

@@ -29,27 +34,37 @@ export const ProjectState: React.FC<Props> = (props: Props) => {
2934
);
3035
}
3136

32-
function renderDescription({ desc, desc_cocalccom }) {
37+
function renderI18N(msg: string | IntlMessage): string {
38+
if (isIntlMessage(msg)) {
39+
return intl.formatMessage(msg);
40+
} else {
41+
return msg;
42+
}
43+
}
44+
45+
function renderDescription({ desc_cocalccom, desc }: ComputeState) {
3346
if (!show_desc) {
3447
return;
3548
}
3649
const text =
3750
showCoCalcCom && desc_cocalccom != null ? desc_cocalccom : desc;
51+
3852
return (
3953
<span>
40-
<span style={{ fontSize: "11pt" }}>{text}</span>
54+
<span style={{ fontSize: "11pt" }}>{renderI18N(text)}</span>
4155
</span>
4256
);
4357
}
4458

45-
const s = COMPUTE_STATES[state?.get("state") ?? ""];
59+
const current_state = state?.get("state") ?? "";
60+
const s: ComputeState = COMPUTE_STATES[current_state];
4661
if (s == null) {
4762
return <></>;
4863
}
4964
const { display, icon, stable } = s;
5065
return (
5166
<span>
52-
<Icon name={icon} /> {display}
67+
<Icon name={icon} /> {renderI18N(display)}
5368
<Gap />
5469
{!stable && renderSpinner()}
5570
{renderDescription(s)}

src/packages/frontend/editors/slate/format/commands.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
* License: MS-RSL – see LICENSE.md for details
44
*/
55

6+
import { delay } from "awaiting";
67
import { isEqual } from "lodash";
8+
9+
import { redux } from "@cocalc/frontend/app-framework";
10+
import { commands } from "@cocalc/frontend/editors/editor-button-bar";
11+
import { getLocale } from "@cocalc/frontend/i18n";
12+
import { is_array, startswith } from "@cocalc/util/misc";
713
import {
814
BaseRange,
915
Editor,
@@ -15,8 +21,6 @@ import {
1521
Text,
1622
Transforms,
1723
} from "slate";
18-
import { commands } from "@cocalc/frontend/editors/editor-button-bar";
19-
import { is_array, startswith } from "@cocalc/util/misc";
2024
import { getMarks } from "../edit-bar/marks";
2125
import { SlateEditor } from "../editable-markdown";
2226
import { markdown_to_slate } from "../markdown-to-slate";
@@ -27,7 +31,6 @@ import { insertAIFormula } from "./insert-ai-formula";
2731
import { insertImage } from "./insert-image";
2832
import { insertLink } from "./insert-link";
2933
import { insertSpecialChar } from "./insert-special-char";
30-
import { delay } from "awaiting";
3134

3235
// currentWord:
3336
//
@@ -424,7 +427,9 @@ export async function formatAction(
424427

425428
if (cmd === "ai_formula") {
426429
if (project_id == null) throw new Error("ai_formula requires project_id");
427-
const formula = await insertAIFormula(project_id);
430+
const account_store = redux.getStore("account")
431+
const locale = getLocale(account_store.get("other_settings"))
432+
const formula = await insertAIFormula(project_id, locale);
428433
const value = removeDollars(removeBlankLines(formula.trim()));
429434
const node: Node = {
430435
type: "math_inline",

src/packages/frontend/editors/slate/format/insert-ai-formula.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@
55

66
import { alert_message } from "@cocalc/frontend/alerts";
77
import { ai_gen_formula } from "@cocalc/frontend/codemirror/extensions/ai-formula";
8+
import { Locale } from "@cocalc/util/i18n";
89

9-
export async function insertAIFormula(project_id: string): Promise<string> {
10+
export async function insertAIFormula(
11+
project_id: string,
12+
locale: Locale,
13+
): Promise<string> {
1014
try {
11-
return await ai_gen_formula({ mode: "md", project_id });
15+
return await ai_gen_formula({ mode: "md", project_id, locale });
1216
} catch (err) {
1317
alert_message({ type: "error", message: err.errorFields[0]?.errors });
1418
return "";

0 commit comments

Comments
 (0)