Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translation support for packages/loot-core/src/server/budget/template-nodes #3840

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/loot-core/src/server/budget/template-notes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
getCategoriesWithTemplateNotes,
resetCategoryGoalDefsWithNoTemplates,
} from './statements';
import { Template } from './types/templates';

Check warning on line 11 in packages/loot-core/src/server/budget/template-notes.ts

View workflow job for this annotation

GitHub Actions / lint

There should be at least one empty line between import groups
import { useTranslation } from 'react-i18next';

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

View workflow job for this annotation

GitHub Actions / lint

`react-i18next` import should occur before import of `../../client/state-types/notifications`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical: Incorrect usage of React hooks in server-side code

The useTranslation hook cannot be used in server-side code as it's specifically designed for React components. This will cause runtime errors since React hooks can only be used within React components or custom hooks.

Consider these alternatives for server-side translation:

  1. Use i18next directly instead of react-i18next:
-import { useTranslation } from 'react-i18next';
+import i18next from 'i18next';

 export async function checkTemplates(): Promise<Notification> {
-  const { t } = useTranslation();
+  const t = i18next.t.bind(i18next);
   // ... rest of the function
  1. Or inject the translation function as a parameter:
-import { useTranslation } from 'react-i18next';

-export async function checkTemplates(): Promise<Notification> {
+export async function checkTemplates(t: (key: string, options?: any) => string): Promise<Notification> {
-  const { t } = useTranslation();
   // ... rest of the function

Also applies to: 39-39

🧰 Tools
🪛 GitHub Check: lint

[warning] 12-12:
react-i18next import should occur before import of ../../client/state-types/notifications


export const TEMPLATE_PREFIX = '#template';
export const GOAL_PREFIX = '#goal';
Expand All @@ -35,12 +36,13 @@
};

export async function checkTemplates(): Promise<Notification> {
const { t } = useTranslation();

Check failure on line 39 in packages/loot-core/src/server/budget/template-notes.ts

View workflow job for this annotation

GitHub Actions / lint

React Hook "useTranslation" is called in function "checkTemplates" that is neither a React function component nor a custom React Hook function. React component names must start with an uppercase letter. React Hook names must start with the word "use"
const categoryWithTemplates = await getCategoriesWithTemplates();
const schedules = await getActiveSchedules();
const scheduleNames = schedules.map(({ name }) => name);
const errors: string[] = [];

categoryWithTemplates.forEach(({ id, name, templates }) => {

Check warning on line 45 in packages/loot-core/src/server/budget/template-notes.ts

View workflow job for this annotation

GitHub Actions / lint

'id' is defined but never used
templates.forEach(template => {
if (template.type === 'error') {
errors.push(`${name}: ${template.line}`);
Expand All @@ -48,22 +50,22 @@
template.type === 'schedule' &&
!scheduleNames.includes(template.name)
) {
errors.push(`${id}: Schedule “${template.name} does not exist`);
errors.push(t('{{id}}: Schedule {{template.name}} does not exist'));
jfdoming marked this conversation as resolved.
Show resolved Hide resolved
}
});
});

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
Loading