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

Force API translation with URL language defined #224

Merged
merged 4 commits into from
Dec 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ on:
env:
DEBIAN_FRONTEND: noninteractive

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
flake8:
name: Check python linting
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ var/
/georiviere/settings/custom.py
/install/georiviere/
/install.zip
.idea/
4 changes: 4 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ CHANGELOG
1.3.0+dev (XXXX-XX-XX)
---------------------

**Bug fix**

- Force translation defined in API url /api/portal/<lang> (fix #222)


1.3.0 (2023-11-17)
-------------------------
Expand Down
29 changes: 29 additions & 0 deletions georiviere/middleware.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import re

from django.utils import translation
from django.utils.translation.trans_real import get_supported_language_variant

language_code_prefix_re = re.compile(r'^/api/portal/([\w-]+)(/|$)')


def get_language_from_path(path):
regex_match = language_code_prefix_re.match(path)
if not regex_match:
return None
lang_code = regex_match.group(1)
try:
return get_supported_language_variant(lang_code)
except LookupError:
return None


class APILocaleMiddleware:
def __init__(self, get_response):
self.get_response = get_response

def __call__(self, request):
language = get_language_from_path(request.path_info)
if language:
translation.activate(language)
request.LANGUAGE_CODE = translation.get_language()
return self.get_response(request)
9 changes: 9 additions & 0 deletions georiviere/portal/tests/test_views/test_river.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,12 @@ def test_stream_list_json_structure(self):
self.assertEqual(len(response.json()), 1)
self.assertSetEqual(set(response.json()[0].keys()), {'attachments', 'flow', 'descent', 'length',
'name', 'id', 'description', 'geometryCenter'})

def test_stream_ranslation_according_url(self):
for lang in ['en', 'fr']:
url = reverse('api_portal:streams-detail',
kwargs={'portal_pk': self.portal.pk, 'pk': self.stream.pk, 'lang': lang, 'format': 'json'})
response = self.client.get(url)
self.assertEqual(response.status_code, 200)
data = response.json()
self.assertEqual(data['flow'], 'To be defined' if lang == 'en' else 'À définir')
5 changes: 3 additions & 2 deletions georiviere/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,18 +190,19 @@ def construct_relative_path_mock(current_template_name, relative_name):
PAPERCLIP_ENABLE_LINK = True
PAPERCLIP_ENABLE_VIDEO = True

CRISPY_ALLOWED_TEMPLATE_PACKS = ('bootstrap', 'bootstrap3', 'bootstrap4')
CRISPY_ALLOWED_TEMPLATE_PACKS = ('bootstrap4', )
CRISPY_TEMPLATE_PACK = 'bootstrap4'

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.locale.LocaleMiddleware',
'georiviere.middleware.APILocaleMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.locale.LocaleMiddleware',
'mapentity.middleware.AutoLoginMiddleware',
]

Expand Down
Loading