Skip to content

Commit

Permalink
fix: add a note to add generated file to eslintignore
Browse files Browse the repository at this point in the history
  • Loading branch information
rubiin committed Aug 17, 2023
1 parent 8e8e937 commit a7d641a
Show file tree
Hide file tree
Showing 16 changed files with 2,834 additions and 2,323 deletions.
6 changes: 3 additions & 3 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Copyright
Copyright

(c) 2022, Toon van Strijp
(c) 2023, Toon van Strijp

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.
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.
2 changes: 2 additions & 0 deletions docs/guides/type-safety.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import { I18nModule } from 'nestjs-i18n';
export class AppModule {}
```

> The `typesOutputPath` should also be added to `eslintignore` to prevent linting errors.
# Usage

To use the types within your code import the `I18nTranslations` type from the generated file. Pass this type into the generic type properties of the `I18nContext` or `I18nService`.
Expand Down
4,885 changes: 2,696 additions & 2,189 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 7 additions & 3 deletions samples/dto-validation/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AcceptLanguageResolver, I18nModule, QueryResolver , HeaderResolver} from 'nestjs-i18n';
import {
AcceptLanguageResolver,
I18nModule,
QueryResolver,
HeaderResolver,
} from 'nestjs-i18n';
import { join } from 'path';

@Module({
Expand All @@ -15,10 +20,9 @@ import { join } from 'path';
resolvers: [
{ use: QueryResolver, options: ['lang'] },
AcceptLanguageResolver,
new HeaderResolver(['x-lang'])
new HeaderResolver(['x-lang']),
],
}),

],
controllers: [AppController],
providers: [AppService],
Expand Down
2 changes: 1 addition & 1 deletion samples/dto-validation/src/app.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return "hello world";
return 'hello world';
}
}
7 changes: 1 addition & 6 deletions samples/dto-validation/src/dtos/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@

import {
IsEmail,
IsNotEmpty,
} from 'class-validator';
import { IsEmail, IsNotEmpty } from 'class-validator';

export class CreateUserDto {
@IsNotEmpty({ message: 'validation.NOT_EMPTY' })
Expand All @@ -11,5 +7,4 @@ export class CreateUserDto {

@IsNotEmpty({ message: 'validation.NOT_EMPTY' })
password: string;

}
11 changes: 6 additions & 5 deletions samples/dto-validation/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { I18nValidationExceptionFilter, I18nValidationPipe } from 'nestjs-i18n';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(
new I18nValidationPipe())
app.useGlobalPipes(new I18nValidationPipe());

app.useGlobalFilters(new I18nValidationExceptionFilter({
detailedErrors: false,
}))
app.useGlobalFilters(
new I18nValidationExceptionFilter({
detailedErrors: false,
}),
);

await app.listen(3000);
}
Expand Down
10 changes: 7 additions & 3 deletions samples/simple/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AcceptLanguageResolver, I18nModule, QueryResolver , HeaderResolver} from 'nestjs-i18n';
import {
AcceptLanguageResolver,
I18nModule,
QueryResolver,
HeaderResolver,
} from 'nestjs-i18n';
import { join } from 'path';

@Module({
Expand All @@ -15,10 +20,9 @@ import { join } from 'path';
resolvers: [
{ use: QueryResolver, options: ['lang'] },
AcceptLanguageResolver,
new HeaderResolver(['x-lang'])
new HeaderResolver(['x-lang']),
],
}),

],
controllers: [AppController],
providers: [AppService],
Expand Down
3 changes: 1 addition & 2 deletions samples/simple/src/app.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Injectable } from '@nestjs/common';
import { I18nContext, I18nService } from 'nestjs-i18n';


@Injectable()
export class AppService {
constructor(private readonly i18n: I18nService) {}
getHello(): string {
return this.i18n.t('test.HELLO',{ lang: I18nContext.current().lang });
return this.i18n.t('test.HELLO', { lang: I18nContext.current().lang });
}
}
11 changes: 6 additions & 5 deletions samples/simple/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import { I18nValidationExceptionFilter, I18nValidationPipe } from 'nestjs-i18n';

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(
new I18nValidationPipe())
app.useGlobalPipes(new I18nValidationPipe());

app.useGlobalFilters(new I18nValidationExceptionFilter({
detailedErrors: false,
}))
app.useGlobalFilters(
new I18nValidationExceptionFilter({
detailedErrors: false,
}),
);

await app.listen(3000);
}
Expand Down
90 changes: 46 additions & 44 deletions src/i18n.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,7 @@ import {
import { I18nLanguageInterceptor } from './interceptors/i18n-language.interceptor';
import { APP_INTERCEPTOR, HttpAdapterHost } from '@nestjs/core';
import { getI18nResolverOptionsToken } from './decorators/i18n-resolver-options.decorator';
import {
isNestMiddleware,
shouldResolve,
usingFastify,
} from './utils/util';
import { isNestMiddleware, shouldResolve, usingFastify } from './utils/util';
import { I18nTranslation } from './interfaces/i18n-translation.interface';
import { I18nLoader } from './loaders/i18n.loader';
import { Observable, BehaviorSubject, Subject, takeUntil } from 'rxjs';
Expand All @@ -47,6 +43,7 @@ import { I18nMiddleware } from './middlewares/i18n.middleware';
import { mergeDeep } from './utils/merge';
import * as fs from 'fs';
import * as path from 'path';
import * as chalk from 'chalk';

export const logger = new Logger('I18nService');

Expand Down Expand Up @@ -92,46 +89,51 @@ export class I18nModule implements OnModuleInit, OnModuleDestroy, NestModule {
};
}



if (!!this.i18nOptions.typesOutputPath) {
try {
const ts = await import('./utils/typescript');

this.translations.pipe(takeUntil(this.unsubscribe)).subscribe(async (t) => {
logger.log('Checking translation changes');
const object = Object.keys(t).reduce(
(result, key) => mergeDeep(result, t[key]),
{},
);

const rawContent = await ts.createTypesFile(object);

if (!rawContent) {
return;
}

const outputFile = ts.annotateSourceCode(rawContent);

fs.mkdirSync(path.dirname(this.i18nOptions.typesOutputPath), {
recursive: true,
});
let currentFileContent = null;
try {
currentFileContent = fs.readFileSync(
this.i18nOptions.typesOutputPath,
'utf8',
);
} catch (err) {
logger.error(err);
}
if (currentFileContent != outputFile) {
fs.writeFileSync(this.i18nOptions.typesOutputPath, outputFile);
logger.log(`Types generated in: ${this.i18nOptions.typesOutputPath}`);
} else {
logger.log('No changes detected');
}
});
try {
const ts = await import('./utils/typescript');

this.translations
.pipe(takeUntil(this.unsubscribe))
.subscribe(async (t) => {
logger.log('Checking translation changes');
const object = Object.keys(t).reduce(
(result, key) => mergeDeep(result, t[key]),
{},
);

const rawContent = await ts.createTypesFile(object);

if (!rawContent) {
return;
}

const outputFile = ts.annotateSourceCode(rawContent);

fs.mkdirSync(path.dirname(this.i18nOptions.typesOutputPath), {
recursive: true,
});
let currentFileContent = null;
try {
currentFileContent = fs.readFileSync(
this.i18nOptions.typesOutputPath,
'utf8',
);
} catch (err) {
logger.error(err);
}
if (currentFileContent != outputFile) {
fs.writeFileSync(this.i18nOptions.typesOutputPath, outputFile);
logger.log(
`Types generated in: ${this.i18nOptions.typesOutputPath}.
${chalk.yellow(
`Please also add it to your eslintignore file to avoid linting errors`,
)}`,
);
} else {
logger.log('No changes detected');
}
});
} catch (_) {
// NOOP: typescript package not found
}
Expand Down
14 changes: 5 additions & 9 deletions src/loaders/i18n.json.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,11 @@ export class I18nJsonLoader extends I18nAbstractLoader {
};
}
formatData(data: any) {

try{
return JSON.parse(data);
}
catch(e){
if(e instanceof SyntaxError){
throw new Error(
'Invalid JSON file. Please check your JSON syntax.'
);
try {
return JSON.parse(data);
} catch (e) {
if (e instanceof SyntaxError) {
throw new Error('Invalid JSON file. Please check your JSON syntax.');
}
throw e;
}
Expand Down
14 changes: 5 additions & 9 deletions src/loaders/i18n.yaml.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
import * as yaml from 'js-yaml';

export class I18nYamlLoader extends I18nAbstractLoader {

getDefaultOptions(): Partial<I18nAbstractLoaderOptions> {
return {
filePattern: '*.yml',
Expand All @@ -14,14 +13,11 @@ export class I18nYamlLoader extends I18nAbstractLoader {
}

formatData(data: any) {
try{
return yaml.load(data, { json: true });
}
catch(e){
if(e instanceof yaml.YAMLException){
throw new Error(
'Invalid YAML file. Please check your YAML syntax.'
);
try {
return yaml.load(data, { json: true });
} catch (e) {
if (e instanceof yaml.YAMLException) {
throw new Error('Invalid YAML file. Please check your YAML syntax.');
}

throw e;
Expand Down
32 changes: 17 additions & 15 deletions src/services/i18n.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export type TranslateOptions = {

@Injectable()
export class I18nService<K = Record<string, unknown>>
implements I18nTranslator<K>, OnModuleDestroy {
implements I18nTranslator<K>, OnModuleDestroy
{
private supportedLanguages: string[];
private translations: I18nTranslation;
private pluralRules = new Map<string, Intl.PluralRules>();
Expand Down Expand Up @@ -106,8 +107,9 @@ export class I18nService<K = Record<string, unknown>>
) {
if (lang !== this.i18nOptions.fallbackLanguage || !!defaultValue) {
if (this.i18nOptions.logging) {
const message = `Translation "${key as string
}" in "${lang}" does not exist.`;
const message = `Translation "${
key as string
}" in "${lang}" does not exist.`;
this.logger.error(message);
}

