Add languages to Language menu (#17606)

* Add support for Spanish

* add polish

* add turkish

* fix

* dynamically import non English locales to keep bundle size low
This commit is contained in:
Josh Hawkins 2025-04-11 07:38:36 -05:00 committed by GitHub
parent 78c461750e
commit 03f4318e40
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 114 additions and 12 deletions

View File

@ -108,7 +108,33 @@
"languages": "Languages", "languages": "Languages",
"language": { "language": {
"en": "English", "en": "English",
"es": "Español (Spanish)",
"zhCN": "简体中文 (Simplified Chinese)", "zhCN": "简体中文 (Simplified Chinese)",
"hi": "हिन्दी (Hindi)",
"fr": "Français (French)",
"ar": "العربية (Arabic)",
"pt": "Português (Portuguese)",
"ru": "Русский (Russian)",
"de": "Deutsch (German)",
"ja": "日本語 (Japanese)",
"tr": "Türkçe (Turkish)",
"it": "Italiano (Italian)",
"nl": "Nederlands (Dutch)",
"sv": "Svenska (Swedish)",
"cs": "Čeština (Czech)",
"nb": "Norsk Bokmål (Norwegian Bokmål)",
"ko": "한국어 (Korean)",
"vi": "Tiếng Việt (Vietnamese)",
"fa": "فارسی (Persian)",
"pl": "Polski (Polish)",
"uk": "Українська (Ukrainian)",
"he": "עברית (Hebrew)",
"el": "Ελληνικά (Greek)",
"ro": "Română (Romanian)",
"hu": "Magyar (Hungarian)",
"fi": "Suomi (Finnish)",
"da": "Dansk (Danish)",
"sk": "Slovenčina (Slovak)",
"withSystem": { "withSystem": {
"label": "Use the system settings for language" "label": "Use the system settings for language"
} }

View File

@ -331,6 +331,24 @@ export default function GeneralSettings({ className }: GeneralSettingsProps) {
<span className="ml-6 mr-2">{t("menu.language.en")}</span> <span className="ml-6 mr-2">{t("menu.language.en")}</span>
)} )}
</MenuItem> </MenuItem>
<MenuItem
className={
isDesktop
? "cursor-pointer"
: "flex items-center p-2 text-sm"
}
aria-label={t("menu.language.es")}
onClick={() => setLanguage("es")}
>
{language === "es" ? (
<>
<LuLanguages className="mr-2 size-4" />
{t("menu.language.es")}
</>
) : (
<span className="ml-6 mr-2">{t("menu.language.es")}</span>
)}
</MenuItem>
<MenuItem <MenuItem
className={ className={
isDesktop isDesktop
@ -351,6 +369,24 @@ export default function GeneralSettings({ className }: GeneralSettingsProps) {
</span> </span>
)} )}
</MenuItem> </MenuItem>
<MenuItem
className={
isDesktop
? "cursor-pointer"
: "flex items-center p-2 text-sm"
}
aria-label={t("menu.language.tr")}
onClick={() => setLanguage("tr")}
>
{language === "tr" ? (
<>
<LuLanguages className="mr-2 size-4" />
{t("menu.language.tr")}
</>
) : (
<span className="ml-6 mr-2">{t("menu.language.tr")}</span>
)}
</MenuItem>
</SubItemContent> </SubItemContent>
</Portal> </Portal>
</SubItem> </SubItem>

View File

@ -1,23 +1,43 @@
import * as React from "react"; import { useState, useEffect } from "react";
import { ChevronLeft, ChevronRight } from "lucide-react"; import { ChevronLeft, ChevronRight } from "lucide-react";
import { DayPicker } from "react-day-picker"; import { DayPicker } from "react-day-picker";
import { enUS, Locale, zhCN } from "date-fns/locale"; import { Locale, enUS } from "date-fns/locale";
import { cn } from "@/lib/utils"; import { cn } from "@/lib/utils";
import { buttonVariants } from "@/components/ui/button"; import { buttonVariants } from "@/components/ui/button";
import i18n from "@/utils/i18n"; import i18n from "@/utils/i18n";
export type CalendarProps = React.ComponentProps<typeof DayPicker>; export type CalendarProps = React.ComponentProps<typeof DayPicker>;
// Map of locale codes to dynamic import functions
let locale: Locale; const localeMap: Record<string, () => Promise<Locale>> = {
switch(i18n.language) { "zh-CN": () => import("date-fns/locale/zh-CN").then((module) => module.zhCN),
case "zh-CN": es: () => import("date-fns/locale/es").then((module) => module.es),
locale = zhCN; hi: () => import("date-fns/locale/hi").then((module) => module.hi),
break; fr: () => import("date-fns/locale/fr").then((module) => module.fr),
default: ar: () => import("date-fns/locale/ar").then((module) => module.ar),
locale = enUS; pt: () => import("date-fns/locale/pt").then((module) => module.pt),
break; ru: () => import("date-fns/locale/ru").then((module) => module.ru),
} de: () => import("date-fns/locale/de").then((module) => module.de),
ja: () => import("date-fns/locale/ja").then((module) => module.ja),
tr: () => import("date-fns/locale/tr").then((module) => module.tr),
it: () => import("date-fns/locale/it").then((module) => module.it),
nl: () => import("date-fns/locale/nl").then((module) => module.nl),
sv: () => import("date-fns/locale/sv").then((module) => module.sv),
cs: () => import("date-fns/locale/cs").then((module) => module.cs),
nb: () => import("date-fns/locale/nb").then((module) => module.nb),
ko: () => import("date-fns/locale/ko").then((module) => module.ko),
vi: () => import("date-fns/locale/vi").then((module) => module.vi),
fa: () => import("date-fns/locale/fa-IR").then((module) => module.faIR),
pl: () => import("date-fns/locale/pl").then((module) => module.pl),
uk: () => import("date-fns/locale/uk").then((module) => module.uk),
he: () => import("date-fns/locale/he").then((module) => module.he),
el: () => import("date-fns/locale/el").then((module) => module.el),
ro: () => import("date-fns/locale/ro").then((module) => module.ro),
hu: () => import("date-fns/locale/hu").then((module) => module.hu),
fi: () => import("date-fns/locale/fi").then((module) => module.fi),
da: () => import("date-fns/locale/da").then((module) => module.da),
sk: () => import("date-fns/locale/sk").then((module) => module.sk),
};
function Calendar({ function Calendar({
className, className,
@ -25,6 +45,26 @@ function Calendar({
showOutsideDays = true, showOutsideDays = true,
...props ...props
}: CalendarProps) { }: CalendarProps) {
const [locale, setLocale] = useState<Locale>(enUS);
useEffect(() => {
const loadLocale = async () => {
if (i18n.language === "en") {
setLocale(enUS);
return;
}
const localeLoader = localeMap[i18n.language];
if (localeLoader) {
const loadedLocale = await localeLoader();
setLocale(loadedLocale);
} else {
setLocale(enUS);
}
};
loadLocale();
}, [i18n.language]);
return ( return (
<DayPicker <DayPicker
locale={locale} locale={locale}