Skip to content

Commit 0765110

Browse files
committed
fix: load event ns
1 parent 568365d commit 0765110

File tree

7 files changed

+56
-18
lines changed

7 files changed

+56
-18
lines changed

apps/test-bot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
"devDependencies": {
2222
"tsx": "^4.7.0"
2323
}
24-
}
24+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export default async function onGuildMemberAdd() {
22
console.log('guildMemberAdd event fired!');
3-
};
3+
}

packages/commandkit/events.cjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { getCommandKit } = require('./dist/context/async-context.js');
22

33
export function useEvents() {
4-
const commandkit = getCommandKit(true);
5-
return commandkit.events;
6-
}
4+
const commandkit = getCommandKit(true);
5+
return commandkit.events;
6+
}

packages/commandkit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,4 @@
137137
"engines": {
138138
"node": ">=22"
139139
}
140-
}
140+
}

packages/i18n/src/hooks.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
eventWorkerContext,
66
CommandKitEnvironment,
77
} from 'commandkit';
8+
import { TFunction } from 'i18next';
89

910
/**
1011
* Gets the localization context. If the context originates from the event worker context, the returned t function is not bound to the command.
@@ -30,7 +31,7 @@ export function locale(locale?: Locale): CommandLocalizationContext {
3031
const detectedLocale: Locale = locale || commandkit.config.defaultLocale;
3132

3233
return {
33-
t: i18n.t,
34+
t: i18n.getFixedT(context.event),
3435
locale: detectedLocale,
3536
i18n,
3637
isEventWorker: true,
@@ -45,3 +46,18 @@ export function locale(locale?: Locale): CommandLocalizationContext {
4546

4647
return context.locale(locale);
4748
}
49+
50+
/**
51+
* Fetches the translation function for the given locale and namespace.
52+
* @param lng The locale to use.
53+
* @param ns The namespace to use.
54+
* @param keyPrefix The key prefix to use.
55+
* @returns The translation function.
56+
*/
57+
export function fetchT(
58+
lng: string | string[],
59+
ns?: string | null,
60+
keyPrefix?: string,
61+
): TFunction {
62+
return locale().i18n.getFixedT(lng, ns, keyPrefix);
63+
}

packages/i18n/src/i18n.ts

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ export class I18nPlugin extends RuntimePlugin<LocalizationPluginOptions> {
193193

194194
private async scanLocaleDirectory() {
195195
const endPattern = /{{lng}}(\/|\\){{ns}}.json$/;
196+
const eventFilePattern = /\.event\.json$/;
196197
const localesPath = I18nPlugin.getLoadPath();
197198
const scanDirTarget = localesPath.replace(endPattern, '');
198199

@@ -211,6 +212,7 @@ export class I18nPlugin extends RuntimePlugin<LocalizationPluginOptions> {
211212

212213
for (const file of files) {
213214
const isValidLocale = DISCORD_LOCALES.has(file.name as Locale);
215+
214216
if (file.isDirectory()) {
215217
if (!isValidLocale) {
216218
Logger.warn(`I18nPlugin: Invalid locale directory ${file.name}`);
@@ -229,29 +231,49 @@ export class I18nPlugin extends RuntimePlugin<LocalizationPluginOptions> {
229231

230232
if (!/\.json$/.test(ext)) continue;
231233

232-
const name = basename(file.name, ext);
233-
namespaces.add(name);
234+
let name: string;
235+
const isEvent = eventFilePattern.test(file.name);
236+
// Handle event files specially
237+
if (isEvent) {
238+
name = file.name.replace('.event.json', '');
239+
} else {
240+
name = basename(file.name, ext);
241+
}
242+
namespaces.add(`${isEvent ? 'event_' : ''}${name}`);
234243

235244
const locale = basename(file.parentPath);
236245

237-
await this.loadMetadata(
238-
join(file.parentPath, file.name),
239-
locale,
240-
name,
241-
);
246+
if (!isEvent)
247+
await this.loadMetadata(
248+
join(file.parentPath, file.name),
249+
locale,
250+
name,
251+
);
242252
}
243253
}
244254
} else if (file.isFile()) {
245255
const ext = extname(file.name);
246256

247257
if (!/\.json$/.test(ext)) continue;
248258

249-
const name = basename(file.name, ext);
250-
namespaces.add(name);
259+
let name: string;
260+
const isEvent = eventFilePattern.test(file.name);
261+
// Handle event files specially
262+
if (isEvent) {
263+
name = file.name.replace('.event.json', '');
264+
} else {
265+
name = basename(file.name, ext);
266+
}
267+
namespaces.add(`${isEvent ? 'event_' : ''}${name}`);
251268

252269
const locale = basename(file.parentPath);
253270

254-
await this.loadMetadata(join(file.parentPath, file.name), locale, name);
271+
if (!isEvent)
272+
await this.loadMetadata(
273+
join(file.parentPath, file.name),
274+
locale,
275+
name,
276+
);
255277
}
256278
}
257279

packages/i18n/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ export function i18n(
2222
}
2323

2424
export { I18nCliTemplatePlugin };
25-
export { locale } from './hooks';
25+
export { locale, fetchT } from './hooks';
2626
export { getI18n, I18nPlugin, type CommandLocalizationContext } from './i18n';

0 commit comments

Comments
 (0)