emmett-framework / emmett

The web framework for inventors
BSD 3-Clause "New" or "Revised" License
1.06k stars 71 forks source link

Improve internationalization: allow default language in URL #71

Closed GiantCrocodile closed 8 years ago

GiantCrocodile commented 8 years ago

If you set app.language_force_on_url = True and app.language_default = "en" you can't access URLs with /en in because it will return Invalid action. I would suggest to change this bahaviour because you can't share a link with someone who can understand English but have an other supported language in browser set up.

This could be intended behaviour or a bug.

gi0baro commented 8 years ago

@GiantCrocodile I'm not sure I understand the issue. The fact that the default language points to the urls without any language is an intended behavior.

The share scenario is wrong, since setting app.language_force_on_url = True makes weppy ignoring the browser language and use the url one. So if you share yourappurl.com will open the app in default language, sharing e.g. yourappurl.com/it will open the app in italian language.

GiantCrocodile commented 8 years ago

I think you are right but the issue I had was that I want to generate all links with current language in them. All links should have same structure.

What I'm looking for in any link's case is: domain.tld/{lang}/...

Like if I add /de to URL. All current URLs point to domain without language in them. I think I have to use url function in combination with what Weppy's core provide: self._language = environ.get('HTTP_ACCEPT_LANGUAGE') as parameter.

gi0baro commented 8 years ago

@GiantCrocodile the url function builds urls using the actual language of the user. So it should work without any troubles. Where are you using it?

If you really need to have the default language urls available also with the language component, I may add an option to make it happens.. But i would like to clearly understand the usage case.

GiantCrocodile commented 8 years ago

I will close this request now. There is no need for this function anymore. You were right @gi0baro.

GiantCrocodile commented 8 years ago

OK back to this because I can't get it solved: I want a link on every page to change the language between German and English. English is default one.

What I have so far:

            {{if current.request.language == "en":}}
                <a href="{{=url("/de")}}">German</a>
            {{else:}}
                <a href="{{=url("../")}}">English</a>
            {{pass}}

This is working for the index page but of course not if you are on one of the sub pages. How can I make the generated URL relative to current URL? I have to keep URL structure but change language to en or de accordingly.

Is there some method to get base URL and current URL? Then I could do it without usage of url().

gi0baro commented 8 years ago

@GiantCrocodile ok so you have two problems:

Now, while I can implement a parameter to the url function in order to set a specific language, you still have the problem of re-building the current url, and I think this is quite specific to your use case.

In the meantime, a solution can be making a Helper for your templates with a function:

from weppy import Helper, request, url
class MyHelper(Helper):
    @staticmethod
    def other_lang_url():
        u = url(request.name)
        if u.startswith("/de"):
            u = "/"+u[3:]
        else:
            u = "/de"+u[1:]
        return u

register this helper in your application helpers and then you would be able to call it in the templates:

<a href="{{=other_lang_url()}}">Change Language</a>

Hope it helps.