convert no-arg TR references to method invocations in *.ts

This commit is contained in:
Damien Elmes 2021-03-26 18:17:08 +10:00
parent 264b9204c0
commit b435658acb
9 changed files with 44 additions and 69 deletions

View File

@ -9,13 +9,13 @@ from re import Match
import stringcase import stringcase
TR_REF = re.compile(r"tr\(\s*TR.([^,) ]+),\s*([^)]+)\)") TR_REF = re.compile(r"i18n\.tr\(i18n\.TR\.([^,) ]+)\)")
def repl(m: Match) -> str: def repl(m: Match) -> str:
name = m.group(1).lower() name = stringcase.camelcase(m.group(1).lower())
args = m.group(2) #args = m.group(2)
return f"tr.{name}({args})" return f"i18n.{name}()"
def update_py(path: str) -> None: def update_py(path: str) -> None:
@ -24,11 +24,12 @@ def update_py(path: str) -> None:
if buf != buf2: if buf != buf2:
open(path, "w").writelines(buf2) open(path, "w").writelines(buf2)
print("updated", path) print("updated", path)
#print(buf2)
for dirpath, dirnames, fnames in os.walk(os.environ["BUILD_WORKSPACE_DIRECTORY"]): for dirpath, dirnames, fnames in os.walk(os.environ["BUILD_WORKSPACE_DIRECTORY"]):
if "bazel-" in dirpath: if "bazel-" in dirpath:
continue continue
for fname in fnames: for fname in fnames:
if fname.endswith(".py"): if fname.endswith(".ts"):
update_py(os.path.join(dirpath, fname)) update_py(os.path.join(dirpath, fname))

View File

@ -99,11 +99,11 @@ export function buildHistogram(
const cardsPerDay = Math.round(totalInPeriod / periodDays); const cardsPerDay = Math.round(totalInPeriod / periodDays);
const tableData = [ const tableData = [
{ {
label: i18n.tr(i18n.TR.STATISTICS_TOTAL), label: i18n.statisticsTotal(),
value: i18n.tr(i18n.TR.STATISTICS_CARDS, { cards: totalInPeriod }), value: i18n.tr(i18n.TR.STATISTICS_CARDS, { cards: totalInPeriod }),
}, },
{ {
label: i18n.tr(i18n.TR.STATISTICS_AVERAGE), label: i18n.statisticsAverage(),
value: i18n.tr(i18n.TR.STATISTICS_CARDS_PER_DAY, { count: cardsPerDay }), value: i18n.tr(i18n.TR.STATISTICS_CARDS_PER_DAY, { count: cardsPerDay }),
}, },
]; ];
@ -115,7 +115,7 @@ export function buildHistogram(
): string { ): string {
const day = dayLabel(i18n, bin.x0!, bin.x1!); const day = dayLabel(i18n, bin.x0!, bin.x1!);
const cards = i18n.tr(i18n.TR.STATISTICS_CARDS, { cards: bin.length }); const cards = i18n.tr(i18n.TR.STATISTICS_CARDS, { cards: bin.length });
const total = i18n.tr(i18n.TR.STATISTICS_RUNNING_TOTAL); const total = i18n.statisticsRunningTotal();
const totalCards = i18n.tr(i18n.TR.STATISTICS_CARDS, { cards: cumulative }); const totalCards = i18n.tr(i18n.TR.STATISTICS_CARDS, { cards: cumulative });
return `${day}:<br>${cards}<br>${total}: ${totalCards}`; return `${day}:<br>${cards}<br>${total}: ${totalCards}`;
} }

View File

