mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
feat: improve i18n support as a whole (#1526)
* feat: improve i18n support as a whole - Remove dayjs in favor of /helpers/datetime.ts, which uses Intl.DateTimeFormat and Date. Dayjs is not exactly i18n friendly and has several locale related opened issues. - Move/refactor date/time code from /helpers/utils.ts to /helpers/datetime.ts. - Fix Daily Review weekday not changing according to selected date. - Localize Daily review weekday and month. - Load i18n listed strings from /locales/{locale}.json in a dynamic way. This makes much easier to add new locales, by just adding a properly named json file and listing it only in /web/src/i18n.ts and /api/user_setting.go. - Fallback languages are now set in /web/src/i18n.ts. - Full language codes are now preffered, but they fallback to 2-letter codes when not available. - The locale dropdown is now populated dynamically from the available locales. Locale names are populated by the browser via Intl.DisplayNames(locale). - /web/src/i18n.ts now exports a type TLocale from availableLocales array. This is used only by findNearestLanguageMatch(). As I was unable to use this type in ".d.ts" files, I switched the Locale type from /web/src/types/i18n.d.ts to string. - Move pretty much all hardcoded text strings to i18n strings. - Add pt-BR translation. - Remove site.ts and move its content to a i18n string. - Rename zh.json to zh-Hans.json to get the correct language name on selector dropdown. - Remove pt_BR.json and replace with pt-BR.json. - Some minor layout spacing fixes to accommodate larger texts. - Improve some error messages. * Delete .yarnrc.yml * Delete package-lock.json * fix: 158:28 error Insert `⏎` prettier/prettier
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import dayjs from "dayjs";
|
||||
import { getRelativeTimeString } from "@/helpers/datetime";
|
||||
import { memo, useEffect, useRef, useState } from "react";
|
||||
import { toast } from "react-hot-toast";
|
||||
import { useTranslation } from "react-i18next";
|
||||
@ -19,14 +19,6 @@ interface Props {
|
||||
readonly?: boolean;
|
||||
}
|
||||
|
||||
export const getFormatedMemoTimeStr = (time: number, locale = "en"): string => {
|
||||
if (Date.now() - time < 1000 * 60 * 60 * 24) {
|
||||
return dayjs(time).locale(locale).fromNow();
|
||||
} else {
|
||||
return dayjs(time).locale(locale).format("YYYY/MM/DD HH:mm:ss");
|
||||
}
|
||||
};
|
||||
|
||||
const Memo: React.FC<Props> = (props: Props) => {
|
||||
const { memo, readonly } = props;
|
||||
const { t, i18n } = useTranslation();
|
||||
@ -35,7 +27,7 @@ const Memo: React.FC<Props> = (props: Props) => {
|
||||
const filterStore = useFilterStore();
|
||||
const userStore = useUserStore();
|
||||
const memoStore = useMemoStore();
|
||||
const [createdTimeStr, setCreatedTimeStr] = useState<string>(getFormatedMemoTimeStr(memo.createdTs, i18n.language));
|
||||
const [createdTimeStr, setCreatedTimeStr] = useState<string>(getRelativeTimeString(memo.createdTs));
|
||||
const memoContainerRef = useRef<HTMLDivElement>(null);
|
||||
const isVisitorMode = userStore.isVisitorMode() || readonly;
|
||||
|
||||
@ -43,7 +35,7 @@ const Memo: React.FC<Props> = (props: Props) => {
|
||||
let intervalFlag: any = -1;
|
||||
if (Date.now() - memo.createdTs < 1000 * 60 * 60 * 24) {
|
||||
intervalFlag = setInterval(() => {
|
||||
setCreatedTimeStr(getFormatedMemoTimeStr(memo.createdTs, i18n.language));
|
||||
setCreatedTimeStr(getRelativeTimeString(memo.createdTs));
|
||||
}, 1000 * 1);
|
||||
}
|
||||
|
||||
@ -90,8 +82,8 @@ const Memo: React.FC<Props> = (props: Props) => {
|
||||
|
||||
const handleDeleteMemoClick = async () => {
|
||||
showCommonDialog({
|
||||
title: "Delete memo",
|
||||
content: "Are you sure to delete this memo?",
|
||||
title: t("memo.delete-memo"),
|
||||
content: t("memo.delete-confirm"),
|
||||
style: "warning",
|
||||
dialogName: "delete-memo-dialog",
|
||||
onConfirm: async () => {
|
||||
|
Reference in New Issue
Block a user