question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unknown route switching language

See original GitHub issue

Hi @meeroslav integrating your localize-router I found what I believe is an issue, correct me if I am wrong, what I am trying to do is that: when a user click on a new language the page is translated and the navigation is updated to march the new language, in summary the basic idea of this module.

The way the feature is implemented is that: I created 2 buttons and on click I invoke a function on my module that using the LocalizeRouterService#changeLanguage method set the new language.

HTML

<span class="pull-left ">
      <button style="padding-left: 0 !important; " class="nav-link d-inline " (click)="switchLanguage( 'en') ">
          English
      </button>
      <button style="padding-left: 0 !important; " class="nav-link d-inline " (click)="switchLanguage( 'it') ">
          Italian
      </button>
</span>

Component

@Component({
  selector: 'app-nav',
  templateUrl: './nav.component.html',
  styleUrls: ['./nav.component.css']
})
export class NavComponent implements OnInit {

  constructor(private localize: LocalizeRouterService) { }

  ngOnInit() {
    console.log('ROUTES', this.localize.parser.routes);
  }

  switchLanguage(language: string) {
    this.localize.changeLanguage(language);
  }

}

What I observe is this when I click the button the page is translated and the links are updated to the new language as well but when I try to click on any of the links the route is not recognized and the following error is thrown

Error: Cannot match any routes. URL Segment: ‘it/howitworks’

doing some tests I can see that the error goes away and all work fine if after I click the button I manually change the language on the address bar and refresh the page with the new URL.

I spent few minutes debugging and I believe the issue is related to the LocalizeRouteParser#_translateRouteTree method that on line 163 of the master branch should invoke the LocalizeRouteParser#_translateProperty setting to true the 3rd prefixLang parameter. This is because, as far as my understanding goes, in this case the route will be prepended with the new language and as a result the routes configuration will be updated and back on LocalizeRouterService#changeLanguage the browser will be refreshed with the new route and as a result the system will start working correctly. I am not sure if there are other implications that is something that need to be considered.

Looking at the bug list I believe this is a duplicate of #139 but I’ll leave this to you to judge, in the meantime I will update my switchLanguage method so that it refreshes the browser page with new language and update on this.

Thanks, ~Q

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:1
  • Comments:20

github_iconTop GitHub Comments

3reactions
quirinobrizicommented, Aug 16, 2018

Hi @meeroslav done some more test and the only way that seem to work is to reload the full website so if I write something like that:

switchLanguage(language: string) {
    this.localize.changeLanguage(language);
    console.log('AFTER CHANGE ROUTES', this.localize.parser.routes);
    let currentUrl: string = this.router.url;
    let parts: Array<string> = /\/(.+)\/(.+)/.exec(currentUrl);
    if (parts) {
      let currentLanguage = parts[1];
      let currentPage = parts[2];
      if (currentLanguage != language) {
        let newUrl = `/${language}/${currentPage}`;
        // this.router.navigateByUrl(newUrl);
        window.location.href = newUrl;
      }
    } else {
      console.log('current URL does not match');
      window.location.href = `/${language}`;
    }
  }

it does work, the page is reloaded the new language is set and the navigation works with no problem. What I noticed and that may void what I wrote above but I don’t know the module well enough to say so is that the redirect route is not updated when the the LocalizeRouterService#changeLanguage is invoked.

On Init

START ROUTES (2) [{…}, {…}] {children: Array(5), path: “en”} {path: “”, redirectTo: “en”, pathMatch: “full”}

After LocalizeRouterService#changeLanguage

AFTER CHANGE ROUTES (2) [{…}, {…}] {children: Array(5), path: “it”} {path: “”, redirectTo: “en”, pathMatch: “full”}

Thanks, ~Q

1reaction
Roelensdamcommented, Oct 8, 2018

@apolon23 Thanks for the tip, but i DON’T want to reload the page. This make the whole app reload. I DON’T want the component to be reloaded either.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ruby on rails - How can I change the language of a posted view?
I have routing problems because there is no route that matches [Get]"/Contacts" and localhost:3000/contacts is called with post . My apllication ...
Read more >
Node.js Handling invalid routes - GeeksforGeeks
Route for invalid routing should be placed at the last of all routes because routes are called in the order in which they...
Read more >
how can i change the language from Spanish to English on ...
I have an Acer Aspireone which is brand new and I live in Spain. I had an English based computer that got stolen...
Read more >
Unicast Flooding in Switched Campus Networks - Cisco
Cause 1: Asymmetric Routing ... Large amounts of flooded traffic might saturate low-bandwidth links causing network performance issues or complete ...
Read more >
Route - Angular
The component to instantiate when the path matches. Can be empty if child routes specify components. loadComponent?: () => Type<unknown> | Observable<Type< ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found