1

What i what is make localized urls like:

Let pretend that we always know user lang.

angular.module('website', [])
.constant('ROUTES', (function () {
    var lang = 'eng'; // just for example
    return {
        SOME_PATH: '/' + lang  + '/somepath'
  }
})())
.config([..., '$locationProvider', 'ROUTES', function(..., $locationProvider, ROUTES {
    $routeProvider.when(ROUTES.SOME_PATH, {templateUrl: 'pages/some_page.html', controller:     'SomePageController'});
    $locationProvider.html5Mode(true);
}])
.run(['$rootScope', 'ROUTES', function ($rootScope, ROUTES) {
    $rootScope.ROUTES = ROUTES;
}]);

So, if i am add link in html, everithing works:

<a href="{{ROUTES.SOME_PATH}}">Some path</a>

After i am click a link routing work just as planned. But if i refresh page or came by direct link ("http://example.com/eng/somepath"), i cannot reach any page and see a 404 page.

4
  • Where is the '#' in your URLs? mysite.com/eng/somepath must be mysite.com/#/eng/somepath or the browser will be requesting an entirely different URL from the server. Only server settings could remap that back for you. Commented Jul 7, 2013 at 15:03
  • @lossleader You don't need the # since $locationProvider has it's html5Mode set to true Commented Jul 7, 2013 at 21:32
  • Yep, it's because $locationProvider.html5Mode(true); In browser without support html5 mode '#' will be exist Commented Jul 8, 2013 at 1:07
  • 1
    html5 mode isn't magic.. The urls you generate have to come back to the same page or they only work in the current DOM thanks to the history API. Try the docs examples (Books/chapters) and note that going to any of the addressbar urls generated are 404s.. Commented Jul 8, 2013 at 10:49

2 Answers 2

2

Wouldn't be easier to map the route with the $routeProvider?

$routeProvider.when('/:langId/:somepath', 
    {
     controller: 'languageHandlingController'
    })
Sign up to request clarification or add additional context in comments.

3 Comments

So, it's work but only in move :langId param to the end of url: mysite.com/somepath/ger. May be it cause of '#' in url
it should work in both ways. maybe is something in your controller. if you want to post more code, i can try to help you.
Actualy thats no difference for me, so thank you again. Now i'm facing problem with direct links(or external, or refresh) - angular cannot load page by path, but it's look like i should solve it with server settings
0

I think the best option will be to have a the urls to be something like http://mysite.com/somepath?lang=en and then you can just read lang directly from the url wherever you need to.

1 Comment

Yes, it's more traditional path. The only reason is kepp url clear of extra get params. And, imho, urls like mysite.com/ger/somepath looks much pretty

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.