Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

nestjs-i18n

Package Overview
Dependencies
Maintainers
1
Versions
120
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

nestjs-i18n - npm Package Compare versions

Comparing version 4.0.6 to 4.1.0

bin/lib/decorators/i18n.decorator.js

4

bin/lib/decorators/i18n-lang.decorator.js

@@ -6,3 +6,3 @@ "use strict";

if (Array.isArray(req)) {
return resolveI18nLangaugeFromGraphQLContext(req);
return resolveI18nLanguageFromGraphQLContext(req);
}

@@ -14,5 +14,5 @@ return resolveI18nLanguageFromRestRequest(req);

}
function resolveI18nLangaugeFromGraphQLContext(req) {
function resolveI18nLanguageFromGraphQLContext(req) {
const [root, args, ctx, info] = req;
return ctx.req.i18nLang;
}

@@ -19,2 +19,3 @@ "use strict";

const i18n_service_1 = require("./services/i18n.service");
const i18n_request_scope_service_1 = require("./services/i18n-request-scope.service");
const parse_1 = require("./utils/parse");

@@ -78,6 +79,7 @@ const path = require("path");

i18n_service_1.I18nService,
i18n_request_scope_service_1.I18nRequestScopeService,
i18nOptions,
translationsProvider,
],
exports: [i18n_service_1.I18nService],
exports: [i18n_service_1.I18nService, i18n_request_scope_service_1.I18nRequestScopeService],
};

@@ -96,4 +98,5 @@ }

i18n_service_1.I18nService,
i18n_request_scope_service_1.I18nRequestScopeService,
],
exports: [i18n_service_1.I18nService],
exports: [i18n_service_1.I18nService, i18n_request_scope_service_1.I18nRequestScopeService],
};

@@ -100,0 +103,0 @@ }

@@ -8,5 +8,8 @@ "use strict";

__export(require("./i18n.constants"));
__export(require("./i18n.context"));
__export(require("./services/i18n.service"));
__export(require("./decorators/i18n-lang.decorator"));
__export(require("./decorators/i18n.decorator"));
__export(require("./resolvers/header.resolver"));
__export(require("./resolvers/query.resolver"));
__export(require("./resolvers/cookie.resolver"));

@@ -17,5 +17,7 @@ "use strict";

const i18n_constants_1 = require("../i18n.constants");
const i18n_service_1 = require("../services/i18n.service");
let I18nLanguageMiddleware = class I18nLanguageMiddleware {
constructor(i18nOptions) {
constructor(i18nOptions, i18nService) {
this.i18nOptions = i18nOptions;
this.i18nService = i18nService;
}

@@ -31,2 +33,3 @@ use(req, res, next) {

req.i18nLang = language || this.i18nOptions.fallbackLanguage;
req.i18nService = this.i18nService;
next();

@@ -38,4 +41,4 @@ }

__param(0, common_1.Inject(i18n_constants_1.I18N_OPTIONS)),
__metadata("design:paramtypes", [Object])
__metadata("design:paramtypes", [Object, i18n_service_1.I18nService])
], I18nLanguageMiddleware);
exports.I18nLanguageMiddleware = I18nLanguageMiddleware;

@@ -28,5 +28,3 @@ "use strict";

translate(key, options) {
options = Object.assign({
lang: this.i18nOptions.fallbackLanguage,
}, options);
options = Object.assign({ lang: this.i18nOptions.fallbackLanguage }, options);
const { lang, args } = options;

@@ -33,0 +31,0 @@ const translationsByLanguage = this.translations[lang];

@@ -6,3 +6,3 @@ "use strict";

if (Array.isArray(req)) {
return resolveI18nLangaugeFromGraphQLContext(req);
return resolveI18nLanguageFromGraphQLContext(req);
}

@@ -14,3 +14,3 @@ return resolveI18nLanguageFromRestRequest(req);

}
function resolveI18nLangaugeFromGraphQLContext(req) {
function resolveI18nLanguageFromGraphQLContext(req) {
const [root, args, ctx, info] = req;

@@ -17,0 +17,0 @@ return ctx.req.i18nLang;

@@ -19,2 +19,3 @@ "use strict";

const i18n_service_1 = require("./services/i18n.service");
const i18n_request_scope_service_1 = require("./services/i18n-request-scope.service");
const parse_1 = require("./utils/parse");

@@ -78,6 +79,7 @@ const path = require("path");

i18n_service_1.I18nService,
i18n_request_scope_service_1.I18nRequestScopeService,
i18nOptions,
translationsProvider,
],
exports: [i18n_service_1.I18nService],
exports: [i18n_service_1.I18nService, i18n_request_scope_service_1.I18nRequestScopeService],
};

@@ -96,4 +98,5 @@ }

i18n_service_1.I18nService,
i18n_request_scope_service_1.I18nRequestScopeService,
],
exports: [i18n_service_1.I18nService],
exports: [i18n_service_1.I18nService, i18n_request_scope_service_1.I18nRequestScopeService],
};

@@ -100,0 +103,0 @@ }

export * from './i18n.module';
export * from './i18n.constants';
export * from './i18n.context';
export * from './services/i18n.service';

@@ -7,3 +8,5 @@ export * from './interfaces/i18n-options.interface';

export * from './decorators/i18n-lang.decorator';
export * from './decorators/i18n.decorator';
export * from './resolvers/header.resolver';
export * from './resolvers/query.resolver';
export * from './resolvers/cookie.resolver';

@@ -8,6 +8,9 @@ "use strict";

