6aika / issue-reporting

Reference Open311 API server implementation
MIT License
7 stars 4 forks source link

How to switch language on services API endpoint #72

Open milovanderlinden opened 8 years ago

milovanderlinden commented 8 years ago

I have tried by setting the accept headers to dutch, but this will not switch the language inside the response. Can you please explain?

aapris commented 8 years ago

You probably managed to set LANGUAGES='en,nl' (or something similar) in your environment and you have successfully migrated those languages to the database?

Then just add locale=nl parameter to the request, e.g. /api/georeport/v2/services.json?locale=nl.

This is documented here: https://github.com/6aika/api-palaute/blob/master/palaute.swagger.yaml#L65 but I realise better documentation is needed.

Just keep asking things. :)

akx commented 8 years ago

It might be a good idea to also support Accept-Language.

Django's default locale middleware will do just that -- it's just that it's not enabled for us by default.

milovanderlinden commented 8 years ago

Ok, I will use locale=nl for testing right now and in the meanwhile see if I can get Accept-language working as well. Thank you for the comment.

aapris commented 8 years ago

It seems it is enough if you just add locale middleware into your settings.MIDDLEWARE_CLASSES like this:

MIDDLEWARE_CLASSES = [
    ...
    'django.middleware.locale.LocaleMiddleware',
]

I tried with http -v GET http://127.0.0.1:8000/api/georeport/v2/services.json "Accept-Language:fi,en" and it works as expected.

Now I wonder should this be enabled by default? And if not, what is the recommended method to inject the LocaleMiddleware into MIDDLEWARE_CLASSES? I've heard that local_settings.py is a bit last season, or what do you think @akx?

akx commented 8 years ago

Yeah, I believe it's pretty safe to be enabled by default, @aapris. I think it was left out in an attempt to be as minimalistic as possible :)