Skip to content

Commit

Permalink
fix issue #152
Browse files Browse the repository at this point in the history
  • Loading branch information
toonvanstrijp committed Jul 20, 2020
1 parent 3c3861b commit 60fe9af
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 29 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nestjs-i18n",
"version": "8.0.6",
"version": "8.0.8",
"description": "",
"author": "Toon van Strijp",
"license": "MIT",
Expand Down
24 changes: 1 addition & 23 deletions src/lib/interceptors/i18n-language.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,34 +51,12 @@ export class I18nLanguageInterceptor implements NestInterceptor {
}
}

ctx.i18nLang = language;
ctx.i18nLang = language || this.i18nOptions.fallbackLanguage;
}

return next.handle();
}

async use(req: any, res: any, next: () => void) {
let language = null;

req.i18nService = this.i18nService;
for (const r of this.i18nResolvers) {
const resolver = await this.getResolver(r);

language = resolver.resolve(req);

if (language instanceof Promise) {
language = await (language as Promise<string>);
}

if (language !== undefined) {
break;
}
}
req.i18nLang = language || this.i18nOptions.fallbackLanguage;

next();
}

private async getResolver(r: I18nOptionResolver): Promise<I18nResolver> {
if (shouldResolve(r)) {
if (r.hasOwnProperty('use') && r.hasOwnProperty('options')) {
Expand Down
9 changes: 4 additions & 5 deletions src/lib/services/i18n.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class I18nService {
private readonly languagesSubject: BehaviorSubject<string[]>,
@Inject(I18N_TRANSLATIONS_SUBJECT)
private readonly translationsSubject: BehaviorSubject<I18nTranslation>,
) { }
) {}

public async translate(
key: string,
Expand Down Expand Up @@ -117,10 +117,9 @@ export class I18nService {
private async handleFallbacks(lang: string) {
const supportedLanguages = await this.getSupportedLanguages();
if (this.i18nOptions.fallbacks && !supportedLanguages.includes(lang)) {
const sanitizedLang =
lang.includes('-')
? lang.substring(0, lang.indexOf('-')).concat('-*')
: lang;
const sanitizedLang = lang.includes('-')
? lang.substring(0, lang.indexOf('-')).concat('-*')
: lang;

for (const key in this.i18nOptions.fallbacks) {
if (key === lang || key === sanitizedLang) {
Expand Down

0 comments on commit 60fe9af

Please sign in to comment.