managarten/packages/bot-services/src/i18n/i18n.module.ts
Till-JS fd30fdc620 fix(bot-services): add global: true to I18nModule dynamic module config
The @Global() decorator on a module class doesn't apply when using
dynamic modules (forRoot/forRootAsync). The global: true property
must be set directly in the DynamicModule return object.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-02-17 12:48:28 +01:00

75 lines
1.7 KiB
TypeScript

import { Module, DynamicModule } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { I18nService, I18N_OPTIONS } from './i18n.service';
import { I18nOptions } from './types';
/**
* I18n Module for Matrix Bots
*
* Provides multi-language support with per-user language preferences.
*
* NOTE: SessionService is optional. If you want per-user language preferences,
* import SessionModule in your app before I18nModule. Otherwise, the default
* language will be used for all users.
*
* @example
* ```typescript
* // Basic usage (uses default language for all users)
* @Module({
* imports: [I18nModule.forRoot()],
* })
*
* // With per-user preferences (requires SessionModule)
* @Module({
* imports: [
* SessionModule.forRoot({ storageMode: 'redis' }),
* I18nModule.forRoot({ defaultLanguage: 'en' }),
* ],
* })
* ```
*/
@Module({})
export class I18nModule {
/**
* Register the I18n module
*/
static forRoot(options?: I18nOptions): DynamicModule {
return {
module: I18nModule,
global: true,
imports: [ConfigModule],
providers: [
{
provide: I18N_OPTIONS,
useValue: options || {},
},
I18nService,
],
exports: [I18nService],
};
}
/**
* Register the I18n module with async configuration
*/
static forRootAsync(options: {
imports?: any[];
useFactory: (...args: any[]) => I18nOptions | Promise<I18nOptions>;
inject?: any[];
}): DynamicModule {
return {
module: I18nModule,
global: true,
imports: [...(options.imports || []), ConfigModule],
providers: [
{
provide: I18N_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
},
I18nService,
],
exports: [I18nService],
};
}
}