feat(front-end): ssr i18n fix
All checks were successful
Build and Deploy / test-backend (push) Successful in 26s
Build and Deploy / test-frontend (push) Successful in 1m3s
Build and Deploy / build-and-push (push) Successful in 30s
Build and Deploy / deploy (push) Successful in 19s

This commit is contained in:
2026-03-11 17:19:26 +01:00
parent 3ac3262e77
commit 6a22c54e9f

View File

@@ -1,17 +1,25 @@
import { import {
ApplicationConfig, ApplicationConfig,
provideAppInitializer,
provideZoneChangeDetection, provideZoneChangeDetection,
importProvidersFrom, importProvidersFrom,
inject,
REQUEST,
} from '@angular/core'; } from '@angular/core';
import { import {
provideRouter, provideRouter,
withComponentInputBinding, withComponentInputBinding,
withInMemoryScrolling, withInMemoryScrolling,
withViewTransitions, withViewTransitions,
Router,
} from '@angular/router'; } from '@angular/router';
import { routes } from './app.routes'; import { routes } from './app.routes';
import { provideHttpClient, withInterceptors } from '@angular/common/http'; import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core'; import {
TranslateLoader,
TranslateModule,
TranslateService,
} from '@ngx-translate/core';
import { import {
provideTranslateHttpLoader, provideTranslateHttpLoader,
TranslateHttpLoader, TranslateHttpLoader,
@@ -22,6 +30,22 @@ import {
withEventReplay, withEventReplay,
} from '@angular/platform-browser'; } from '@angular/platform-browser';
import { serverOriginInterceptor } from './core/interceptors/server-origin.interceptor'; import { serverOriginInterceptor } from './core/interceptors/server-origin.interceptor';
import { catchError, firstValueFrom, of } from 'rxjs';
type SupportedLang = 'it' | 'en' | 'de' | 'fr';
const SUPPORTED_LANGS: readonly SupportedLang[] = ['it', 'en', 'de', 'fr'];
function resolveLangFromUrl(url: string): SupportedLang {
const firstSegment = (url || '/')
.split('?')[0]
.split('#')[0]
.split('/')
.filter(Boolean)[0]
?.toLowerCase();
return SUPPORTED_LANGS.includes(firstSegment as SupportedLang)
? (firstSegment as SupportedLang)
: 'it';
}
export const appConfig: ApplicationConfig = { export const appConfig: ApplicationConfig = {
providers: [ providers: [
@@ -50,6 +74,32 @@ export const appConfig: ApplicationConfig = {
}, },
}), }),
), ),
provideAppInitializer(() => {
const translate = inject(TranslateService);
const router = inject(Router);
const request = inject(REQUEST, { optional: true }) as
| { url?: string }
| null;
translate.addLangs([...SUPPORTED_LANGS]);
translate.setDefaultLang('it');
const requestedUrl =
(typeof request?.url === 'string' && request.url) || router.url || '/';
const lang = resolveLangFromUrl(requestedUrl);
return firstValueFrom(
translate.use(lang).pipe(
catchError((error) => {
console.error('[i18n] Failed to preload language for SSR', {
lang,
requestedUrl,
error,
});
return of({});
}),
),
).then(() => undefined);
}),
provideClientHydration(withEventReplay()), provideClientHydration(withEventReplay()),
], ],
}; };