dotJEM / angular-routing

Enhanced state based routing for Angular applications!
https://dotjem.github.io/angular-routing/
MIT License
75 stars 9 forks source link

Multi url for states (e.g. multilingual) #119

Closed vizo closed 9 years ago

vizo commented 9 years ago

Is there a way to define multi urls for state? I am searching for this solution for a long time ... i made plugin for ui-router, custom url matcher ... but it's a little bit hacky and every new version i must update it... you can check it how it works on http://naslovnice.si.

I guess it would be so easy if route parameter could be also a function e.g.:

route: function($state, resolvedLocale, otherResolvedVar, ...) {
  // In functions for generating urls (e.g. $state.url), uses param locale if passed,
  // else uses resolved locale to return right url...
  var locale = $state.params.locale || resolvedLocale; 
  switch (locale) {
    default:
    case 'en':
      return '/en/home';
    break;
    case 'sl':
      return '/sl/domov';
    break;
  }
}

This could also be used for state's url aliases ...

What do you think?

jeme commented 9 years ago

You can use the route provider: https://github.com/dotJEM/angular-routing/wiki/Route-provider---basic-configuration for aliased states

angular.module('phonecat', ['dotjem.routing']).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider
      .when('/phones', { state: 'phones' })
      .when('/otherphones', { state: 'phones' });
}]);

As for your example of locales, I would make locale a parameter (/{locale}/home

vizo commented 9 years ago

As i understand, in this case, i can redirect different urls to same state. e.g.:

angular.module('phonecat', ['dotjem.routing']).
  config(['$routeProvider', function($routeProvider) {
  $routeProvider
      .when('/{locale}/phones', { state: 'phones'})
      .when('/{locale}/telefoni', { state: 'phones'})
}]);

But what about reverse, generating urls with $state.url ?

jeme commented 9 years ago

You may configure the primary URL on the state, and that will be used when using $state.url...

vizo commented 9 years ago

Maybe you didn't understand my point. I want to generate seo multilingual urls (optimizing for search engines)

jeme commented 9 years ago

That is safe to say, out of the scope of the router, as it would be a very complex scenario and there are more prioritized things atm... getting ready for 1.3 being one thing.

If you could settle for having just one active route set at one time. (so when en. is selected only those routes are available)... You could look into loading states dynamically.

See #19

And use $state.reinitialize whenever you change locale... That is the best built in thing I can think of... Otherwise you will need to use $route.format your self.