-
Notifications
You must be signed in to change notification settings - Fork 93
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
Unknown route switching language #140
Comments
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:
it does work, the page is reloaded the new language is set and the navigation works with no problem. On Init
After LocalizeRouterService#changeLanguage
Thanks, |
have the same problem, i think idea of this plugin is work exactly that your described in your issue |
same problem here, +1 |
anyone solved the problem? @meeroslav |
Solve it by manually changing the locale in the url and then reload the page, the easiest way |
@apolon23 my comment above provides the method I used to workaround the issue which in principle does what @Elmanderrr suggests as well. |
So I got bored of seeing my app reloading as I consider it a very bad practice from technical and user experience point of view and decided to crack on figure the problem out, there are 2 issues that make this problem up:
So the solution is simple just reset the config of the router after the routes translation is completed so the changeLanguage should be implemented as swtchLanguage(language: string, options: NavigationExtras, useNavigateMethod: Boolean) {
if (language != this.parser.currentLang) {
const currentUrl: string = this.router.url;
const newRoute: string = this.parser.translateRoute(currentUrl.replace(this.parser.currentLang, language));
this.parser.translateRoutes(language).subscribe(() => {
// reset the router config so that routes are dynamically loaded.
this.router.resetConfig(this.parser.routes);
const extras = Object.assign({ skipLocationChange: false }, options);
if (useNavigateMethod) {
this.router.navigate([newRoute], extras);
} else {
this.router.navigateByUrl(newRoute, extras);
}
});
}
} with the method above I can navigate my app without any page reload. I am creating a little project that tries to provide a way of approaching multil-anguage applications, is still very young and based as you can understand on this library. In order to use this new implementation load MultilangModule as const routes: Routes = [...]
@NgModule({
imports: [
TranslateModule.forRoot({
loader: {
provide: TranslateLoader,
useFactory: (http: HttpClient) => { return new TranslateHttpLoader(http, '/assets/locales/', '.json'); },
deps: [HttpClient]
}
}),
MultilangModule.forRoot(routes, {
parser: {
provide: LocalizeParser,
useFactory: (translate, location, settings, http) =>
new LocalizeRouterHttpLoader(translate, location, settings, http),
deps: [TranslateService, Location, LocalizeRouterSettings, HttpClient]
}
}),
RouterModule.forRoot(routes)],
exports: [TranslateModule, LocalizeRouterModule, RouterModule]
})
export class AppRoutingModule {
} and use it as: constructor(private multilangService: MultilangService) {}
...
switchLanguage(language: string) {
this.multilangService.swtchLanguage(language, {}, true);
} I will create a PR on this library as soon as I get time. Hope the above can help. @meeroslav I think we can considered this issue as verified, if you are OK with it I will use this issue to create a PR as soon as I get time. |
I have the same issue. When changing language, the component reload (it occurs when hitting The function : After fixing the function
I've tried to implement your function Any update on this ? |
@Roelensdam i solved this problem like this: |
@apolon23 |
After further investigation, it seems linked to the immutability of the new router (Angular 6). Any suggestions, ideas, work around to prevent the reloading of the component ? |
@Roelensdam I am not familiar with Angular 6, I am between moving and have no much time to look into it I will as soon as I find some time... sorry about that. |
@Roelensdam Problem fixed on angular 6 fork https://www.npmjs.com/package/@gilsdav/ngx-translate-router @Greentube you can check what changed here: gilsdav/ngx-translate-router@de42217 |
@quirinobrizi, @Roelensdam, @gilsdav, This should be solved on both version v1.0.1 (for Angular 5.0.0-5.2.6) and 2.0.0-RC.1 (for Angular 6+). Can you please verify? |
@meeroslav
|
@meeroslav Some bugs that are no more on my repo but still in 2.0.0-RC.1:
All probably fixed in this PR : gilsdav/ngx-translate-router@de42217 You can test by cloning my repo and you change every reference of my fork to localize-router into src folder (the demo app). |
@gilsdav, I like your approach with ReuseStrategy, but I would not make it part of localize-router. As for the empty subroute, I will look into it ... it's on my list. |
@Elmanderrr, can you provide a minimal example with this behavior? Also, please open the new issue so I can target that specifically. |
I was stuck with this issue when switching the route with parameters, such as When I use just the Hence in my switch locale function, I decided to manually reconstruct the URL and navigate the user, here is the function I came up with, which is working fine for me.
|
I was excited it worked. Upon further testing it is still an issue, in some cases it still produces the same URL and in most cases it doesn't ... |
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
Component
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
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
The text was updated successfully, but these errors were encountered: