|
| 1 | +// (C) Copyright 2015 Moodle Pty Ltd. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { Injectable } from '@angular/core'; |
| 16 | +import { ActivatedRoute, NavigationEnd } from '@angular/router'; |
| 17 | + |
| 18 | +import { CoreSitesReadingStrategy } from '@services/sites'; |
| 19 | +import { makeSingleton, Router } from '@singletons'; |
| 20 | +import { |
| 21 | + CoreCourses, |
| 22 | + CoreCourseSearchedData, |
| 23 | +} from '../../courses/services/courses'; |
| 24 | +import { CoreNavigator } from '@services/navigator'; |
| 25 | +import { filter } from 'rxjs/operators'; |
| 26 | +import { CorePromiseUtils } from '@singletons/promise-utils'; |
| 27 | +import { CoreLang } from '@services/lang'; |
| 28 | + |
| 29 | +/** |
| 30 | + * Service that provides some features course and module forced languages. |
| 31 | + */ |
| 32 | +@Injectable({ providedIn: 'root' }) |
| 33 | +export class CoreCourseForceLanguageService { |
| 34 | + |
| 35 | + protected lastNavigationCheck: { |
| 36 | + courseId: number; |
| 37 | + courseLang: string | undefined; |
| 38 | + timestamp: number; |
| 39 | + }= { |
| 40 | + courseId: 0, |
| 41 | + courseLang: undefined, |
| 42 | + timestamp: 0, |
| 43 | + }; |
| 44 | + |
| 45 | + /** |
| 46 | + * Initialize. |
| 47 | + */ |
| 48 | + initialize(): void { |
| 49 | + Router.events |
| 50 | + .pipe(filter(event => event instanceof NavigationEnd)) |
| 51 | + .subscribe(async () => { |
| 52 | + this.lastNavigationCheck.timestamp = Date.now(); |
| 53 | + const currentNavigationCheck = this.lastNavigationCheck.timestamp; |
| 54 | + |
| 55 | + let route: ActivatedRoute | null = CoreNavigator.getCurrentRoute(); |
| 56 | + let routeData = CoreNavigator.getRouteData(route); |
| 57 | + while (!routeData.checkForcedLanguage && route) { |
| 58 | + route = route.parent; |
| 59 | + routeData = route ? CoreNavigator.getRouteData(route) : {}; |
| 60 | + } |
| 61 | + |
| 62 | + let lang: string | undefined = undefined; |
| 63 | + if (route && routeData.checkForcedLanguage) { |
| 64 | + lang = await this.getForcedLanguageFromRoute(); |
| 65 | + } |
| 66 | + |
| 67 | + if (currentNavigationCheck === this.lastNavigationCheck.timestamp) { |
| 68 | + await CoreLang.forceCourseLanguage(lang); |
| 69 | + } |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Get forced language from route. |
| 75 | + * |
| 76 | + * @returns Language code if forced. |
| 77 | + */ |
| 78 | + protected async getForcedLanguageFromRoute(): Promise<string | undefined> { |
| 79 | + let course = CoreNavigator.getRouteParam<CoreCourseSearchedData>('course'); |
| 80 | + if (course?.lang !== undefined) { |
| 81 | + this.lastNavigationCheck.courseLang = course.lang; |
| 82 | + this.lastNavigationCheck.courseId = course.id; |
| 83 | + |
| 84 | + return course.lang; |
| 85 | + } |
| 86 | + |
| 87 | + const courseId = CoreNavigator.getRouteNumberParam('courseId'); |
| 88 | + if (!courseId) { |
| 89 | + // Not in a course/module, empty the cache and return. |
| 90 | + this.lastNavigationCheck.courseId = 0; |
| 91 | + this.lastNavigationCheck.courseLang = undefined; |
| 92 | + |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + if (this.lastNavigationCheck.courseId === courseId) { |
| 97 | + return this.lastNavigationCheck.courseLang; |
| 98 | + } |
| 99 | + this.lastNavigationCheck.courseId = courseId; |
| 100 | + |
| 101 | + course = await CorePromiseUtils.ignoreErrors( |
| 102 | + CoreCourses.getCourseByField('id', courseId, { readingStrategy: CoreSitesReadingStrategy.PREFER_CACHE }), |
| 103 | + ); |
| 104 | + |
| 105 | + this.lastNavigationCheck.courseLang = course?.lang; |
| 106 | + |
| 107 | + return course?.lang; |
| 108 | + } |
| 109 | + |
| 110 | +} |
| 111 | +export const CoreCourseForceLanguage = makeSingleton(CoreCourseForceLanguageService); |
0 commit comments