__export(require("./i18n.constants"));
__export(require("./i18n.context"));
__export(require("./services/i18n.service"));
__export(require("./decorators/i18n-lang.decorator"));
__export(require("./decorators/i18n.decorator"));
__export(require("./resolvers/header.resolver"));
__export(require("./resolvers/query.resolver"));
__export(require("./resolvers/cookie.resolver"));
//# sourceMappingURL=index.js.map
import { NestMiddleware } from '@nestjs/common';
import { I18nOptions } from '../index';
import { I18nService } from '../services/i18n.service';
export declare class I18nLanguageMiddleware implements NestMiddleware {
private readonly i18nOptions;
constructor(i18nOptions: I18nOptions);
private readonly i18nService;
constructor(i18nOptions: I18nOptions, i18nService: I18nService);
use(req: any, res: any, next: () => void): void;
}

@@ -17,5 +17,7 @@ "use strict";

const i18n_constants_1 = require("../i18n.constants");
const i18n_service_1 = require("../services/i18n.service");
let I18nLanguageMiddleware = class I18nLanguageMiddleware {
constructor(i18nOptions) {
constructor(i18nOptions, i18nService) {
this.i18nOptions = i18nOptions;
this.i18nService = i18nService;
}

@@ -31,2 +33,3 @@ use(req, res, next) {

req.i18nLang = language || this.i18nOptions.fallbackLanguage;
req.i18nService = this.i18nService;
next();

@@ -38,5 +41,5 @@ }

__param(0, common_1.Inject(i18n_constants_1.I18N_OPTIONS)),
__metadata("design:paramtypes", [Object])
__metadata("design:paramtypes", [Object, i18n_service_1.I18nService])
], I18nLanguageMiddleware);
exports.I18nLanguageMiddleware = I18nLanguageMiddleware;
//# sourceMappingURL=i18n-language-middleware.js.map

@@ -28,5 +28,3 @@ "use strict";

translate(key, options) {
options = Object.assign({
lang: this.i18nOptions.fallbackLanguage,
}, options);
options = Object.assign({ lang: this.i18nOptions.fallbackLanguage }, options);
const { lang, args } = options;

@@ -33,0 +31,0 @@ const translationsByLanguage = this.translations[lang];

{
"name": "nestjs-i18n",
"version": "4.0.6",
"version": "4.1.0",
"description": "",

@@ -35,2 +35,3 @@ "author": "Toon van Strijp",

"@types/commander": "^2.12.2",
"@types/cookie": "^0.3.3",
"@types/flat": "0.0.28",

@@ -46,5 +47,5 @@ "@types/jest": "^24.0.18",

"graphql-tools": "^4.0.5",
"husky": "3.0.5",
"husky": "3.0.9",
"jest": "^24.9.0",
"lint-staged": "9.3.0",
"lint-staged": "9.4.1",
"prettier": "1.18.2",

@@ -58,4 +59,5 @@ "reflect-metadata": "^0.1.13",

"chalk": "^2.4.2",
"commander": "^3.0.1",
"flat": "^4.1.0",
"commander": "^4.0.0",
"cookie": "^0.4.0",
"flat": "^5.0.0",
"lodash": "^4.17.0",

@@ -62,0 +64,0 @@ "string-format": "^2.0.0"

@@ -98,2 +98,3 @@ [![Build Status](https://travis-ci.org/ToonvanStrijp/nestjs-i18n.svg?branch=master)](https://travis-ci.org/ToonvanStrijp/nestjs-i18n) [![Greenkeeper badge](https://badges.greenkeeper.io/ToonvanStrijp/nestjs-i18n.svg)](https://greenkeeper.io/)

new HeaderResolver(),
new CookieResolver(['lang', 'locale', 'l'])
],

@@ -112,6 +113,8 @@ }),

| `HeaderResolver` | `accept-language` |
| `CookieResolver` | `lang` |
To implement your own resolver (or custom logic) use the `I18nResolver` interface.
### Using Translation Service and Language Resolver
### Translating with i18n module
#### `I18nLang` decorator and `I18nService`
```typescript

@@ -136,2 +139,41 @@ @Controller()

#### `I18n` decorator
```typescript
@Controller()
export class SampleController {
@Get()
sample(
@I18n() i18n: I18nContext
) {
i18n.translate('HELLO_MESSAGE', {args: {id: 1, username: 'Toon'}})
i18n.translate('SETUP.WELCOME', {args: {id: 1, username: 'Toon'}});
i18n.translate('ARRAY.0');
}
}
```
No need to handle `lang` manually.
#### `I18nRequestScopeService` within a custom service using request scoped translation service
```typescript
@Injectable()
export class SampleService {
constructor(private readonly i18n: I18nRequestScopeService) {}
doFancyStuff() {
this.i18n.translate('HELLO_MESSAGE', {args: {id: 1, username: 'Toon'}})
this.i18n.translate('SETUP.WELCOME', {args: {id: 1, username: 'Toon'}});
this.i18n.translate('ARRAY.0');
}
}
```
To be use within other services like sending E-mails.
The advantage is that you don't have to worry about transporting `lang` from `Request` to your service.
**Use with caution!** The `I18nRequestScopeService` uses `REQUEST` scope and is no singleton.
This will be inherited to all consumers of `I18nRequestScopeService`!
Read [Nest Docs](https://docs.nestjs.com/fundamentals/injection-scopes) for more information.
**Dont use `I18nRequestScopeService` within controllers.** `I18n` decorator is a much better solution.
### Missing Translations

@@ -138,0 +180,0 @@ If you require a translation that is missing, `I18n` will log an error. However, you can also write these missing translations to a new file in order to help translating your application later on.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc