Skip to content

Commit 3f3af69

Browse files
committed
feat(api): ✨ Add pathToMessages function for dynamic locale loading
1 parent 5b8d677 commit 3f3af69

File tree

9 files changed

+22
-17
lines changed

9 files changed

+22
-17
lines changed

apps/api/src/vitnode.api.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export const POSTGRES_URL =
1313

1414
export const vitNodeApiConfig = buildApiConfig({
1515
plugins: [],
16+
pathToMessages: async path => await import(`./locales/${path}`),
1617
dbProvider: drizzle({
1718
connection: POSTGRES_URL,
1819
casing: 'camelCase',

apps/docs/src/vitnode.api.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export const POSTGRES_URL =
1111
process.env.POSTGRES_URL || 'postgresql://root:root@localhost:5432/vitnode';
1212

1313
export const vitNodeApiConfig = buildApiConfig({
14+
pathToMessages: async path => await import(`./locales/${path}`),
1415
captcha: {
1516
type: 'cloudflare_turnstile',
1617
siteKey: process.env.CLOUDFLARE_TURNSTILE_SITE_KEY,

packages/create-vitnode-app/copy-of-vitnode-app/api-single-app/src/vitnode.api.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const POSTGRES_URL =
66
process.env.POSTGRES_URL || 'postgresql://root:root@localhost:5432/vitnode';
77

88
export const vitNodeApiConfig = buildApiConfig({
9+
pathToMessages: async path => await import(`./locales/${path}`),
910
metadata: {
1011
title: 'VitNode',
1112
shortTitle: 'VitNode',

packages/vitnode/scripts/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ export const processPlugin = ({ initMessage }: { initMessage: string }) => {
254254
// followed by a path separator (or is exactly the sourceDir)
255255
const normalizedSrcPath = srcPath.replace(/\\/g, '/');
256256
const normalizedSourceDir = sourceDir.replace(/\\/g, '/');
257-
257+
258258
return (
259259
normalizedSrcPath === normalizedSourceDir ||
260260
normalizedSrcPath.startsWith(normalizedSourceDir + '/')

packages/vitnode/src/api/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export function VitNodeAPI({
5959
app.use(
6060
'*',
6161
globalMiddleware({
62+
pathToMessages: vitNodeApiConfig.pathToMessages,
6263
email: vitNodeApiConfig.email,
6364
metadata: vitNodeApiConfig.metadata,
6465
authorization: vitNodeApiConfig.authorization,

packages/vitnode/src/api/middlewares/global.middleware.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ export interface EnvVariablesVitNode {
3939
};
4040
};
4141
core: {
42-
plugins: { id: string }[];
4342
authorization: {
4443
adminCookieExpires: number;
4544
adminCookieName: string;
@@ -64,6 +63,8 @@ export interface EnvVariablesVitNode {
6463
shortTitle?: string;
6564
title: string;
6665
};
66+
pathToMessages: (path: string) => Promise<{ default: object }>;
67+
plugins: { id: string }[];
6768
};
6869
db: Pick<VitNodeApiConfig, 'dbProvider'>['dbProvider'];
6970
email: {
@@ -100,9 +101,15 @@ export const globalMiddleware = ({
100101
dbProvider,
101102
captcha,
102103
plugins,
104+
pathToMessages,
103105
}: Pick<
104106
VitNodeApiConfig,
105-
'authorization' | 'captcha' | 'dbProvider' | 'email' | 'plugins'
107+
| 'authorization'
108+
| 'captcha'
109+
| 'dbProvider'
110+
| 'email'
111+
| 'pathToMessages'
112+
| 'plugins'
106113
> &
107114
Pick<VitNodeConfig, 'metadata'>) => {
108115
return async (c: Context, next: Next) => {
@@ -154,6 +161,7 @@ export const globalMiddleware = ({
154161
c.set('email', new EmailModel(c));
155162

156163
c.set('core', {
164+
pathToMessages,
157165
metadata,
158166
email,
159167
authorization: {

packages/vitnode/src/api/models/email.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { HTTPException } from 'hono/http-exception';
66

77
import DefaultTemplateEmail from '../../emails/default-template';
88
import { CONFIG } from '../../lib/config';
9-
import { join } from 'path';
10-
import { readFile } from 'fs/promises';
119

1210
export interface EmailApiPlugin {
1311
sendEmail: (args: {
@@ -52,16 +50,10 @@ export class EmailModel {
5250

5351
const messagesPromises = pluginIds.map(async pluginId => {
5452
try {
55-
const path = join(
56-
process.cwd(),
57-
'src',
58-
'locales',
59-
pluginId,
60-
`${locale}.json`,
61-
);
62-
const messages = await readFile(path, 'utf-8');
53+
const path = `${pluginId}/${locale}.json`;
54+
const messages = await core.pathToMessages(path);
6355

64-
return JSON.parse(messages);
56+
return messages.default;
6557
} catch {
6658
return {};
6759
}
@@ -71,7 +63,7 @@ export class EmailModel {
7163
const messages = allMessages.reduce(
7264
(acc, curr) => ({ ...acc, ...curr }),
7365
{},
74-
);
66+
) as Record<string, string>;
7567

7668
const htmlContent =
7769
html ??

packages/vitnode/src/emails/default-template.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ import { CONFIG } from '../lib/config';
1919
interface DefaultTemplateEmailProps {
2020
children: React.ReactNode;
2121
head?: React.ReactNode;
22+
locale: string;
2223
logo?: {
2324
className?: string;
2425
src: Blob | string;
2526
};
27+
messages: Record<string, string>;
2628
metadata: {
2729
shortTitle?: string;
2830
title: string;
2931
url: string;
3032
};
3133
previewText?: string;
32-
messages: Record<string, string>;
33-
locale: string;
3434
}
3535

3636
export default function DefaultTemplateEmail({

packages/vitnode/src/vitnode.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export interface VitNodeApiConfig {
5454
shortTitle?: string;
5555
title: string;
5656
};
57+
pathToMessages: (path: string) => Promise<{ default: object }>;
5758
plugins: BuildPluginApiReturn[];
5859
rateLimiter?: Omit<IRateLimiterOptions, 'keyPrefix'>;
5960
}

0 commit comments

Comments
 (0)