Skip to content
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

Support locale in route paths #23

Open
wichert opened this issue Oct 14, 2015 · 11 comments
Open

Support locale in route paths #23

wichert opened this issue Oct 14, 2015 · 11 comments

Comments

@wichert
Copy link

wichert commented Oct 14, 2015

A common way to handle language selection in a site is to use the first component in the URL path to set the locale. Aaike implemented this in aaike/router@83891160c2c116c388517b344ed91d972089a911 with the following rules:

  • if the current locale is the sites default locale the URL is not changed
  • if the current locale is not the site default locale the URL path is changed to /${locale}<normal route path>
  • if the locale is changed the URL path is automatically updated

His changes work fine, and we have a site in production that relies on this behaviour. Unfortunately our use of Aaike's router fork means we can not upgrade to a recent Aurelia version.

In hindsight I am not so sure about the magic no-prefix-for-default-locale rule. This feels a bit risky since it will break for paths that happen to match a locale name: for example /my/settings could be both "my settings" or "settings in Burmese", and requires you to have a default locale concept which might not be necessary for anything else.

The discussion in zewa666/aurelia-i18next#10 also relates to this topic.

@zewa666
Copy link
Member

zewa666 commented Oct 14, 2015

@Aaike can you take a look at this one?

@wichert
Copy link
Author

wichert commented Oct 14, 2015

FWIW Aaike's original change no longer applies: in aurelia/router@3185d5b @bryanrsmith moved the relevant code to history-browser. I don't think that is the right place to do this though. Perhaps there is a missing router hook that i18n could use so this doesn't have to be done in the routing system itself.

@zewa666
Copy link
Member

zewa666 commented Oct 14, 2015

that's what @bryanrsmith was talking about as well. I'd really like to see a nice integration via hooks so we don't have to spoil the code all over multiple repos

@wichert
Copy link
Author

wichert commented Oct 14, 2015

At first sight it looks like two hooks should suffice:

  • a pre-parse hook which can take the path being parsed, grab the locale from it and return the modified path for further router processing
  • a post-generate hook which gets a generated path and insert the current locale

Another option is to use a pre-generate hook which takes a route object as parameter, and returns the route object to use. That allows you do to the normal locale-insertion:

import {config} from 'app-configuration';

function addLocaleToPath(route, params) {
    r = clone(route);  // I am guessing there is a handy utility function for this
    r.path = `${config.locale}/${r.path}`;
    return r;
}

but also more involved things like using translated paths:

import {config} from 'app-configuration';
import {I18N} from 'aurelia-i18next';

function translatePath(route, params) {
    r = clone(route);  // I am guessing there is a handy utility function for this
    r.path = I18N.tr(r.path);
    return r;
}

of course that would require another hook to pre-process routes before attempting to match them, which makes the whole thing way more complicated than 99% or all sites use, so I wouldn't go down that rabbit hole (again - I've done that once many years ago).

@wichert
Copy link
Author

wichert commented Nov 23, 2015

@zewa666 Has there ben any progress on this front? Is there anything I can do to help?

@wichert
Copy link
Author

wichert commented Feb 3, 2016

@zewa666 ping?

@zewa666
Copy link
Member

zewa666 commented Feb 3, 2016

oh sry totally forgot to reply here.
Yep we've internally talked about it and currently the top priorities are to bring the plugins dependencies up to date. The Routing parts will follow then. So for now, no new progress

@zewa666
Copy link
Member

zewa666 commented Nov 28, 2016

@wichert it's been a while, but seems there was an important merged tying into this issue. aurelia/router#409 Wonder if you have time to re-validate how that applies to your story?

@wichert
Copy link
Author

wichert commented Nov 28, 2016

@zewa666 It's certainly a related feature that is useful, but does not implement anything this ticket asks for.

@zewa666
Copy link
Member

zewa666 commented Nov 28, 2016

Thats true, its just the part about applying the translation, nothing regarding the hooks. Just wanted to let you know there is at least some progress going on ;)

@lioff
Copy link

lioff commented Dec 19, 2019

This issue is quite old but I need a way to parse the locale from the url (without defining each route several times for all the locales). Is there a solution to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants