Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

416
Visualizações
Proper way to handle language switching in angular

I've added internationalization (i18n) to my angular app. And I want it to work this way :

  • When the user visits the home component : localhost:4200/ . I detect the locale from localStorage and append it to the existing url on component loading . So that the final url will be something like localhost:4200/detectedLocale (for e.g : localhost:4200/en/)

  • Also I want to implement the switching between languages. So when a user selects a new language from the language selector , the url is changed for e.g from localhost:4200/fr/ to localhost:4200/en/

For that I've used window.location.pathname = '/' + selectedLocale. So that the whole pathname gets overwritten.

This worked fine in devel mode. However I had some issues with the angular app being deployed to another backend server.

The backend server routing is something like the following :

  • /api : for the rest calls (backend stuff)
  • /frontend : for the embedded angular app

I built my angular app using ng build and specified frontend as a base href.

Now when I run the backend server and hit http://host/frontend/ , the home component gets loaded and then I get redirected to http://host/language thing I do not want it to happen.

I guess it is beacause of the window.location.pathname = '/' + selectedLocale thing. (Same when switching between languages).

How can I fix this ? Is there an alternative to the window.location.pathname ? Like a proper way to append the language to the URL in angular with taking consideration of the prefix ( base href ) and without overwriting the whole thing .

Note also that am adding this in my app module :

{ provide: APP_BASE_HREF, useValue: '/' + extractedLanguage },

Any ideas ? Thank you for your help.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda