1

Angular2 - TypeScript - ng-Translate

Hello people,

I need to change the rest of my url depending on language selected "en/soybean", "fr/soya". In my app.routes I got this path :

{ path: ':lang/soybean', component: SoybeanComponent }

My SoyBean component is translate from the lang param with ng-translate

this.route.params.subscribe(params => {
   translate.use(params['lang']);
})

How can I display the url as fr/soya but still use path fr/soybean !?

What I tried

*I create a new path: { path: ':lang/soya', component: SoybeanComponent }, I thought I could redirect the user to the good path :

 if(params['lang'] === 'en'){
       this.router.navigateByUrl('lang/soybean');
    }
    if(params['lang'] === 'fr'){
       this.router.navigateByUrl('lang/soya');
    }

but it will result on a infinite loading.
Then I thought I could hardcode the lang param after redirecting like :

if(params['lang'] === 'en'){
       this.router.navigateByUrl('en/soybean');
    }
    if(params['lang'] === 'fr'){
       this.router.navigateByUrl('fr/soya');
    }

but in this case I will catch an exception.*

2
  • It looks like you are treating lang as both a route parameter and as a constant path route? Commented Jul 15, 2017 at 1:14
  • lang determine witch language I will use, but it didn't change the path. If lang is fr then soybean.html and soybean.component will be translate to french. What I want is how can I display the url as fr/soya but still use path fr/soybean ! Sorry for bad explanation I will edit my first post ! Commented Jul 17, 2017 at 14:09

2 Answers 2

1

You are overcomplicating it :-). Just used template strings with backticks

url = `${lang}/${i18name}`
Sign up to request clarification or add additional context in comments.

2 Comments

Is this angular 2 ? And could you precise where and how you use it ! Cause I define my url in the app.routes.ts like that { path: ':lang/soybean', component: SoybeanComponent } ? thx !
Well it's baked into Typescript that's the foundation Angular 2 & 4 are based upon. basarat.gitbooks.io/typescript/content/docs/…
0

I found my own solution with location.replace(newUrl)

I create a new path : { path: ':lang/soya', component: SoybeanComponent }

and in my component I use location.replace(url)

 this.route.params.subscribe(params => {
            translate.use(params['lang']);
            switch (params['lang']) {
                case 'en':
                    location.replace(`index.html#/${params['lang']}/soybean`)
                        break;
                case 'fr':
                    location.replace(`index.html#/${params['lang']}/soya`);
                        break;
            }

        })

This works for me ! If you know a better solution i'll be glad to hear it ! Thx !

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.