Skip to content

Commit ca7cccf

Browse files
committed
feat(core): compare user language to current locale in log in and my account pages
1 parent 0933a5f commit ca7cccf

4 files changed

Lines changed: 17 additions & 19 deletions

File tree

src/app/core/components/error-404/error-404.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ <h1 class="first-heading__title" i18n>Error 404: A Wild Error Appeared</h1>
99
i18n-alt
1010
height="690"
1111
width="588"
12+
priority
1213
/>
1314
</div>
1415
</main>

src/app/core/services/language.service.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { inject, Injectable, LOCALE_ID } from '@angular/core';
2-
import { DEFAULT_LANGUAGE } from '~core/constants/language.constants';
32
import { Router } from '@angular/router';
43
import { Language } from '~core/enums/language.enum';
54
import { Locale } from '~core/enums/locale.enum';
@@ -18,17 +17,24 @@ export class LanguageService {
1817
return Language.EN_US;
1918
}
2019

21-
navigateWithUserLanguage(userLanguage: string, path: string) {
22-
const localeToRedirect = this.getLocaleFromUserLanguage(userLanguage);
23-
if (userLanguage === this.localeId || userLanguage === (DEFAULT_LANGUAGE as string)) {
24-
void this.router.navigate([path]);
20+
navigateWithUserLanguage(language: Language, pathToRedirect: string) {
21+
const localeToRedirect = this.getLocaleFromLanguage(language);
22+
if (this.doesLocaleMatchLanguage(language)) {
23+
void this.router.navigate([pathToRedirect]);
2524
} else {
26-
window.location.href = `/${localeToRedirect}${path}`;
25+
window.location.href = `/${localeToRedirect}${pathToRedirect}`;
2726
}
2827
}
2928

30-
private getLocaleFromUserLanguage(userLanguage: string) {
31-
if (userLanguage === (Language.ES_ES as string)) {
29+
private doesLocaleMatchLanguage(language: Language) {
30+
if (this.localeId === (Locale.ES as string)) {
31+
return language === Language.ES_ES;
32+
}
33+
return language === Language.EN_US;
34+
}
35+
36+
private getLocaleFromLanguage(language: Language) {
37+
if (language === Language.ES_ES) {
3238
return Locale.ES;
3339
}
3440
return Locale.EN;

src/app/features/authentication/pages/log-in/log-in.component.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,7 @@ export class LogInComponent {
7171
next: (user: User) => {
7272
this.isButtonLogInLoading = false;
7373
this.changeDetectorRef.markForCheck();
74-
this.languageService.navigateWithUserLanguage(
75-
user.language as string,
76-
ROOT_URLS.myPokedex,
77-
);
74+
this.languageService.navigateWithUserLanguage(user.language, ROOT_URLS.myPokedex);
7875
},
7976
error: (response) => {
8077
this.isButtonLogInLoading = false;

src/app/features/authentication/pages/my-account/my-account.component.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,7 @@ export class MyAccountComponent implements OnInit {
130130
this.isButtonUpdateUserFormLoading = false;
131131
this.alertService.createSuccessAlert(translations.myAccountSuccessAlert);
132132
this.changeDetectorRef.markForCheck();
133-
134-
if (this.user?.language !== formValue.language) {
135-
this.languageService.navigateWithUserLanguage(
136-
formValue.language as string,
137-
AUTH_URLS.myAccount,
138-
);
139-
}
133+
this.languageService.navigateWithUserLanguage(formValue.language!, AUTH_URLS.myAccount);
140134
},
141135
error: () => {
142136
this.isButtonUpdateUserFormLoading = false;

0 commit comments

Comments
 (0)