Skip to content

Commit

Permalink
Merge pull request #653 from toonvanstrijp/logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin committed Sep 7, 2024
2 parents df4f637 + 27540f9 commit e9732dd
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions src/i18n.context.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ArgumentsHost } from '@nestjs/common';
import { AsyncLocalStorage } from 'async_hooks';
import { I18nTranslator, I18nValidationError } from './interfaces';
import { I18nOptions, I18nTranslator, I18nValidationError } from './interfaces';
import { I18nService, TranslateOptions } from './services/i18n.service';
import { Path, PathValue } from './types';
import { getContextObject } from './utils';
Expand All @@ -16,7 +16,7 @@ export class I18nContext<K = Record<string, unknown>>
return this;
}

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

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

if (!i18n && !!context) {
return getContextObject(context)?.i18nContext;
return getContextObject(i18n.i18nOptions,context)?.i18nContext;
}

return i18n;
Expand Down
4 changes: 2 additions & 2 deletions src/interceptors/i18n-language.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class I18nLanguageInterceptor implements NestInterceptor {
const i18nContext = I18nContext.current();
let language = null;

const ctx = getContextObject(context);
const ctx = getContextObject(this.i18nOptions,context);

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

if (!i18nContext) {
ctx.i18nContext = new I18nContext(ctx.i18nLang, this.i18nService);
ctx.i18nContext = new I18nContext(ctx.i18nLang, this.i18nService, this.i18nOptions);

if (!this.i18nOptions.skipAsyncHook) {
return new Observable((observer) => {
Expand Down
2 changes: 1 addition & 1 deletion src/middlewares/i18n.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class I18nMiddleware implements NestMiddleware {
req.app.locals.i18nLang = req.i18nLang;
}

req.i18nContext = new I18nContext(req.i18nLang, this.i18nService);
req.i18nContext = new I18nContext(req.i18nLang, this.i18nService, this.i18nOptions);

if (this.i18nOptions.skipAsyncHook) {
next();
Expand Down
6 changes: 5 additions & 1 deletion src/utils/context.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { ArgumentsHost, ExecutionContext, Logger } from '@nestjs/common';
import { I18nOptions } from '..';

const logger = new Logger('I18nService');

export function getContextObject(
i18nOptions: I18nOptions,
context?: ExecutionContext | ArgumentsHost,
): any {
const contextType = context?.getType<string>() ?? 'undefined';
Expand All @@ -17,6 +19,8 @@ export function getContextObject(
case 'rmq':
return context.getArgs()[1];
default:
logger.warn(`context type: ${contextType} not supported`);
if(i18nOptions.logging){
logger.warn(`context type: ${contextType} not supported`);
}
}
}

0 comments on commit e9732dd

Please sign in to comment.