@ -160,14 +160,14 @@ export function renderButtons(
let kind: string; let kind: string;
switch (d) { switch (d) {
case "learning": case "learning":
kind = i18n.tr(i18n.TR.STATISTICS_COUNTS_LEARNING_CARDS); kind = i18n.statisticsCountsLearningCards();
break; break;
case "young": case "young":
kind = i18n.tr(i18n.TR.STATISTICS_COUNTS_YOUNG_CARDS); kind = i18n.statisticsCountsYoungCards();
break; break;
case "mature": case "mature":
default: default:
kind = i18n.tr(i18n.TR.STATISTICS_COUNTS_MATURE_CARDS); kind = i18n.statisticsCountsMatureCards();
break; break;
} }
return `${kind} \u200e(${totalCorrect(d).percent}%)`; return `${kind} \u200e(${totalCorrect(d).percent}%)`;
@ -239,8 +239,8 @@ export function renderButtons(
// hover/tooltip // hover/tooltip
function tooltipText(d: Datum): string { function tooltipText(d: Datum): string {
const button = i18n.tr(i18n.TR.STATISTICS_ANSWER_BUTTONS_BUTTON_NUMBER); const button = i18n.statisticsAnswerButtonsButtonNumber();
const timesPressed = i18n.tr(i18n.TR.STATISTICS_ANSWER_BUTTONS_BUTTON_PRESSED); const timesPressed = i18n.statisticsAnswerButtonsButtonPressed();
const correctStr = i18n.tr( const correctStr = i18n.tr(
i18n.TR.STATISTICS_HOURS_CORRECT, i18n.TR.STATISTICS_HOURS_CORRECT,
totalCorrect(d.group) totalCorrect(d.group)

View File

@ -89,48 +89,38 @@ function countCards(
const extraQuery = separateInactive ? 'AND -("is:buried" OR "is:suspended")' : ""; const extraQuery = separateInactive ? 'AND -("is:buried" OR "is:suspended")' : "";
const counts: Count[] = [ const counts: Count[] = [
[i18n.statisticsCountsNewCards(), newCards, true, `"is:new"${extraQuery}`],
[ [
i18n.tr(i18n.TR.STATISTICS_COUNTS_NEW_CARDS), i18n.statisticsCountsLearningCards(),
newCards,
true,
`"is:new"${extraQuery}`,
],
[
i18n.tr(i18n.TR.STATISTICS_COUNTS_LEARNING_CARDS),
learn, learn,
true, true,
`(-"is:review" AND "is:learn")${extraQuery}`, `(-"is:review" AND "is:learn")${extraQuery}`,
], ],
[ [
i18n.tr(i18n.TR.STATISTICS_COUNTS_RELEARNING_CARDS), i18n.statisticsCountsRelearningCards(),
relearn, relearn,
true, true,
`("is:review" AND "is:learn")${extraQuery}`, `("is:review" AND "is:learn")${extraQuery}`,
], ],
[ [
i18n.tr(i18n.TR.STATISTICS_COUNTS_YOUNG_CARDS), i18n.statisticsCountsYoungCards(),
young, young,
true, true,
`("is:review" AND -"is:learn") AND "prop:ivl<21"${extraQuery}`, `("is:review" AND -"is:learn") AND "prop:ivl<21"${extraQuery}`,
], ],
[ [
i18n.tr(i18n.TR.STATISTICS_COUNTS_MATURE_CARDS), i18n.statisticsCountsMatureCards(),
mature, mature,
true, true,
`("is:review" -"is:learn") AND "prop:ivl>=21"${extraQuery}`, `("is:review" -"is:learn") AND "prop:ivl>=21"${extraQuery}`,
], ],
[ [
i18n.tr(i18n.TR.STATISTICS_COUNTS_SUSPENDED_CARDS), i18n.statisticsCountsSuspendedCards(),
suspended, suspended,
separateInactive, separateInactive,
'"is:suspended"', '"is:suspended"',
], ],
[ [i18n.statisticsCountsBuriedCards(), buried, separateInactive, '"is:buried"'],
i18n.tr(i18n.TR.STATISTICS_COUNTS_BURIED_CARDS),
buried,
separateInactive,
'"is:buried"',
],
]; ];
return counts; return counts;
@ -145,7 +135,7 @@ export function gatherData(
const counts = countCards(data.cards, separateInactive, i18n); const counts = countCards(data.cards, separateInactive, i18n);
return { return {
title: i18n.tr(i18n.TR.STATISTICS_COUNTS_TITLE), title: i18n.statisticsCountsTitle(),
counts, counts,
totalCards, totalCards,
}; };

View File

@ -112,7 +112,7 @@ export function prepareData(
const xTickFormat = (num: number): string => `${num.toFixed(0)}%`; const xTickFormat = (num: number): string => `${num.toFixed(0)}%`;
const tableData = [ const tableData = [
{ {
label: i18n.tr(i18n.TR.STATISTICS_AVERAGE_EASE), label: i18n.statisticsAverageEase(),
value: xTickFormat(sum(allEases) / total), value: xTickFormat(sum(allEases) / total),
}, },
]; ];

View File

@ -157,7 +157,7 @@ export function buildHistogram(
const cards = i18n.tr(i18n.TR.STATISTICS_CARDS_DUE, { const cards = i18n.tr(i18n.TR.STATISTICS_CARDS_DUE, {
cards: binValue(bin as any), cards: binValue(bin as any),
}); });
const totalLabel = i18n.tr(i18n.TR.STATISTICS_RUNNING_TOTAL); const totalLabel = i18n.statisticsRunningTotal();
return `${days}:<br>${cards}<br>${totalLabel}: ${cumulative}`; return `${days}:<br>${cards}<br>${totalLabel}: ${cumulative}`;
} }
@ -172,17 +172,17 @@ export function buildHistogram(
const periodDays = xMax! - xMin!; const periodDays = xMax! - xMin!;
const tableData = [ const tableData = [
{ {
label: i18n.tr(i18n.TR.STATISTICS_TOTAL), label: i18n.statisticsTotal(),
value: i18n.tr(i18n.TR.STATISTICS_REVIEWS, { reviews: total }), value: i18n.tr(i18n.TR.STATISTICS_REVIEWS, { reviews: total }),
}, },
{ {
label: i18n.tr(i18n.TR.STATISTICS_AVERAGE), label: i18n.statisticsAverage(),
value: i18n.tr(i18n.TR.STATISTICS_REVIEWS_PER_DAY, { value: i18n.tr(i18n.TR.STATISTICS_REVIEWS_PER_DAY, {
count: Math.round(total / periodDays), count: Math.round(total / periodDays),
}), }),
}, },
{ {
label: i18n.tr(i18n.TR.STATISTICS_DUE_TOMORROW), label: i18n.statisticsDueTomorrow(),
value: i18n.tr(i18n.TR.STATISTICS_REVIEWS, { value: i18n.tr(i18n.TR.STATISTICS_REVIEWS, {
reviews: sourceData.dueCounts.get(1) ?? 0, reviews: sourceData.dueCounts.get(1) ?? 0,
}), }),

View File

@ -148,7 +148,7 @@ export function prepareIntervalData(
): string { ): string {
// const day = dayLabel(i18n, bin.x0!, bin.x1!); // const day = dayLabel(i18n, bin.x0!, bin.x1!);
const interval = intervalLabel(i18n, bin.x0!, bin.x1!, bin.length); const interval = intervalLabel(i18n, bin.x0!, bin.x1!, bin.length);
const total = i18n.tr(i18n.TR.STATISTICS_RUNNING_TOTAL); const total = i18n.statisticsRunningTotal();
return `${interval}<br>${total}: \u200e${percent.toFixed(1)}%`; return `${interval}<br>${total}: \u200e${percent.toFixed(1)}%`;
} }
@ -163,7 +163,7 @@ export function prepareIntervalData(
const meanIntervalString = timeSpan(i18n, meanInterval * 86400, false); const meanIntervalString = timeSpan(i18n, meanInterval * 86400, false);
const tableData = [ const tableData = [
{ {
label: i18n.tr(i18n.TR.STATISTICS_AVERAGE_INTERVAL), label: i18n.statisticsAverageInterval(),
value: meanIntervalString, value: meanIntervalString,
}, },
]; ];

View File

@ -238,36 +238,20 @@ export function renderReviews(
const dayTotal = valueLabel(sum(totals)); const dayTotal = valueLabel(sum(totals));
let buf = `<table><tr><td>${day}</td><td align=right>${dayTotal}</td></tr>`; let buf = `<table><tr><td>${day}</td><td align=right>${dayTotal}</td></tr>`;
const lines = [ const lines = [
[ [oranges(1), i18n.statisticsCountsLearningCards(), valueLabel(totals[0])],
oranges(1), [reds(1), i18n.statisticsCountsRelearningCards(), valueLabel(totals[1])],
i18n.tr(i18n.TR.STATISTICS_COUNTS_LEARNING_CARDS),
valueLabel(totals[0]),
],
[
reds(1),
i18n.tr(i18n.TR.STATISTICS_COUNTS_RELEARNING_CARDS),
valueLabel(totals[1]),
],
[ [
lighterGreens(1), lighterGreens(1),
i18n.tr(i18n.TR.STATISTICS_COUNTS_YOUNG_CARDS), i18n.statisticsCountsYoungCards(),
valueLabel(totals[2]), valueLabel(totals[2]),
], ],
[ [
darkerGreens(1), darkerGreens(1),
i18n.tr(i18n.TR.STATISTICS_COUNTS_MATURE_CARDS), i18n.statisticsCountsMatureCards(),
valueLabel(totals[3]), valueLabel(totals[3]),
], ],
[ [purples(1), i18n.statisticsCountsEarlyCards(), valueLabel(totals[4])],
purples(1), ["transparent", i18n.statisticsRunningTotal(), valueLabel(cumulative)],
i18n.tr(i18n.TR.STATISTICS_COUNTS_EARLY_CARDS),
valueLabel(totals[4]),
],
[
"transparent",
i18n.tr(i18n.TR.STATISTICS_RUNNING_TOTAL),
valueLabel(cumulative),
],
]; ];
for (const [colour, label, detail] of lines) { for (const [colour, label, detail] of lines) {
buf += `<tr> buf += `<tr>
@ -400,7 +384,7 @@ export function renderReviews(
averageForPeriod = i18n.tr(i18n.TR.STATISTICS_MINUTES_PER_DAY, { averageForPeriod = i18n.tr(i18n.TR.STATISTICS_MINUTES_PER_DAY, {
count: Math.round(periodAvg / 1000 / 60), count: Math.round(periodAvg / 1000 / 60),
}); });
averageAnswerTimeLabel = i18n.tr(i18n.TR.STATISTICS_AVERAGE_ANSWER_TIME_LABEL); averageAnswerTimeLabel = i18n.statisticsAverageAnswerTimeLabel();
// need to get total review count to calculate average time // need to get total review count to calculate average time
const countBins = histogram() const countBins = histogram()
@ -429,7 +413,7 @@ export function renderReviews(
const tableData: TableDatum[] = [ const tableData: TableDatum[] = [
{ {
label: i18n.tr(i18n.TR.STATISTICS_DAYS_STUDIED), label: i18n.statisticsDaysStudied(),
value: i18n.tr(i18n.TR.STATISTICS_AMOUNT_OF_TOTAL_WITH_PERCENTAGE, { value: i18n.tr(i18n.TR.STATISTICS_AMOUNT_OF_TOTAL_WITH_PERCENTAGE, {
amount: studiedDays, amount: studiedDays,
total: periodDays, total: periodDays,
@ -437,15 +421,15 @@ export function renderReviews(
}), }),
}, },
{ label: i18n.tr(i18n.TR.STATISTICS_TOTAL), value: totalString }, { label: i18n.statisticsTotal(), value: totalString },
{ {
label: i18n.tr(i18n.TR.STATISTICS_AVERAGE_FOR_DAYS_STUDIED), label: i18n.statisticsAverageForDaysStudied(),
value: averageForDaysStudied, value: averageForDaysStudied,
}, },
{ {
label: i18n.tr(i18n.TR.STATISTICS_AVERAGE_OVER_PERIOD), label: i18n.statisticsAverageOverPeriod(),
value: averageForPeriod, value: averageForPeriod,
}, },
]; ];

View File

@ -72,7 +72,7 @@ export function gatherData(data: pb.BackendProto.GraphsOut, i18n: I18n): TodayDa
if (answerCount) { if (answerCount) {
const studiedTodayText = studiedToday(i18n, answerCount, answerMillis / 1000); const studiedTodayText = studiedToday(i18n, answerCount, answerMillis / 1000);
const againCount = answerCount - correctCount; const againCount = answerCount - correctCount;
let againCountText = i18n.tr(i18n.TR.STATISTICS_TODAY_AGAIN_COUNT); let againCountText = i18n.statisticsTodayAgainCount();
againCountText += ` ${againCount} (${((againCount / answerCount) * 100).toFixed( againCountText += ` ${againCount} (${((againCount / answerCount) * 100).toFixed(
2 2
)}%)`; )}%)`;
@ -90,16 +90,16 @@ export function gatherData(data: pb.BackendProto.GraphsOut, i18n: I18n): TodayDa
percent: (matureCorrect / matureCount) * 100, percent: (matureCorrect / matureCount) * 100,
}); });
} else { } else {
matureText = i18n.tr(i18n.TR.STATISTICS_TODAY_NO_MATURE_CARDS); matureText = i18n.statisticsTodayNoMatureCards();
} }
lines = [studiedTodayText, againCountText, typeCounts, matureText]; lines = [studiedTodayText, againCountText, typeCounts, matureText];
} else { } else {
lines = [i18n.tr(i18n.TR.STATISTICS_TODAY_NO_CARDS)]; lines = [i18n.statisticsTodayNoCards()];
} }
return { return {
title: i18n.tr(i18n.TR.STATISTICS_TODAY_TITLE), title: i18n.statisticsTodayTitle(),
lines, lines,
}; };
} }