Skip to content

Commit

Permalink
Merge 3ca24ec into bee66b9
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin committed Sep 14, 2023
2 parents bee66b9 + 3ca24ec commit 3c6d5d8
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 5 deletions.
48 changes: 48 additions & 0 deletions docs/quick-start.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,54 @@ export class AppModule {}
```


## I18nOptions

```typescript

export interface I18nOptions {
// The default language to use as a fallback if a translation is not available in the requested language.
fallbackLanguage: string;

// An optional dictionary of fallback languages for specific keys or phrases.
fallbacks?: { [key: string]: string };

// An array of resolvers used to resolve the requested translation.
resolvers?: I18nOptionResolver[];

// The loader type to use for loading translation data.
loader?: Type<I18nLoader>;

// Configuration options for the loader.
loaderOptions: any;

// A formatter for formatting translations (e.g., for date or number formatting).
formatter?: Formatter;

// Whether or not to enable logging for i18n operations.
logging?: boolean;

// The view engine to use for rendering templates (if applicable).
viewEngine?: 'hbs' | 'pug' | 'ejs';

// Whether to disable any middleware related to i18n.
disableMiddleware?: boolean;

// Whether to skip asynchronous hooks related to i18n.
skipAsyncHook?: boolean;

// Configuration options for the i18n validator.
validatorOptions?: I18nValidatorOptions;

// Whether to throw an error when a translation key is missing.
throwOnMissingKey?: boolean;

// The output path for generated types (if any).
typesOutputPath?: string;
}

```


:::caution

The `I18nModule` is a [**global**](https://docs.nestjs.com/modules#global-modules) module. This means you'll only need to register the module once (in the root module). After that it's accessible throughout the whole application.
Expand Down
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"internationalise",
"international",
"internationalize",
"internationalise",
"locale"
],
"repository": {
Expand Down
1 change: 1 addition & 0 deletions src/i18n.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const defaultOptions: Partial<I18nOptions> = {
resolvers: [],
formatter: format,
logging: true,
throwOnMissingKey: false,
loader: I18nJsonLoader,
};

Expand Down
1 change: 1 addition & 0 deletions src/interfaces/i18n-options.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export interface I18nOptions {
disableMiddleware?: boolean;
skipAsyncHook?: boolean;
validatorOptions?: I18nValidatorOptions;
throwOnMissingKey?: boolean;
typesOutputPath?: string;
}

Expand Down
3 changes: 3 additions & 0 deletions src/services/i18n.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ export class I18nService<K = Record<string, unknown>>
key as string
}" in "${lang}" does not exist.`;
this.logger.error(message);
if (this.i18nOptions.throwOnMissingKey) {
throw new Error(message);
}
}

const nextFallbackLanguage = this.getFallbackLanguage(lang);
Expand Down
9 changes: 5 additions & 4 deletions src/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ValidationError,
} from 'class-validator';
import { I18nService, TranslateOptions } from '../services/i18n.service';
import { HttpStatus, MiddlewareConsumer } from '@nestjs/common';
import { MiddlewareConsumer } from '@nestjs/common';
import { NestMiddlewareConsumer, Path } from '../types';

export function shouldResolve(e: I18nOptionResolver) {
Expand All @@ -32,11 +32,12 @@ function validationErrorToI18n(e: ValidationError): I18nValidationError {
};
}

export function i18nValidationErrorFactory(
): (errors: ValidationError[]) => I18nValidationException {
export function i18nValidationErrorFactory(): (
errors: ValidationError[],
) => I18nValidationException {
return (errors: ValidationError[]): I18nValidationException => {
return new I18nValidationException(
errors.map((e) => validationErrorToI18n(e))
errors.map((e) => validationErrorToI18n(e)),
);
};
}
Expand Down

0 comments on commit 3c6d5d8

Please sign in to comment.