Expand All @@ -126,16 +128,16 @@ export class I18nService<K = Record<string, unknown>>
}

private getFallbackLanguage(lang: string) {
let regionSepIndex =-1
let regionSepIndex = -1;

if(lang.includes("-")){
if (lang.includes('-')) {
regionSepIndex = lang.lastIndexOf('-');
}

if (lang.includes("_")) {
if (lang.includes('_')) {
regionSepIndex = lang.lastIndexOf('_');
}

return regionSepIndex !== -1
? lang.slice(0, regionSepIndex)
: this.i18nOptions.fallbackLanguage;
Expand Down Expand Up @@ -270,14 +272,14 @@ export class I18nService<K = Record<string, unknown>>
for (const nestedTranslation of nestedTranslations) {
const result = rootTranslations
? (this.translateObject(
nestedTranslation.key,
rootTranslations,
lang,
{
...options,
args: { parent: options.args, ...nestedTranslation.args },
},
) as string) ?? ''
nestedTranslation.key,
rootTranslations,
lang,
{
...options,
args: { parent: options.args, ...nestedTranslation.args },
},
) as string) ?? ''
: '';
translation =
translation.substring(0, nestedTranslation.index - offset) +
Expand Down
Loading

0 comments on commit a7d641a

Please sign in to comment.