Skip to content

Feature(scope): Route Title Resolver #839

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
Doublon opened this issue Mar 1, 2025 · 2 comments
Open
1 task done

Feature(scope): Route Title Resolver #839

Doublon opened this issue Mar 1, 2025 · 2 comments

Comments

@Doublon
Copy link

Doublon commented Mar 1, 2025

Is there an existing issue for this?

  • I have searched the existing issues

Which Transloco package(s) will this feature affect?

Don't know / other

Is your feature request related to a problem? Please describe

Hi everyone,

I'm looking for a way to translate every tab title without duplicating the code. To solve this, I would like to create a resolver such as:

export const titleTranslationResolver: ResolveFn<string> = (route, state) => {
  console.log("titleTranslationResolver")
  if (!route.data["titleKey"]) {
    return Promise.resolve('Default title');
  }

  const transloco = inject(TranslocoService);
  return Promise.resolve(transloco.translate(route.data["titleKey"]));
};

and use it in my router:

export const routes: Routes = [
  {
    path: 'participants',
    title: titleTranslationResolver,
    pathMatch: 'full',
    loadComponent: () => import('@presentation/pages/participants-page/participants-page.component').then(m => m.ParticipantsPageComponent),
    canActivate: [ConnectedGuard],
    data: { titleKey: 'participant_list' }
  },
  { path: '',   redirectTo: 'participants', pathMatch: 'full' },
  {
    path: '**',
    title: titleTranslationResolver,
    loadComponent: () => import('@presentation/pages/not-found-page/not-found-page.component').then(m => m.NotFoundPageComponent)
  }
];

The issue with this solution is that Transloco is initialized after my routes, even with this code:

export const appConfig: ApplicationConfig = {
  providers: [
    provideHttpClient(),
    provideTransloco({
      config: {
        availableLangs: ['en'],
        defaultLang: 'en',
        // Remove this option if your application doesn't support changing language in runtime.
        reRenderOnLangChange: true,
        prodMode: !isDevMode(),
      },
      loader: TranslocoHttpLoader
    }),
    provideRouter(routes),
  ]
};

Image

Describe the solution you'd like

Maybe I did something wrong, but here are two potential solutions:

  • Use a native Transloco resolver for translating tabs.
  • Allow the TranslocoHttpLoader to initialize before the router.

Describe alternatives you've considered

For now I translate in every component "page" in the ngOnInit method

Additional context

No response

I would like to make a pull request for this feature

No

@windmichael
Copy link

Instead of translating the key via the resolver, I would recommend using a TitleStrategy.

For your use-case the TitleStrategy could look like this:

import { Injectable, inject, signal, effect } from "@angular/core";
import { Title } from "@angular/platform-browser";
import { TitleStrategy, RouterStateSnapshot } from "@angular/router";
import { TranslocoService, translateSignal } from "@jsverse/transloco";

@Injectable({ providedIn: 'root' })
export class TemplatePageTitleStrategy extends TitleStrategy {
  private readonly title = inject(Title);
  private readonly transloco = inject(TranslocoService);

  private readonly activeTitle = signal('');
  private readonly translatedTitle = translateSignal(this.activeTitle);

  constructor() {
    super();
    effect(() => this.title.setTitle(this.translatedTitle()));
  }

  override updateTitle(routerState: RouterStateSnapshot) {
    const title = this.buildTitle(routerState);
    if (title) {
      this.activeTitle.set(title);
    }
  }
}

Then you need to provide your own TitleStrategy in the app-config or AppModule if you still use NgModules:

export const appConfig: ApplicationConfig = {
  providers: [
    ...
    { provide: TitleStrategy, useClass: TemplatePageTitleStrategy },
    ...
  ],
};

Finally, in your routes you can set the key as title:

export const ROUTES: Routes = [
  {
    path: 'home',
    component: HomeComponent,
    title: 'key.home'
  },
];

Hope this helps solving your problem

@Doublon
Copy link
Author

Doublon commented May 19, 2025

Ok, I will tri it !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants