mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-04 05:28:59 +01:00 
			
		
		
		
	fix(client): normalize locale when formatting time (closes #7444)
This commit is contained in:
		
							parent
							
								
									797741c7d0
								
							
						
					
					
						commit
						9e1e300c72
					
				@ -1,8 +1,8 @@
 | 
				
			|||||||
import { describe, expect, it } from "vitest";
 | 
					import { describe, expect, it } from "vitest";
 | 
				
			||||||
import options from "../services/options";
 | 
					import options from "../services/options";
 | 
				
			||||||
import { formatDateTime } from "./formatters";
 | 
					import { formatDateTime, normalizeLocale } from "./formatters";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
describe("formatDateTime", () => {
 | 
					describe("formatters", () => {
 | 
				
			||||||
    it("tolerates incorrect locale", () => {
 | 
					    it("tolerates incorrect locale", () => {
 | 
				
			||||||
        options.set("formattingLocale", "cn_TW");
 | 
					        options.set("formattingLocale", "cn_TW");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -10,4 +10,10 @@ describe("formatDateTime", () => {
 | 
				
			|||||||
        expect(formatDateTime(new Date(), "full", "none")).toBeTruthy();
 | 
					        expect(formatDateTime(new Date(), "full", "none")).toBeTruthy();
 | 
				
			||||||
        expect(formatDateTime(new Date(), "none", "full")).toBeTruthy();
 | 
					        expect(formatDateTime(new Date(), "none", "full")).toBeTruthy();
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    it("normalizes locale", () => {
 | 
				
			||||||
 | 
					        expect(normalizeLocale("zh_CN")).toBe("zh-CN");
 | 
				
			||||||
 | 
					        expect(normalizeLocale("cn")).toBe("zh-CN");
 | 
				
			||||||
 | 
					        expect(normalizeLocale("tw")).toBe("zh-TW");
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
				
			|||||||
@ -10,7 +10,7 @@ export function formatDateTime(date: string | Date | number | null | undefined,
 | 
				
			|||||||
        return "";
 | 
					        return "";
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const locale = options.get("formattingLocale") || options.get("locale") || navigator.language;
 | 
					    const locale = normalizeLocale(options.get("formattingLocale") || options.get("locale") || navigator.language);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let parsedDate;
 | 
					    let parsedDate;
 | 
				
			||||||
    if (typeof date === "string" || typeof date === "number") {
 | 
					    if (typeof date === "string" || typeof date === "number") {
 | 
				
			||||||
@ -51,3 +51,12 @@ export function formatDateTime(date: string | Date | number | null | undefined,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    throw new Error("Incorrect state.");
 | 
					    throw new Error("Incorrect state.");
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export function normalizeLocale(locale: string) {
 | 
				
			||||||
 | 
					    locale = locale.replaceAll("_", "-");
 | 
				
			||||||
 | 
					    switch (locale) {
 | 
				
			||||||
 | 
					        case "cn": return "zh-CN";
 | 
				
			||||||
 | 
					        case "tw": return "zh-TW";
 | 
				
			||||||
 | 
					        default: return locale;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user