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,16 +1,7 @@
|
||||
export function convertToMillis(localSetting: LocalSetting) {
|
||||
const hoursToMillis = localSetting.dailyReviewTimeOffset * 60 * 60 * 1000;
|
||||
return hoursToMillis;
|
||||
}
|
||||
|
||||
export const isNullorUndefined = (value: any) => {
|
||||
return value === null || value === undefined;
|
||||
};
|
||||
|
||||
export function getNowTimeStamp(): number {
|
||||
return Date.now();
|
||||
}
|
||||
|
||||
export function getOSVersion(): "Windows" | "MacOS" | "Linux" | "Unknown" {
|
||||
const appVersion = navigator.userAgent;
|
||||
let detectedOS: "Windows" | "MacOS" | "Linux" | "Unknown" = "Unknown";
|
||||
@ -26,63 +17,6 @@ export function getOSVersion(): "Windows" | "MacOS" | "Linux" | "Unknown" {
|
||||
return detectedOS;
|
||||
}
|
||||
|
||||
export function getTimeStampByDate(t: Date | number | string): number {
|
||||
if (typeof t === "string") {
|
||||
t = t.replaceAll("-", "/");
|
||||
}
|
||||
const d = new Date(t);
|
||||
|
||||
return d.getTime();
|
||||
}
|
||||
|
||||
export function getDateStampByDate(t: Date | number | string): number {
|
||||
const d = new Date(getTimeStampByDate(t));
|
||||
|
||||
return new Date(d.getFullYear(), d.getMonth(), d.getDate()).getTime();
|
||||
}
|
||||
|
||||
export function getDateString(t: Date | number | string): string {
|
||||
const d = new Date(getTimeStampByDate(t));
|
||||
|
||||
const year = d.getFullYear();
|
||||
const month = d.getMonth() + 1;
|
||||
const date = d.getDate();
|
||||
|
||||
return `${year}/${month}/${date}`;
|
||||
}
|
||||
|
||||
export function getTimeString(t: Date | number | string): string {
|
||||
const d = new Date(getTimeStampByDate(t));
|
||||
|
||||
const hours = d.getHours();
|
||||
const mins = d.getMinutes();
|
||||
|
||||
const hoursStr = hours < 10 ? "0" + hours : hours;
|
||||
const minsStr = mins < 10 ? "0" + mins : mins;
|
||||
|
||||
return `${hoursStr}:${minsStr}`;
|
||||
}
|
||||
|
||||
// For example: 2021-4-8 17:52:17
|
||||
export function getDateTimeString(t: Date | number | string): string {
|
||||
const d = new Date(getTimeStampByDate(t));
|
||||
|
||||
const year = d.getFullYear();
|
||||
const month = d.getMonth() + 1;
|
||||
const date = d.getDate();
|
||||
const hours = d.getHours();
|
||||
const mins = d.getMinutes();
|
||||
const secs = d.getSeconds();
|
||||
|
||||
const monthStr = month < 10 ? "0" + month : month;
|
||||
const dateStr = date < 10 ? "0" + date : date;
|
||||
const hoursStr = hours < 10 ? "0" + hours : hours;
|
||||
const minsStr = mins < 10 ? "0" + mins : mins;
|
||||
const secsStr = secs < 10 ? "0" + secs : secs;
|
||||
|
||||
return `${year}/${monthStr}/${dateStr} ${hoursStr}:${minsStr}:${secsStr}`;
|
||||
}
|
||||
|
||||
export const getElementBounding = (element: HTMLElement, relativeEl?: HTMLElement) => {
|
||||
const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
|
||||
const scrollLeft = window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
|
||||
@ -162,3 +96,12 @@ export function convertFileToBase64(file: File): Promise<string> {
|
||||
reader.onerror = (error) => reject(error);
|
||||
});
|
||||
}
|
||||
|
||||
export const formatBytes = (bytes: number) => {
|
||||
if (bytes <= 0) return "0 Bytes";
|
||||
const k = 1024,
|
||||
dm = 2,
|
||||
sizes = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"],
|
||||
i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
|
||||
};
|
||||
|
Reference in New Issue
Block a user