diff --git a/README.md b/README.md index ae711e5a0aad7cdffd034bd74ceea0d5a6476db7..4a515e4ce482c6d4595626e54424c87e1e065d71 100644 --- a/README.md +++ b/README.md @@ -86,3 +86,31 @@ If you started the server as described above, you can access the django admin in [localhost:8000/admin](localhost:8000/admin). If you seeded the database you can login with username: "admin" and the password you specified. +## Internationalization and how to use it + +At the current time, the backend uses [django-modeltranslation])(https://django-modeltranslation.readthedocs.io/en/latest/index.html) to handle translations of Strings in the models Question, CallToMove, KnowledgeSnack. The list of available languages is defined in `settings.py`, and the translatable fields are defined in `translation.py`. Right now, the two languages `de` and `en` are enabled, with `de` translations needing to be filled out. + +Django internally keeps track of the active language, which decides on the language strings that `object.text` returns. If you want to force a certain language, `object.text_de` can be used. + +`"django.middleware.locale.LocaleMiddleware"` has been added as a Middleware. This allows Django to recognize the locale of the requesting Browser and automatically sets the language to that locale. This means that both the Django Admin Panel and our own API choose the Strings depending on the GET request. It is yet to be determined whether React can change the locale in the requests, but if that were the case, this feature would potentially ease the implementation of Serializers for the User-Frontend. + +Instead of relying on the Locale given by the Requests, one can also manually change the active language. This example demonstrates how: + +```python +from django.utils.translation import get_language, activate + +cur_language = get_language() # Returns current active language + +activate("de") # Selects German as active language + +print(object.text) # Prints German Translation + +activate("en") # Selects English as active language + +print(object.text) # Prints English Translation + +activate(cur_language) # Resets active language to the one before manual activations + +``` + +This might be particularly useful for entering data from the Admin Frontend into the database, or choosing the language for the User Frontend if the Browser Locale cannot be used. \ No newline at end of file diff --git a/unisportomat/quiz/serializers.py b/unisportomat/quiz/serializers.py index 9ae6987e07ac762c5f5f4dc4768cf8926eba2b5c..48c9443dcffc85870c086a3b0ec6f9bbda888973 100644 --- a/unisportomat/quiz/serializers.py +++ b/unisportomat/quiz/serializers.py @@ -2,7 +2,7 @@ Serializers creating JSONs for every Model from .models """ from rest_framework import serializers -from .models import Sport, Criterion +from .models import Sport, Criterion, Question class SportListSerializer(serializers.ModelSerializer): @@ -15,6 +15,17 @@ class SportListSerializer(serializers.ModelSerializer): fields = ("id", "name", "url", "criteria_ratings") +class QuestionListSerializer(serializers.ModelSerializer): + """ + Serializes all Questions. + """ + + class Meta: + model = Question + + fields = ("text", "criterion") + + class CriterionListSerializer(serializers.ModelSerializer): """ Serializes Criterions diff --git a/unisportomat/quiz/views.py b/unisportomat/quiz/views.py index ce6602ab7f47da71c0cc8ed0cd76f5e018d4450f..03cb6f920dc1ad9f4aed1e338c7c4333dd736927 100644 --- a/unisportomat/quiz/views.py +++ b/unisportomat/quiz/views.py @@ -4,8 +4,12 @@ Defines the views for the API # from django.shortcuts import render from rest_framework import viewsets -from .serializers import SportListSerializer, CriterionListSerializer -from .models import Sport, Criterion +from .serializers import ( + SportListSerializer, + CriterionListSerializer, + QuestionListSerializer, +) +from .models import Sport, Criterion, Question # Create your views here. @@ -26,3 +30,12 @@ class CriterionListView(viewsets.ModelViewSet): # pylint: disable=too-many-ance serializer_class = CriterionListSerializer queryset = Criterion.objects.all() + + +class QuestionListView(viewsets.ModelViewSet): # pylint: disable=too-many-ancestors + """ + A View returning every Question Object + """ + + serializer_class = QuestionListSerializer + queryset = Question.objects.all() diff --git a/unisportomat/unisportomat/urls.py b/unisportomat/unisportomat/urls.py index 9e2516382c8b1d9c5ec515c5af440259289c2eb5..801097cd6baa927bd468c4b170c198230b30dfe9 100644 --- a/unisportomat/unisportomat/urls.py +++ b/unisportomat/unisportomat/urls.py @@ -21,6 +21,7 @@ from quiz import views router = routers.DefaultRouter() router.register(r"sport-list", views.SportListView, "sport-list") router.register(r"criterion-list", views.CriterionListView, "criterion-list") +router.register(r"question-list", views.QuestionListView, "question-list") urlpatterns = [ path("admin/", admin.site.urls),