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

View File

@ -99,11 +99,11 @@ export function buildHistogram(
const cardsPerDay = Math.round(totalInPeriod / periodDays);
const tableData = [
{
label: i18n.tr(i18n.TR.STATISTICS_TOTAL),
label: i18n.statisticsTotal(),
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 }),
},
];
@ -115,7 +115,7 @@ export function buildHistogram(
): string {
const day = dayLabel(i18n, bin.x0!, bin.x1!);
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 });
return `${day}:<br>${cards}<br>${total}: ${totalCards}`;
}

View File

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

View File

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

View File

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

View File

@ -157,7 +157,7 @@ export function buildHistogram(
const cards = i18n.tr(i18n.TR.STATISTICS_CARDS_DUE, {
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}`;
}
@ -172,17 +172,17 @@ export function buildHistogram(
const periodDays = xMax! - xMin!;
const tableData = [
{
label: i18n.tr(i18n.TR.STATISTICS_TOTAL),
label: i18n.statisticsTotal(),
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, {
count: Math.round(total / periodDays),
}),
},
{
label: i18n.tr(i18n.TR.STATISTICS_DUE_TOMORROW),
label: i18n.statisticsDueTomorrow(),
value: i18n.tr(i18n.TR.STATISTICS_REVIEWS, {
reviews: sourceData.dueCounts.get(1) ?? 0,
}),

View File

@ -148,7 +148,7 @@ export function prepareIntervalData(
): string {
// const day = dayLabel(i18n, bin.x0!, bin.x1!);
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)}%`;
}
@ -163,7 +163,7 @@ export function prepareIntervalData(
const meanIntervalString = timeSpan(i18n, meanInterval * 86400, false);
const tableData = [
{
label: i18n.tr(i18n.TR.STATISTICS_AVERAGE_INTERVAL),
label: i18n.statisticsAverageInterval(),
value: meanIntervalString,
},
];

View File

@ -238,36 +238,20 @@ export function renderReviews(
const dayTotal = valueLabel(sum(totals));
let buf = `<table><tr><td>${day}</td><td align=right>${dayTotal}</td></tr>`;
const lines = [
[
oranges(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]),
],
[oranges(1), i18n.statisticsCountsLearningCards(), valueLabel(totals[0])],
[reds(1), i18n.statisticsCountsRelearningCards(), valueLabel(totals[1])],
[
lighterGreens(1),
i18n.tr(i18n.TR.STATISTICS_COUNTS_YOUNG_CARDS),
i18n.statisticsCountsYoungCards(),
valueLabel(totals[2]),
],
[
darkerGreens(1),
i18n.tr(i18n.TR.STATISTICS_COUNTS_MATURE_CARDS),
i18n.statisticsCountsMatureCards(),
valueLabel(totals[3]),
],
[
purples(1),
i18n.tr(i18n.TR.STATISTICS_COUNTS_EARLY_CARDS),
valueLabel(totals[4]),
],
[
"transparent",
i18n.tr(i18n.TR.STATISTICS_RUNNING_TOTAL),
valueLabel(cumulative),
],
[purples(1), i18n.statisticsCountsEarlyCards(), valueLabel(totals[4])],
["transparent", i18n.statisticsRunningTotal(), valueLabel(cumulative)],
];
for (const [colour, label, detail] of lines) {
buf += `<tr>
@ -400,7 +384,7 @@ export function renderReviews(
averageForPeriod = i18n.tr(i18n.TR.STATISTICS_MINUTES_PER_DAY, {
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
const countBins = histogram()
@ -429,7 +413,7 @@ export function renderReviews(
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, {
amount: studiedDays,
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,
},
{
label: i18n.tr(i18n.TR.STATISTICS_AVERAGE_OVER_PERIOD),
label: i18n.statisticsAverageOverPeriod(),
value: averageForPeriod,
},
];

View File

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