Skip to content

Commit

Permalink
v0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Rmannn committed Nov 2, 2020
1 parent 4610465 commit d314695
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 11 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Pop-Code

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "nestjs-i18next",
"version": "0.0.2",
"version": "0.0.3",
"description": "The nestjs translator modulde",
"author": "Alex Hermann <alex.hermann@pop-code.com>",
"repository": "https://github.com/Pop-Code/nestjs-i18next.git",
"private": true,
"main": "dist/index.js",
"license": "MIT",
"scripts": {
"build": "rm -Rf ./dist && tsc -b",
"doc": "rm -Rf ./docs && typedoc ./src && touch ./docs/.nojekyll",
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const TRANSLATOR_FUNCTION_TOKEN = 'nestjs-i18next:TranslatorFunction';
export const CONFIG_TOKEN = 'nestjs-i18next:Config';
4 changes: 3 additions & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { TRANSLATOR_FUNCTION_TOKEN } from './constants';
import { CONFIG_TOKEN, TRANSLATOR_FUNCTION_TOKEN } from './constants';

export const getTranslatorFunctionToken = (): string => TRANSLATOR_FUNCTION_TOKEN;

export const getConfigToken = (): string => CONFIG_TOKEN;
7 changes: 7 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ModuleMetadata } from '@nestjs/common';
import { InitOptions } from 'i18next';

export interface AsyncInitOptions extends Pick<ModuleMetadata, 'imports'> {
useFactory: (...args: any[]) => Promise<InitOptions> | InitOptions;
inject?: any[];
}
20 changes: 12 additions & 8 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { DynamicModule, Module } from '@nestjs/common';
import { FactoryProvider } from '@nestjs/common/interfaces';
import i18next, { InitOptions } from 'i18next';

import { getTranslatorFunctionToken } from './helpers';
import { getConfigToken, getTranslatorFunctionToken } from './helpers';
import { AsyncInitOptions } from './interfaces';
import { TranslatorService } from './service';

@Module({})
export class TranslatorModule {
static forRootAsync(optionsFactory?: FactoryProvider<InitOptions>): DynamicModule {
static forRootAsync(options?: AsyncInitOptions): DynamicModule {
const configProvider = {
provide: getConfigToken(),
...options
};

const translatorProvider = {
provide: getTranslatorFunctionToken(),
inject: optionsFactory ? [optionsFactory.provide] : [],
inject: [configProvider],
useFactory: async (options: InitOptions) => {
i18next.init({
whitelist: options.resources ? Object.keys(options.resources) : [],
Expand All @@ -19,6 +24,7 @@ export class TranslatorModule {
lng: 'en-US',
...options
});
return i18next;
}
};

Expand All @@ -27,14 +33,12 @@ export class TranslatorModule {
useValue: i18next
};

const providers: any[] = [i18nextProvider, translatorProvider, TranslatorService];

if (optionsFactory) providers.push(optionsFactory);
const providers: any[] = [configProvider, i18nextProvider, translatorProvider, TranslatorService];

return {
module: TranslatorModule,
providers: providers,
exports: [i18nextProvider, translatorProvider, TranslatorService]
exports: [configProvider, i18nextProvider, TranslatorService]
};
}
}

0 comments on commit d314695

Please sign in to comment.