Skip to content

Commit

Permalink
Merge branch 'main' into fix/exception_filter_error_res
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin authored Sep 8, 2024
2 parents 53368b7 + e9732dd commit dc8b725
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 9 deletions.
5 changes: 4 additions & 1 deletion docs/guides/nested.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ Here is an example of how this looks in your translation files
```

```typescript
i18n.t('test.PAGE_HOME.TITLE', {args: {} })
// => Home to this World

i18n.t('test.PAGE_HOME.SUBTITLE', {args: { username: 'Toon' } })
// => Hello Toon, this is the home page
```

:::tip
The [`formatter`](guides/formatting.md) is applied before doing nested translations. This way you can pass on arguments to your nested translations! 🎉
:::
:::
2 changes: 1 addition & 1 deletion docs/guides/type-safety.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ For now type safety is optional and need to be enabled. We're planning to make a

You can also use the generated types in your DTOs. This way you can reduce the chance of having a typo in your validation messages.

```typescript title="src/craete-user.dto.ts"
```typescript title="src/create-user.dto.ts"
import { I18nTranslations } from './generated/i18n.generated.ts';
import {
IsEmail,
Expand Down
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 dc8b725

Please sign in to comment.