alephdata / aleph

Search and browse documents and data; find the people and companies you look for.

Home Page:http://docs.aleph.occrp.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

BUG: Error messages in Russian when Accept-Language header is *

pbock opened this issue · comments

Describe the bug
When making an HTTP request to the Aleph API with the Accept-Language header set to * (as seems to be the default in the node.js implementation of fetch()), any error messages will be in Russian.

To Reproduce
Assuming Aleph is running on port 8080:

curl -X "POST" "http://localhost:8080/api/2/collections"
# {"status": "error", "message": "You are not authorized to do this.", "roles": [1]}

curl -X "POST" "http://localhost:8080/api/2/collections" -H 'Accept-Language: *'
# {"status": "error", "message": "\u0423 \u0432\u0430\u0441 \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u0430\u0432.", "roles": [1]}
# (That’s "У вас недостаточно прав." if you don’t speak fluent Unicode)

Expected behavior
Requests with Accept-Language: * should be treated as if the header was not set at all.

(You could argue that the semantics of * are “Any language will do” while a missing header indicates “Give me the default”, but since this is caused by the default behaviour of node.js’s standard library, I imagine I won’t be the only one who is going to find themselves unexpectedly told off in Russian.)

Aleph version
3.15.5

We should be specific / deliberate about the default language that is returned with *. Right now Russian is the first in the list and so it will be picked. We should update this to use the defined 'default' language. Currently it looks like flask is not doing this.

At a first glance: in

locale = request.accept_languages.best_match(options)

we are using werkzeug's AcceptLanguage.best_match which specifically matches * against any item. The list of UI languages we are checking against happens to have ru as the first entry:

ui_languages = ["ru", "es", "de", "en", "ar", "fr"]

I'm guessing that if the accept-header says * we should return the default language:

self.DEFAULT_LANGUAGE = env.get("ALEPH_DEFAULT_LANGUAGE", "en")