Skip to content

Commit e9732dd

Browse files
authored
Merge pull request #653 from toonvanstrijp/logging
2 parents df4f637 + 27540f9 commit e9732dd

File tree

4 files changed

+11
-7
lines changed

4 files changed

+11
-7
lines changed

src/i18n.context.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ArgumentsHost } from '@nestjs/common';
22
import { AsyncLocalStorage } from 'async_hooks';
3-
import { I18nTranslator, I18nValidationError } from './interfaces';
3+
import { I18nOptions, I18nTranslator, I18nValidationError } from './interfaces';
44
import { I18nService, TranslateOptions } from './services/i18n.service';
55
import { Path, PathValue } from './types';
66
import { getContextObject } from './utils';
@@ -16,7 +16,7 @@ export class I18nContext<K = Record<string, unknown>>
1616
return this;
1717
}
1818

19-
constructor(readonly lang: string, readonly service: I18nService<K>) {}
19+
constructor(readonly lang: string, readonly service: I18nService<K>, readonly i18nOptions: I18nOptions,) {}
2020

2121
public translate<P extends Path<K> = any, R = PathValue<K, P>>(
2222
key: P,
@@ -64,7 +64,7 @@ export class I18nContext<K = Record<string, unknown>>
6464
const i18n = this.storage.getStore() as I18nContext<K> | undefined;
6565

6666
if (!i18n && !!context) {
67-
return getContextObject(context)?.i18nContext;
67+
return getContextObject(i18n.i18nOptions,context)?.i18nContext;
6868
}
6969

7070
return i18n;

src/interceptors/i18n-language.interceptor.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class I18nLanguageInterceptor implements NestInterceptor {
3737
const i18nContext = I18nContext.current();
3838
let language = null;
3939

40-
const ctx = getContextObject(context);
40+
const ctx = getContextObject(this.i18nOptions,context);
4141

4242
// Skip interceptor if language is already resolved (in case of http middleware) or when ctx is undefined (unsupported context)
4343
if (ctx === undefined || !!ctx.i18nLang) {
@@ -68,7 +68,7 @@ export class I18nLanguageInterceptor implements NestInterceptor {
6868
}
6969

7070
if (!i18nContext) {
71-
ctx.i18nContext = new I18nContext(ctx.i18nLang, this.i18nService);
71+
ctx.i18nContext = new I18nContext(ctx.i18nLang, this.i18nService, this.i18nOptions);
7272

7373
if (!this.i18nOptions.skipAsyncHook) {
7474
return new Observable((observer) => {

src/middlewares/i18n.middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export class I18nMiddleware implements NestMiddleware {
6666
req.app.locals.i18nLang = req.i18nLang;
6767
}
6868

69-
req.i18nContext = new I18nContext(req.i18nLang, this.i18nService);
69+
req.i18nContext = new I18nContext(req.i18nLang, this.i18nService, this.i18nOptions);
7070

7171
if (this.i18nOptions.skipAsyncHook) {
7272
next();

src/utils/context.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { ArgumentsHost, ExecutionContext, Logger } from '@nestjs/common';
2+
import { I18nOptions } from '..';
23

34
const logger = new Logger('I18nService');
45

56
export function getContextObject(
7+
i18nOptions: I18nOptions,
68
context?: ExecutionContext | ArgumentsHost,
79
): any {
810
const contextType = context?.getType<string>() ?? 'undefined';
@@ -17,6 +19,8 @@ export function getContextObject(
1719
case 'rmq':
1820
return context.getArgs()[1];
1921
default:
20-
logger.warn(`context type: ${contextType} not supported`);
22+
if(i18nOptions.logging){
23+
logger.warn(`context type: ${contextType} not supported`);
24+
}
2125
}
2226
}

0 commit comments

Comments
 (0)