st4lk / django-solid-i18n-urls

[DEPRECATED] Use default language for urls without language prefix.
http://www.lexev.org/en/
Other
112 stars 29 forks source link

A Minor Drawback #10

Closed jmp0xf closed 9 years ago

jmp0xf commented 9 years ago

If SOLID_I18N_USE_REDIRECTS is set to be True, the url containing default language code would be unable to route, which is not a friendly behaviour in my opinion.

st4lk commented 9 years ago

Hi, thanks for reporting! What do you mean by "unable to route"? It is about the case, when client have language other than default language of web application and he'll get redirect 301 or 302? But exactly this behaviour you get, if SOLID_I18N_USE_REDIRECTS = True. By the way, default django i18n process will always return 301 or 302 redirect if you request url without language code.

jmp0xf commented 9 years ago

eg. The default language code is "en". Trying to access http://example.com/en will get you a 404 response. A better behaviour should be redirecting it to http://example.com with setting current language to "en"

st4lk commented 9 years ago

Ah, agree, that make sense

st4lk commented 9 years ago

One note: If prefix with default language code is present in url, exactly that language will be rendered, redirect will not occur. Uploaded version v0.6.1

st4lk commented 9 years ago

@jmp0xf i've added some settings to control default language prefix behaviour #12 . Probably you'll be interested in SOLID_I18N_DEFAULT_PREFIX_REDIRECT = True. This setting is available in v0.7.1 from pypi.

jmp0xf commented 9 years ago

it seems that it cannot work properly with SOLID_I18N_USE_REDIRECTS = True Precisely, it cannot change current language by visiting url with default language code prefix.

st4lk commented 9 years ago

Precise, what behaviour do you expect and what you get?

jmp0xf commented 9 years ago

it is able to show the page in proper language when SOLID_I18N_USE_REDIRECTS = True. However the language setting will not be changed. if both SOLID_I18N_USE_REDIRECTS and SOLID_I18N_DEFAULT_PREFIX_REDIRECT are set to be True, visiting url with default language prefix will redirect to url with current language prefix.

st4lk commented 9 years ago

You mean, when

LANGUAGE_CODE = 'en'  # default language
LANGUAGES = (('ru', 'Russian'), ('en', 'English'))

url /en/... must render english content in every case, even if preferred language is Russian? In that case you should not use SOLID_I18N_DEFAULT_PREFIX_REDIRECT = True, use SOLID_I18N_HANDLE_DEFAULT_PREFIX = True instead.

I don't think, that by accessing to url /en/ we must set in client's cookies preferred language = English. To my mind, it must be done explicitly by user by clicking corresponding button.

jmp0xf commented 9 years ago

Of course the actual strategy is a personal opinion. However, changing preferred language in terms of language prefix in url is the behaviour for non-default language prefix. I think it is better to keep consistent.

st4lk commented 9 years ago

Preferred language is never changed implicitly. If i understand you correctly, you mean that by accessing url with non-default language prefix, i.e. /ru/..., we somehow change user's preferred language. But it is not. Language prefix just have higher priority over user's preferences in discovering language for current request. By default in django (without solid-i18n), when you access /ru/ url you'll always get Russian content. Even if your preferred language is English. But url / will redirect you to /en/.

solid-i18n follows this strategy, except when both settings SOLID_I18N_DEFAULT_PREFIX_REDIRECT and SOLID_I18N_USE_REDIRECTS are set to True. In that case you can't see English content just by visiting /en/ url. You need to set your preferred language explicitly first (by sending POST to set_language view).

jmp0xf commented 9 years ago

My fault, the url with language prefix does not change the language setting, and yes, it should not do that. The actual problem annoying me lies in reversing named URLs in my observation. With SOLID_I18N_HANDLE_DEFAULT_PREFIX = True, the named url cannot be parsed to contain corresponding language prefix in rendered page with default language prefix in url. However, a non-default language prefixed page can handle it properly.

jmp0xf commented 9 years ago

My problem lies in

if language_code != settings.LANGUAGE_CODE:
                regex = '^%s/' % language_code
            elif getattr(settings, 'SOLID_I18N_HANDLE_DEFAULT_PREFIX', False):
                regex = '(?:^%s/)?' % language_code
            else:
                regex = ''

If I change it to

if language_code != settings.LANGUAGE_CODE:
                regex = '^%s/' % language_code
            elif getattr(settings, 'SOLID_I18N_HANDLE_DEFAULT_PREFIX', False):
                regex = '^%s/' % language_code
            else:
                regex = ''

Everything goes fine.

st4lk commented 9 years ago
LANGUAGE_CODE = 'en'
LANGUAGES = (('ru', 'Russian'), ('en', 'English'))

urlpatterns = solid_i18n_patterns('',
    url(r'^$', home_view, name='home'),
    url(r'^about/$', about_view, name='about'),
)
SOLID_I18N_HANDLE_DEFAULT_PREFIX = False
    with translation.override('en'):
        url = reverse('about')  # /about/
    with translation.override('ru'):
        url = reverse('about')  # /ru/about/
SOLID_I18N_HANDLE_DEFAULT_PREFIX = True
    with translation.override('en'):
        url = reverse('about')  # /about/
    with translation.override('ru'):
        url = reverse('about')  # /ru/about/

What you need instead of this?

You mean, that when make request to url without language prefix, reverse all other urls without prefix. And when we request url with default language prefix, reverse all other urls with default language prefix?

st4lk commented 9 years ago

By the way, this code:

if language_code != settings.LANGUAGE_CODE:
                regex = '^%s/' % language_code
            elif getattr(settings, 'SOLID_I18N_HANDLE_DEFAULT_PREFIX', False):
                regex = '^%s/' % language_code
            else:
                regex = ''

will respond 404 for root urls (without language prefix), if SOLID_I18N_HANDLE_DEFAULT_PREFIX = True.

jmp0xf commented 9 years ago

Yes, I was trying to say "when make request to url without language prefix, reverse all other urls without prefix and we request url with default language prefix, reverse all other urls with default language prefix"