Skip to content

Feature(transloco): Routes localization #669

Open
@SvenBudak

Description

@SvenBudak

Is there an existing issue for this?

  • I have searched the existing issues

Which Transloco package(s) will this feature affect?

Transloco, Persist Lang

Is your feature request related to a problem? Please describe

I want to add the current Lang to the route of my website like: domain.com/currentLang/blabla.

My idea was now to write a guard like this:

export class LanguageGuard {
  constructor(
    private translocoService: TranslocoService,
    private router: Router
  ) {}

  canActivate(): (
    route: ActivatedRouteSnapshot,
    state: RouterStateSnapshot
  ) => boolean | UrlTree {
    return (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
      const currentLang = this.translocoService.getActiveLang();
      if (!state.url.startsWith(`/${currentLang}`)) {
        return this.router.parseUrl(`/${currentLang}${state.url}`);
      }
      return true;
    };
  }
}

And have a router like this:

export const appRoutes: Route[] = [
  {
    path: ':lang',
    canActivate: [() => inject(LanguageGuard).canActivate()],
    loadComponent: () =>
      import('./frames/frame-default/frame-default.component').then(
        (m) => m.FrameDefaultComponent
      ),
    children: [
      {
        path: '',
        loadComponent: () =>
          import('./views/view-home/view-home.component').then(
            (m) => m.ViewHomeComponent
          ),
      },
    ],
  },
  { path: '**', redirectTo: '/' },
];

And now i need to add the currentLang somehow to ALL of my routerLinks in my entire App:

<li><a [routerLink]="['/', currentLang, 'gallery']">{{ 'common.gallery' | transloco }}</a></li>

Because i dont want to inject in all my 1000 components the currentLang like

currentLang = this.translocoService.getActiveLang();

i thinked about using a global state manager. so i found Elf. Because i use the Persist module for transloco: https://jsverse.github.io/transloco/docs/plugins/persist-lang/ i ask my self now, if it makes maybe sense to ask you, to add Elf support for it? Something like useValue: Elf

Describe the solution you'd like

No response

Describe alternatives you've considered

And... I asked a year or two ago about a module that automatically prefixes the current language to every route. At that time, this request was rejected on the grounds that anyone could program this themselves with a few lines of code. However, I find this to be extremely cumbersome. Therefore, I would like to take this opportunity to ask again if you have changed your mind and would like to implement this feature at some point? To make all the problems mentioned here a thing of the past?

Additional context

No response

I would like to make a pull request for this feature

No

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions