fix: parsing history dates

This commit is contained in:
ThetaDev 2025-01-18 05:51:41 +01:00
parent ff91fc0f72
commit b78adfcbc1
10 changed files with 2084 additions and 429 deletions

View file

@ -0,0 +1,59 @@
const fs = require("fs");
const DICT_PATH = "../dictionary.json";
function translateLang(lang) {
switch (lang) {
case "iw": // Hebrew
return "he";
case "zh-CN": // Simplified Chinese
return "zh-Hans";
case "zh-HK":
return "zh-Hant-HK";
case "zh-TW":
return "zh-Hant";
default:
return lang;
}
}
function isMonthBeforeDay(lang) {
const cldrLang = translateLang(lang);
const dates = require(`cldr-dates-modern/main/${cldrLang}/ca-gregorian.json`);
const dateFields = dates.main[cldrLang].dates.calendars.gregorian;
const dateFmt = dateFields.dateFormats.short;
const mPos = dateFmt.indexOf("M");
const dPos = dateFmt.indexOf("d");
if (mPos < 0 || dPos < 0) throw new Error(`invalid fmt for ${lang}: ${dateFmt}`);
return dPos > mPos;
}
const dict = JSON.parse(fs.readFileSync(DICT_PATH));
for (const [mainLang, entry] of Object.entries(dict)) {
const langs = [mainLang, ...entry["equivalent"]];
const dateOrder = entry["date_order"];
const mPos = dateOrder.indexOf("M");
const dPos = dateOrder.indexOf("D");
let expectMbd = mPos < 0 || dPos < 0 ? null : dPos > mPos;
if (mainLang === "en" || mainLang.startsWith("zh-")) {
expectMbd = true;
} else if (mainLang === "fr")
expectMbd = false;
else {
for (lang of langs) {
const mbd = isMonthBeforeDay(lang)
if (expectMbd === null) {
expectMbd = mbd;
} else if (mbd !== expectMbd) {
throw new Error(`unexpected mbd for ${lang}`);
}
}
}
dict[mainLang]["month_before_day"] = expectMbd;
}
fs.writeFileSync(DICT_PATH, JSON.stringify(dict, null, 2));

View file

@ -17,27 +17,46 @@ function translateLang(lang) {
}
}
function collectMonthNames(lang, by_char, monthNames, weekdayNames) {
function collectMonthNames(lang, by_char, monthNames, monthShortNames, weekdayNames) {
const cldrLang = translateLang(lang);
const dates = require(`cldr-dates-modern/main/${cldrLang}/ca-gregorian.json`);
const dateFields = dates.main[cldrLang].dates.calendars.gregorian;
const months = dateFields.months["stand-alone"].wide;
for (const [n, name] of Object.entries(months)) {
let name2 = name.toLowerCase();
if (name2.includes(n)) {
// Some languages dont have named months
console.log(`${lang}: month name '${name2}' includes number; skipped`);
continue;
// Mongolian dates have the extra numbe арван and have to be handled manually
if (!["mn"].includes(lang)) {
for (const [n, name] of Object.entries(months)) {
let name2 = name.toLowerCase();
if (name2.includes(n)) {
// Some languages dont have named months
console.log(`${lang}: month name '${name2}' includes number; skipped`);
continue;
}
if (/\s/g.test(name2)) {
throw new Error(`${lang}: month name '${name2}' contains whitespace`);
}
monthNames[name2] = parseInt(n);
}
if (lang === "mn") {
name2 = name2.replace(" сар", "").replace("арван ", "");
}
if (!["bg", "fi", "cs", "iw", "lt", "pt-PT", "sk"].includes(lang)) {
const monthsShort = dateFields.months.format.abbreviated;
for (const [n, name] of Object.entries(monthsShort)) {
let name2 = name.toLowerCase().replaceAll(".", "");
if (name2.includes(n)) {
// Some languages dont have named months
console.log(`${lang}: month name '${name2}' includes number; skipped`);
continue;
}
if (lang === "ca") {
name2 = name2.replace("de ", "");
}
if (/\s/g.test(name2)) {
throw new Error(`${lang}: month name '${name2}' contains whitespace`);
}
monthShortNames[name2] = parseInt(n);
}
if (/\s/g.test(name2)) {
throw new Error(`${lang}: month name '${name2}' contains whitespace`);
}
monthNames[name2.toLowerCase()] = parseInt(n);
}
const weekdays = dateFields.days["stand-alone"].wide;
@ -72,12 +91,23 @@ const dict = JSON.parse(fs.readFileSync(DICT_PATH));
for (const [mainLang, entry] of Object.entries(dict)) {
const langs = [mainLang, ...entry["equivalent"]];
let monthNames = {};
let monthShortNames = {};
let weekdayNames = {};
for (lang of langs) {
collectMonthNames(lang, entry["by_char"], monthNames, weekdayNames);
collectMonthNames(
lang,
entry["by_char"],
monthNames,
monthShortNames,
weekdayNames
);
}
dict[mainLang]["months"] = { ...dict[mainLang]["months"], ...monthNames };
dict[mainLang]["months"] = {
...dict[mainLang]["months"],
...monthNames,
...monthShortNames,
};
dict[mainLang]["timeago_nd_tokens"] = {
...dict[mainLang]["timeago_nd_tokens"],
...weekdayNames,