Giter VIP home page Giter VIP logo

Comments (3)

meeroslav avatar meeroslav commented on August 20, 2024 1

Hi @brada1703 ,

in the first code example you have both routerLink and (click). Most likely your route gets translated with changeLang but then navigation via routerLink kicks in and redirects you to home.
Try to remove all routerLink bits and leave just click:

<a (click)="changeLang('en')">English</a>
<a (click)="changeLang('it')">Italiano</a>

If you need to mark active language button you can use ngx-translate's TranslateService or localize-router LocalizeService's parser to get current language and simply use ngClass to mark it.

from localize-router.

brada1703 avatar brada1703 commented on August 20, 2024

Thanks again @meeroslav You're the best! 👍

You're absolutely right. The routerLink was the problem. It kept bringing me back to the homepage. Once I removed it, I realized that the problem then was the "changeLang" part.

In case anyone else is having a problem with this, let me show you what I changed.

I realized that I was importing
import { TranslateService } from '@ngx-translate/core';
instead of
import { LocalizeRouterService } from 'localize-router';

The problem here is that with @ngx-translate/core, they want to see

changeLang(lang:string){
this.translate.use(lang);

but in localize-router, your syntax is slightly different

  changeLanguage(lang: string) {
    this.localize.changeLanguage(lang);
  }

So, I had to change the imports to 'localize-router', the constructor to

  constructor(private localize: LocalizeRouterService) {}

  changeLanguage(lang: string) {
    this.localize.changeLanguage(lang);
  }

and finally change the on click event to
(click)="changeLanguage('it')"

Now it works like a charm! 👍

from localize-router.

AttaHo avatar AttaHo commented on August 20, 2024

I used (click)="changeLanguage('en')" and the strings are translated but the url does not update accordingly.

I am also getting Uncaught (in promise): TypeError: Cannot read property 'component' of null when I change to another language.

Did anyone else have this problem? Please help.

from localize-router.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.