-
Notifications
You must be signed in to change notification settings - Fork 1.9k
refactor(web): timezone select options display with i18n support #2497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,60 @@ | ||
| export const TIMEZONE_OPTIONS = [ | ||
| { value: "America/Los_Angeles", label: "America/Los_Angeles (UTC-8)" }, | ||
| { value: "America/Denver", label: "America/Denver (UTC-7)" }, | ||
| { value: "America/Chicago", label: "America/Chicago (UTC-6)" }, | ||
| { value: "America/New_York", label: "America/New_York (UTC-5)" }, | ||
| { value: "America/Toronto", label: "America/Toronto (UTC-5)" }, | ||
| { value: "UTC", label: "UTC" }, | ||
| { value: "Europe/London", label: "Europe/London (UTC+0)" }, | ||
| { value: "Europe/Paris", label: "Europe/Paris (UTC+1)" }, | ||
| { value: "Europe/Berlin", label: "Europe/Berlin (UTC+1)" }, | ||
| { value: "Europe/Moscow", label: "Europe/Moscow (UTC+3)" }, | ||
| { value: "Asia/Dubai", label: "Asia/Dubai (UTC+4)" }, | ||
| { value: "Asia/Shanghai", label: "Asia/Shanghai (UTC+8)" }, | ||
| { value: "Asia/Hong_Kong", label: "Asia/Hong_Kong (UTC+8)" }, | ||
| { value: "Asia/Singapore", label: "Asia/Singapore (UTC+8)" }, | ||
| { value: "Asia/Tokyo", label: "Asia/Tokyo (UTC+9)" }, | ||
| { value: "Asia/Seoul", label: "Asia/Seoul (UTC+9)" }, | ||
| { value: "Australia/Sydney", label: "Australia/Sydney (UTC+10)" }, | ||
| { value: "Australia/Melbourne", label: "Australia/Melbourne (UTC+10)" }, | ||
| { value: "Pacific/Auckland", label: "Pacific/Auckland (UTC+12)" }, | ||
| ]; | ||
| import { getTimeZones } from "@vvo/tzdb"; | ||
|
|
||
| const TIMEZONE_ID_SET = new Set([ | ||
| "America/Los_Angeles", | ||
| "America/Denver", | ||
| "America/Chicago", | ||
| "America/New_York", | ||
| "America/Toronto", | ||
| "UTC", | ||
| "Europe/London", | ||
| "Europe/Paris", | ||
| "Europe/Berlin", | ||
| "Europe/Moscow", | ||
| "Asia/Dubai", | ||
| "Asia/Shanghai", | ||
| "Asia/Hong_Kong", | ||
| "Asia/Singapore", | ||
| "Asia/Tokyo", | ||
| "Asia/Seoul", | ||
| "Australia/Sydney", | ||
| "Australia/Melbourne", | ||
| "Pacific/Auckland", | ||
| ]); | ||
|
|
||
| export interface TimezoneOption { | ||
| value: string; // for timezone id | ||
| label: string; // for display text | ||
| } | ||
|
|
||
| function getLocalizedName(tzName: string, lang: string): string { | ||
| const locale = { zh: "zh-CN", en: "en", ru: "ru", ja: "ja" }[lang] || "en"; | ||
| try { | ||
| const parts = new Intl.DateTimeFormat(locale, { | ||
| timeZone: tzName, | ||
| timeZoneName: "long", | ||
| }).formatToParts(new Date()); | ||
| return parts.find((p) => p.type === "timeZoneName")?.value || tzName; | ||
| } catch { | ||
| return tzName; | ||
| } | ||
| } | ||
|
|
||
| export function getTimezoneOptions(lang: string = "en"): TimezoneOption[] { | ||
| return getTimeZones({ includeUtc: true }) | ||
| .filter( | ||
| (tz) => | ||
| TIMEZONE_ID_SET.has(tz.name) || | ||
| (tz.name === "Etc/UTC" && TIMEZONE_ID_SET.has("UTC")), | ||
| ) | ||
| .sort((a, b) => a.currentTimeOffsetInMinutes - b.currentTimeOffsetInMinutes) | ||
| .map((tz) => { | ||
| const value = tz.name === "Etc/UTC" ? "UTC" : tz.name; | ||
| return { | ||
| value, | ||
| label: `${getLocalizedName(tz.name, lang)} (${ | ||
| tz.currentTimeFormat.split(" ")[0] | ||
| }, ${value})`, | ||
|
Comment on lines
+50
to
+57
|
||
| }; | ||
| }); | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,8 @@ | ||||||||||||||||||||
| import { useMemo } from "react"; | ||||||||||||||||||||
| import { useTranslation } from "react-i18next"; | ||||||||||||||||||||
| import { getTimezoneOptions, type TimezoneOption } from "../constants/timezone"; | ||||||||||||||||||||
|
|
||||||||||||||||||||
| export function useTimezoneOptions(): TimezoneOption[] { | ||||||||||||||||||||
| const { i18n } = useTranslation(); | ||||||||||||||||||||
| return useMemo(() => getTimezoneOptions(i18n.language), [i18n.language]); | ||||||||||||||||||||
|
||||||||||||||||||||
| return useMemo(() => getTimezoneOptions(i18n.language), [i18n.language]); | |
| const language = i18n.resolvedLanguage ?? i18n.language; | |
| return useMemo( | |
| () => { | |
| const locale = (language ?? "en").split("-")[0]; | |
| return getTimezoneOptions(locale); | |
| }, | |
| [language], | |
| ); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| import { Form, InputNumber, Select, Card, Alert } from "@agentscope-ai/design"; | ||
| import { useTranslation } from "react-i18next"; | ||
| import { TIMEZONE_OPTIONS } from "../../../../constants/timezone"; | ||
| import { useTimezoneOptions } from "../../../../hooks/useTimezoneOptions"; | ||
| import styles from "../index.module.less"; | ||
|
|
||
| const LANGUAGE_OPTIONS = [ | ||
|
|
@@ -63,7 +63,7 @@ export function ReactAgentCard({ | |
| .toLowerCase() | ||
| .includes(input.toLowerCase()) | ||
| } | ||
| options={TIMEZONE_OPTIONS} | ||
| options={useTimezoneOptions()} | ||
| onChange={onTimezoneChange} | ||
|
Comment on lines
63
to
67
|
||
| loading={savingTimezone} | ||
| disabled={savingTimezone} | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,7 +12,8 @@ import { TimePicker } from "antd"; | |
| import { useTranslation } from "react-i18next"; | ||
| import type { FormInstance } from "antd"; | ||
| import type { CronJobSpecOutput } from "../../../../api/types"; | ||
| import { TIMEZONE_OPTIONS, DEFAULT_FORM_VALUES } from "./constants"; | ||
| import { DEFAULT_FORM_VALUES } from "./constants"; | ||
| import { useTimezoneOptions } from "../../../../hooks/useTimezoneOptions"; | ||
| import styles from "../index.module.less"; | ||
|
|
||
| type CronJob = CronJobSpecOutput; | ||
|
|
@@ -230,7 +231,7 @@ export function JobDrawer({ | |
| .toLowerCase() | ||
| .includes(input.toLowerCase()) | ||
| } | ||
| options={TIMEZONE_OPTIONS} | ||
| options={useTimezoneOptions()} | ||
| /> | ||
|
Comment on lines
231
to
235
|
||
| </Form.Item> | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| export { createColumns } from "./columns"; | ||
| export { JobDrawer } from "./JobDrawer"; | ||
| export { useCronJobs } from "../useCronJobs"; | ||
| export { TIMEZONE_OPTIONS, DEFAULT_FORM_VALUES } from "./constants"; | ||
| export { DEFAULT_FORM_VALUES } from "./constants"; |
Uh oh!
There was an error while loading. Please reload this page.