mirror of
https://github.com/router-for-me/Cli-Proxy-API-Management-Center.git
synced 2026-02-20 03:30:50 +08:00
fix(format): use page locale by default
This commit is contained in:
@@ -3,6 +3,14 @@
|
|||||||
* 从原项目 src/utils/string.js 迁移
|
* 从原项目 src/utils/string.js 迁移
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
const resolveDefaultLocale = (): string | undefined => {
|
||||||
|
const fromDocument =
|
||||||
|
typeof document !== 'undefined' ? document.documentElement?.lang?.trim() : '';
|
||||||
|
if (fromDocument) return fromDocument;
|
||||||
|
const fromNavigator = typeof navigator !== 'undefined' ? navigator.language?.trim() : '';
|
||||||
|
return fromNavigator || undefined;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 隐藏 API Key 中间部分,仅保留前后两位
|
* 隐藏 API Key 中间部分,仅保留前后两位
|
||||||
*/
|
*/
|
||||||
@@ -38,14 +46,15 @@ export function formatFileSize(bytes: number): string {
|
|||||||
/**
|
/**
|
||||||
* 格式化日期时间
|
* 格式化日期时间
|
||||||
*/
|
*/
|
||||||
export function formatDateTime(date: string | Date): string {
|
export function formatDateTime(date: string | Date, locale?: string): string {
|
||||||
const d = typeof date === 'string' ? new Date(date) : date;
|
const d = typeof date === 'string' ? new Date(date) : date;
|
||||||
|
|
||||||
if (isNaN(d.getTime())) {
|
if (isNaN(d.getTime())) {
|
||||||
return 'Invalid Date';
|
return 'Invalid Date';
|
||||||
}
|
}
|
||||||
|
|
||||||
return d.toLocaleString('zh-CN', {
|
const resolvedLocale = locale?.trim() || resolveDefaultLocale();
|
||||||
|
return d.toLocaleString(resolvedLocale, {
|
||||||
year: 'numeric',
|
year: 'numeric',
|
||||||
month: '2-digit',
|
month: '2-digit',
|
||||||
day: '2-digit',
|
day: '2-digit',
|
||||||
@@ -89,8 +98,9 @@ export function formatUnixTimestamp(value: unknown, locale?: string): string {
|
|||||||
/**
|
/**
|
||||||
* 格式化数字(添加千位分隔符)
|
* 格式化数字(添加千位分隔符)
|
||||||
*/
|
*/
|
||||||
export function formatNumber(num: number): string {
|
export function formatNumber(num: number, locale?: string): string {
|
||||||
return num.toLocaleString('zh-CN');
|
const resolvedLocale = locale?.trim() || resolveDefaultLocale();
|
||||||
|
return num.toLocaleString(resolvedLocale);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user