UiLangSelect.tsx 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { Button, Dropdown } from "antd";
  2. import type { MenuProps } from "antd";
  3. import { useEffect, useState } from "react";
  4. import { set, get } from "../../locales";
  5. import { GlobalOutlined } from "@ant-design/icons";
  6. interface IUiLang {
  7. key: string;
  8. label: string;
  9. }
  10. const uiLang: IUiLang[] = [
  11. {
  12. key: "en-US",
  13. label: "English",
  14. },
  15. {
  16. key: "zh-Hans",
  17. label: "简体中文",
  18. },
  19. {
  20. key: "zh-Hant",
  21. label: "繁体中文",
  22. },
  23. ];
  24. const UiLangSelectWidget = () => {
  25. const [curr, setCurr] = useState<string>();
  26. const items: MenuProps["items"] = uiLang;
  27. const onClick: MenuProps["onClick"] = ({ key }) => {
  28. set(key, true);
  29. };
  30. useEffect(() => {
  31. const currLang = get();
  32. console.log("lang", currLang);
  33. const find = uiLang.find((item) => item?.key === currLang);
  34. setCurr(find?.label);
  35. }, []);
  36. return (
  37. <Dropdown menu={{ items, onClick }} placement="bottomRight">
  38. <Button ghost style={{ border: "unset" }} icon={<GlobalOutlined />}>
  39. {curr}
  40. </Button>
  41. </Dropdown>
  42. );
  43. };
  44. export default UiLangSelectWidget;