Skip to content

Commit

Permalink
chore: updating server/budget/*.ts for i18n
Browse files Browse the repository at this point in the history
Updating *.ts files in root of packages/loot-core/src/server/budget
  • Loading branch information
stefanedwards committed Nov 15, 2024
1 parent 9743349 commit 3575611
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 21 deletions.
16 changes: 12 additions & 4 deletions packages/loot-core/src/server/budget/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import * as db from '../db';
import * as sheet from '../sheet';
import { batchMessages } from '../sync';

import { useTranslation } from 'react-i18next';

Check warning on line 8 in packages/loot-core/src/server/budget/actions.ts

View workflow job for this annotation

GitHub Actions / lint

`react-i18next` import should occur before import of `../../shared/months`

Check warning on line 8 in packages/loot-core/src/server/budget/actions.ts

View workflow job for this annotation

GitHub Actions / lint

'useTranslation' is defined but never used. Allowed unused vars must match /^(_|React)/u

export async function getSheetValue(
sheetName: string,
cell: string,
Expand Down Expand Up @@ -512,17 +514,23 @@ async function addMovementNotes({

const fromCategoryName =
from === 'to-be-budgeted'
? 'To Budget'
? t('To Budget')

Check failure on line 517 in packages/loot-core/src/server/budget/actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find name 't'.
: categories.find(c => c.id === from)?.name;

const toCategoryName =
to === 'to-be-budgeted'
? 'To Budget'
? t('To Budget')

Check failure on line 522 in packages/loot-core/src/server/budget/actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find name 't'.
: to === 'overbudgeted'
? 'Overbudgeted'
? t('Overbudgeted')

Check failure on line 524 in packages/loot-core/src/server/budget/actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find name 't'.
: categories.find(c => c.id === to)?.name;

const note = `Reassigned ${displayAmount} from ${fromCategoryName}${toCategoryName} on ${displayDay}`;
const note = t('Reassigned {{displayAmount}} from {{fromCategory}} → ' +

Check warning on line 527 in packages/loot-core/src/server/budget/actions.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `'Reassigned·{{displayAmount}}·from·{{fromCategory}}·→·'·+·⏎····'{{toCategory}}·on·{{displayDay}}',` with `⏎····'Reassigned·{{displayAmount}}·from·{{fromCategory}}·→·'·+⏎······'{{toCategory}}·on·{{displayDay}}',⏎···`

Check failure on line 527 in packages/loot-core/src/server/budget/actions.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find name 't'.
'{{toCategory}} on {{displayDay}}', {
displayAmount: displayAmount,

Check warning on line 529 in packages/loot-core/src/server/budget/actions.ts

View workflow job for this annotation

GitHub Actions / lint

Expected property shorthand
fromCategory: fromCategoryName,
toCategory: toCategoryName,
displayDay: displayDay

Check warning on line 532 in packages/loot-core/src/server/budget/actions.ts

View workflow job for this annotation

GitHub Actions / lint

Expected property shorthand

Check warning on line 532 in packages/loot-core/src/server/budget/actions.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,`
});

Check warning on line 533 in packages/loot-core/src/server/budget/actions.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `,⏎··`

await db.update('notes', {
id: monthBudgetNotesId,
Expand Down
27 changes: 17 additions & 10 deletions packages/loot-core/src/server/budget/categoryTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { goalsSchedule } from './goalsSchedule';
import { getActiveSchedules } from './statements';
import { Template } from './types/templates';

import { useTranslation } from 'react-i18next';

Check warning on line 12 in packages/loot-core/src/server/budget/categoryTemplate.ts

View workflow job for this annotation

GitHub Actions / lint

`react-i18next` import should occur before import of `../../shared/months`

Check warning on line 12 in packages/loot-core/src/server/budget/categoryTemplate.ts

View workflow job for this annotation

GitHub Actions / lint

'useTranslation' is defined but never used. Allowed unused vars must match /^(_|React)/u

export class CategoryTemplate {
/*----------------------------------------------------------------------------
* Using This Class:
Expand Down Expand Up @@ -282,7 +284,9 @@ export class CategoryTemplate {
.filter(t => t.type === 'schedule')
.forEach(t => {
if (!scheduleNames.includes(t.name.trim())) {
throw new Error(`Schedule ${t.name.trim()} does not exist`);
throw new Error(t('Schedule {{name}} does not exist', {

Check warning on line 287 in packages/loot-core/src/server/budget/categoryTemplate.ts

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎············`
name: t.name.trim()
}));
}
});
//find lowest priority
Expand All @@ -296,8 +300,9 @@ export class CategoryTemplate {
.filter(t => t.type === 'schedule' || t.type === 'by')
.forEach(t => {
if (t.priority !== lowestPriority) {
throw new Error(
`Schedule and By templates must be the same priority level. Fix by setting all Schedule and By templates to priority level ${lowestPriority}`,
throw new Error(t(
'Schedule and By templates must be the same priority level. Fix by setting all Schedule and By templates to priority level {{lowestPriority}}',
{ lowestPriority: lowestPriority}),
);
//t.priority = lowestPriority;
}
Expand All @@ -312,8 +317,8 @@ export class CategoryTemplate {
);
if (range < 0 && !(t.repeat || t.annual)) {
throw new Error(
`Target month has passed, remove or update the target month`,
);
t('Target month has passed, remove or update the target month'),
);
}
});
}
Expand All @@ -333,7 +338,9 @@ export class CategoryTemplate {
//skip the name check since these are special
} else if (!availNames.includes(n)) {
throw new Error(
`Category \x22${n}\x22 is not found in available income categories`,
t('Category \x22{{name}}\x22 is not found in available income categories', {

Check failure on line 341 in packages/loot-core/src/server/budget/categoryTemplate.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find name 't'.
name: n
}),
);
}
});
Expand All @@ -343,7 +350,7 @@ export class CategoryTemplate {
for (const t of this.templates) {
if (!t.limit) continue;
if (this.limitCheck) {
throw new Error('Only one `up to` allowed per category');
throw new Error(t('Only one `up to` allowed per category'));
} else if (t.limit) {
if (t.limit.period === 'daily') {
const numDays = monthUtils.differenceInCalendarDays(
Expand All @@ -364,7 +371,7 @@ export class CategoryTemplate {
} else if (t.limit.period === 'monthly') {
this.limitAmount = amountToInteger(t.limit.amount);
} else {
throw new Error('Invalid limit period. Check template syntax');
throw new Error(t('Invalid limit period. Check template syntax'));
}
//amount is good save the rest
this.limitCheck = true;
Expand All @@ -376,13 +383,13 @@ export class CategoryTemplate {
private checkSpend() {
const st = this.templates.filter(t => t.type === 'spend');
if (st.length > 1) {
throw new Error('Only one spend template is allowed per category');
throw new Error(t('Only one spend template is allowed per category'));

Check failure on line 386 in packages/loot-core/src/server/budget/categoryTemplate.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find name 't'.
}
}

private checkGoal() {
if (this.goals.length > 1) {
throw new Error(`Only one #goal is allowed per category`);
throw new Error(t('Only one #goal is allowed per category'));

Check failure on line 392 in packages/loot-core/src/server/budget/categoryTemplate.ts

View workflow job for this annotation

GitHub Actions / typecheck

Cannot find name 't'.
}
}

Expand Down
8 changes: 6 additions & 2 deletions packages/loot-core/src/server/budget/goalsSchedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {

import { isReflectBudget } from './actions';

import { useTranslation } from 'react-i18next';

async function createScheduleList(template, current_month, category) {
const t = [];
const errors = [];
Expand Down Expand Up @@ -62,7 +64,8 @@ async function createScheduleList(template, current_month, category) {
);
if (num_months < 0) {
//non-repeating schedules could be negative
errors.push(`Schedule ${template[ll].name} is in the Past.`);
errors.push(t('Schedule {{name}} is in the past.', {
name: template[ll].name }));
} else {
t.push({
target,
Expand Down Expand Up @@ -126,7 +129,8 @@ async function createScheduleList(template, current_month, category) {
}
} else {
errors.push(
`Schedule ${t[ll].name} is not active during the month in question.`,
t('Schedule {{name}} is not active during the month in question.', {
name: t[ll].name }),
);
}
}
Expand Down
9 changes: 6 additions & 3 deletions packages/loot-core/src/server/budget/goaltemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import { isReflectBudget, getSheetValue, setGoal, setBudget } from './actions';
import { CategoryTemplate } from './categoryTemplate';
import { checkTemplates, storeTemplates } from './template-notes';

import { useTranslation } from 'react-i18next';

export async function applyTemplate({ month }): Promise<Notification> {
await storeTemplates();
const categoryTemplates = await getTemplates(null);
Expand Down Expand Up @@ -188,13 +190,13 @@ async function processTemplate(
if (catObjects.length === 0 && errors.length === 0) {
return {
type: 'message',
message: 'Everything is up to date',
message: t('Everything is up to date'),
};
}
if (errors.length > 0) {
return {
sticky: true,
message: 'There were errors interpreting some templates:',
message: t('There were errors interpreting some templates:'),
pre: errors.join(`\n\n`),
};
}
Expand Down Expand Up @@ -245,6 +247,7 @@ async function processTemplate(

return {
type: 'message',
message: `Successfully applied templates to ${catObjects.length} categories`,
message: t('Successfully applied templates to {{count}} categories', {
count: catObjects.length }),
};
}
6 changes: 4 additions & 2 deletions packages/loot-core/src/server/budget/template-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
} from './statements';
import { Template } from './types/templates';

import { useTranslation } from 'react-i18next';

export const TEMPLATE_PREFIX = '#template';
export const GOAL_PREFIX = '#goal';

Expand Down Expand Up @@ -56,14 +58,14 @@ export async function checkTemplates(): Promise<Notification> {
if (errors.length) {
return {
sticky: true,
message: 'There were errors interpreting some templates:',
message: t('There were errors interpreting some templates:'),
pre: errors.join('\n\n'),
};
}

return {
type: 'message',
message: 'All templates passed! 🎉',
message: t('All templates passed!') + ' 🎉',
};
}

Expand Down

0 comments on commit 3575611

Please sign in to comment.