In my app I use angular UI-Router.
I have locals (English and Hebrew) My base language is English.
Thats why i want if the language is English not to add the parameter to the url
For example:
- Home English -->
http://example.com/
Home Hebrew -->
http://example.com/he/
About us English -->
http://example.com/about
- About us Hebrew -->
http://example.com/he/about
Is this possible ?
This is my current code
$stateProvider
.state('/', {
url: "/",
templateUrl: "Assets/app/templates/home.html",
controller: 'homeController'
})
.state('activity', {
url: "/activity",
templateUrl: "Assets/app/templates/gallery.html",
controller: 'galleryController'
})
.state('page', {
url: '/:pagename',
templateUrl: "Assets/app/templates/page.html",
controller: 'pageController'
});
There is a working plunker
As always, that is feasible with
UI-Router
- built in features. Firstly, we'd introduce the super parent state called for example 'root'. It would have defined parameterlang
Interesting things to mention: The
url
uses regexp to reduce amount of matching lang words (in our case, English, Hebrew and Czech) :Read more e.g. here.
Also, we do profit from a setting called
params : {}
. It says, that the default value is'en'
and what is more important - it should be squashed, skipped if there is a match with 'en' param value:Read more e.g. here or here
So, this is our parent, root state, which we just would apply to all states with a state definition setting
parent : 'root'
:And now these links would work:
ui-sref English:
ui-sref Hebrew:
href English:
href Hebrew:
Check it in action here