|
| 1 | +import { isPlatformBrowser } from '@angular/common'; |
| 2 | +import { HttpClient } from '@angular/common/http'; |
| 3 | +import { |
| 4 | + Inject, |
| 5 | + Injectable, |
| 6 | + PLATFORM_ID, |
| 7 | +} from '@angular/core'; |
| 8 | +import { |
| 9 | + firstValueFrom, |
| 10 | + of, |
| 11 | +} from 'rxjs'; |
| 12 | +import { |
| 13 | + catchError, |
| 14 | + map, |
| 15 | +} from 'rxjs/operators'; |
| 16 | + |
| 17 | +import { LocaleService } from '../core/locale/locale.service'; |
| 18 | +import { |
| 19 | + HTML_SUFFIX, |
| 20 | + STATIC_FILES_PROJECT_PATH, |
| 21 | +} from '../static-page/static-page-routing-paths'; |
| 22 | + |
| 23 | +interface HtmlContentResult { |
| 24 | + found: boolean; |
| 25 | + body: string; |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Service for loading static `.html` files stored in the `/static-files` folder. |
| 30 | + */ |
| 31 | +@Injectable({ |
| 32 | + providedIn: 'root', |
| 33 | +}) |
| 34 | +export class HtmlContentService { |
| 35 | + constructor( |
| 36 | + private http: HttpClient, |
| 37 | + private localeService: LocaleService, |
| 38 | + @Inject(PLATFORM_ID) private platformId: object, |
| 39 | + ) {} |
| 40 | + |
| 41 | + private withSuffix(name: string): string { |
| 42 | + return name.endsWith(HTML_SUFFIX) ? name : name + HTML_SUFFIX; |
| 43 | + } |
| 44 | + |
| 45 | + private fetch(url: string) { |
| 46 | + return this.http.get(url, { responseType: 'text' }).pipe( |
| 47 | + map((body): HtmlContentResult => ({ found: true, body })), |
| 48 | + catchError(() => of<HtmlContentResult>({ found: false, body: '' })), |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Load the html content for a file name, trying the current locale package first |
| 54 | + * (`static-files/<lang>/<file>.html`) and falling back to the default package |
| 55 | + * (`static-files/<file>.html`). Returns `undefined` when nothing was found. |
| 56 | + * |
| 57 | + * The files are fetched client-side only; during SSR this resolves to `undefined` |
| 58 | + * and the content is loaded after hydration. |
| 59 | + */ |
| 60 | + async getHtmlContentByPathAndLocale(fileName: string): Promise<string | undefined> { |
| 61 | + if (!isPlatformBrowser(this.platformId)) { |
| 62 | + return undefined; |
| 63 | + } |
| 64 | + |
| 65 | + let language = await firstValueFrom(this.localeService.getCurrentLanguageCode()); |
| 66 | + // Default language `en` lives in the non-translated (root) package. |
| 67 | + language = language === 'en' ? '' : language; |
| 68 | + |
| 69 | + if (language) { |
| 70 | + const localized = await firstValueFrom( |
| 71 | + this.fetch(this.withSuffix(`${STATIC_FILES_PROJECT_PATH}/${language}/${fileName}`)), |
| 72 | + ); |
| 73 | + if (localized.found) { |
| 74 | + return localized.body; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + const fallback = await firstValueFrom( |
| 79 | + this.fetch(this.withSuffix(`${STATIC_FILES_PROJECT_PATH}/${fileName}`)), |
| 80 | + ); |
| 81 | + return fallback.found ? fallback.body : undefined; |
| 82 | + } |
| 83 | +} |
0 commit comments