I am developing an app that offers courses in both English and French. My goal is to create a language dropdown header component that will be visible across all pages of the program. When a user selects a different language from the dropdown, I want the following actions to take place:
- The frontend should call the backend to update the user's language preference so that the next time they visit, they will see the courses in their chosen language.
- Regardless of the current page, the route should change to the course-list page and load the courses according to the updated language preference.
Currently, this functionality only works after refreshing the page and not immediately. It worked when I included the language button in every component.
EDIT: The language switch is functioning on all pages except for the course-list page, possibly because we are already within this component and don't trigger the `getCoursesByLanguage` function (when navigating from other components, I use router.navigate to the course-list component which triggers `getCoursesByLanguage`). How can I make it work on the course-list page?
This is the relevant code:
app.component.html
<div class='container-fluid' id='main'>
<lg-header></lg-header>
<router-outlet></router-outlet>
</div>
header.component.html
<div style="float:right; padding-right:30px">
<button id="button-logout" mat-button (click)="toggleLanguage()">
<img width="27" height="17" style="margin-right: 10px;" src="./assets/images/{{flag}}_Flag.png"/>
<span>{{languageName}}</span>
</button>
</div>
header.component.ts
import { Component, OnInit, Pipe, PipeTransform } from '@angular/core';
import { ActivatedRoute, Router, Routes } from '@angular/router';
import { LocalStorage } from '@ngx-pwa/local-storage';
import { IUser, IUserCourses } from '../users/user';
import { UserProgressService } from '../users/user-progress.service';
@Component({
selector: 'lg-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.sass']
})
export class HeaderComponent implements OnInit {
// variables for laungage
language: number;
languageName: string;
flag: string;
constructor(private route: ActivatedRoute,
private router: Router,
private userProgressService: UserProgressService) {
userProgressService.connectUser();
this.getUpdatedLanguageAndFlag();
}
// get from service updated data from backend and localStorage
getUpdatedLanguageAndFlag() {
this.language = this.userProgressService.getLanguage();
this.flag = this.userProgressService.getFlag();
this.languageName = this.userProgressService.getLanguageName();
}
ngOnInit() { }
// change laungage
toggleLanguage(){
this.userProgressService.changeAppLanguage();
this.getUpdatedLanguageAndFlag();
if (this.router.url == '/courses') {
// I need to trigger here getCourseListByLanguage in course-list from here
}
else
this.router.navigate(["/courses"]);
}
}
user-progress.ts
import { Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError, groupBy } from 'rxjs/operators';
import { LocalStorage } from '@ngx-pwa/local-storage';
import { UserService } from './user.service';
import { IUser, IUserCourses } from './user';
@Injectable({
providedIn: 'root'
})
export class UserProgressService {
private user: IUser;
private errorMessage: string;
private language: number;
private flag: string;
private languageName: string;
constructor(private userService: UserService) { }
// get user from local store
connectUser() {
this.user = JSON.parse(localStorage.getItem('user'));
this.language =+ localStorage.getItem('language');
this.flag = localStorage.getItem('flag');
this.languageName = localStorage.getItem('languageName');
}
...