From 799f0be53e4b61f61da1e9208329cb70d1e80e80 Mon Sep 17 00:00:00 2001 From: Jannis Portmann Date: Tue, 16 May 2023 23:36:40 +0200 Subject: [PATCH] Add translated strings --- pflaenzli/pflaenzli/forms.py | 7 +++-- pflaenzli/pflaenzli/mail.py | 5 +-- pflaenzli/pflaenzli/models.py | 13 ++++---- pflaenzli/pflaenzli/templates/app/index.html | 11 ++++--- pflaenzli/pflaenzli/templates/base.html | 31 ++++++++----------- .../pflaenzli/templates/offer/detail.html | 27 +++++++++------- pflaenzli/pflaenzli/templates/offer/edit.html | 16 ---------- .../pflaenzli/templates/offer/search.html | 13 +++++--- .../registration/confirmation_email.html | 19 +++++------- .../templates/registration/login.html | 9 ++++-- .../registration/password_reset_complete.html | 5 +-- .../registration/password_reset_confirm.html | 5 +-- .../templates/registration/register.html | 7 +++-- .../templates/registration/verify_email.html | 5 +-- .../registration/verify_email_done.html | 7 +++-- .../pflaenzli/templates/user/detail.html | 11 ++++--- .../pflaenzli/templates/user/public.html | 6 ++-- .../templates/user/trade/offer_email.html | 11 ++++--- pflaenzli/pflaenzli/templates/user/wish.html | 13 +++++--- pflaenzli/pflaenzli/views.py | 28 +++++++++-------- pflaenzli/pflaenzli_django/settings.py | 6 +++- 21 files changed, 131 insertions(+), 124 deletions(-) delete mode 100644 pflaenzli/pflaenzli/templates/offer/edit.html diff --git a/pflaenzli/pflaenzli/forms.py b/pflaenzli/pflaenzli/forms.py index 2e7d651..8776e98 100644 --- a/pflaenzli/pflaenzli/forms.py +++ b/pflaenzli/pflaenzli/forms.py @@ -2,6 +2,7 @@ from django import forms from django.contrib.auth.forms import UserCreationForm from django.utils.safestring import mark_safe from friendly_captcha.fields import FrcCaptchaField +from django.utils.translation import gettext_lazy as _ from .models import Offer, PflaenzliUser, Wish @@ -22,11 +23,11 @@ class RegistrationForm(UserCreationForm): class FilterForm(forms.Form): text = forms.CharField(max_length=128, required=False, label=mark_safe( - ' Search')) + f' {_("Search")}')) zipcode = forms.CharField(max_length=4, required=False, label=mark_safe( - ' Zipcode')) + f' {_("ZIP code")}')) distance = forms.IntegerField(required=False, label=mark_safe( - ' Distance (km)')) + f' {_("Entfernung")} (km)')) class WishForm(forms.ModelForm): diff --git a/pflaenzli/pflaenzli/mail.py b/pflaenzli/pflaenzli/mail.py index d2a8504..8095fa2 100644 --- a/pflaenzli/pflaenzli/mail.py +++ b/pflaenzli/pflaenzli/mail.py @@ -1,6 +1,7 @@ from django.core.mail import EmailMessage, EmailMultiAlternatives from django.urls import reverse from django.template.loader import render_to_string +from django.utils.translation import gettext_lazy as _ def send_offer_email(request, offer, sender_user, recipient_user): @@ -9,7 +10,7 @@ def send_offer_email(request, offer, sender_user, recipient_user): plain_text = get_offer_text(request, offer, sender_user, recipient_user) message = EmailMultiAlternatives( - f'{sender_user.username} wants to trade', + f'{sender_user.username} {_("wants to trade")}', plain_text, 'no-reply@pflaenz.li', [recipient_user.email], @@ -22,4 +23,4 @@ def send_offer_email(request, offer, sender_user, recipient_user): def get_offer_text(request, offer, sender_user, recipient_user): - return f"Hello {recipient_user.username},\n\nThe user {sender_user.username} would like to trade '{offer.title}' with you!\n\nIf you would like to trade with {sender_user.username}, just reply to this email to get in touch with them.\n\nYou can also view their offers here: {request.scheme}://{request.get_host()}{reverse('user_detail', args=[sender_user.id])}" + return f"{_('Hello')} {recipient_user.username},\n\n{sender_user.username} {_('is interested in')} {offer.title}!\n\n{_('Checkout their offers')}: {request.scheme}://{request.get_host()}{reverse('user_detail', args=[sender_user.id])}\n\n{_('Contact')} {sender_user.username}, {_('by replying to this mail')}." diff --git a/pflaenzli/pflaenzli/models.py b/pflaenzli/pflaenzli/models.py index c9055a3..a4a7266 100644 --- a/pflaenzli/pflaenzli/models.py +++ b/pflaenzli/pflaenzli/models.py @@ -5,26 +5,27 @@ from django.dispatch import receiver from django.contrib.auth.models import AbstractUser from django.utils import timezone from django.core.files.storage import default_storage +from django.utils.translation import gettext_lazy as _ class PflaenzliUser(AbstractUser): email = models.EmailField(max_length=254) - zipcode = models.PositiveIntegerField(blank=True, null=True) + zipcode = models.PositiveIntegerField(blank=True, null=True, verbose_name=_('ZIP Code')) class Offer(models.Model): created = models.DateTimeField(default=timezone.now) user = models.ForeignKey(PflaenzliUser, on_delete=models.CASCADE) - title = models.CharField(max_length=50) - description = models.TextField(max_length=5000) - zipcode = models.IntegerField(blank=True, default=0) - image = models.ImageField(upload_to="uploads/") + title = models.CharField(max_length=50, verbose_name=_('Title')) + description = models.TextField(max_length=5000, verbose_name=_('Description')) + zipcode = models.IntegerField(blank=True, default=0, verbose_name=_('ZIP Code')) + image = models.ImageField(upload_to="uploads/", verbose_name=_('Image')) class Wish(models.Model): created = models.DateTimeField(default=timezone.now) user = models.ForeignKey(PflaenzliUser, on_delete=models.CASCADE) - title = models.CharField(max_length=200) + title = models.CharField(max_length=200, verbose_name=_('Title')) @receiver(models.signals.post_delete, sender=Offer) diff --git a/pflaenzli/pflaenzli/templates/app/index.html b/pflaenzli/pflaenzli/templates/app/index.html index 7374ee5..adb67bf 100644 --- a/pflaenzli/pflaenzli/templates/app/index.html +++ b/pflaenzli/pflaenzli/templates/app/index.html @@ -1,5 +1,6 @@ {% extends 'base.html' %} {% load static %} +{% load i18n %} {% block title %}Home{% endblock %} {% block meta %}{% endblock %} {% block background %}home-background{% endblock %} @@ -9,18 +10,18 @@
-

Welcome to Pflänz.li

+

{% trans "Welcome to Pflänz.li" %}

- This is a platform to trade plants. You can offer plants and setup a wishlist what you want to trade it for. + {% trans "This is a platform to trade plants. You can offer plants and setup a wishlist what you want to trade it for." %}


-

To offer your plants, please register first.

+

{% trans "To offer your plants, please register first." %}

Show Offers + type="button">{% trans "Offers" %} Register + type="button">{% trans "Register" %}
diff --git a/pflaenzli/pflaenzli/templates/base.html b/pflaenzli/pflaenzli/templates/base.html index af145a0..e8f0a20 100644 --- a/pflaenzli/pflaenzli/templates/base.html +++ b/pflaenzli/pflaenzli/templates/base.html @@ -1,4 +1,5 @@ {% load static %} +{% load i18n %} @@ -6,12 +7,6 @@ {% block title %}{% endblock %} - + + {% block head %}{% endblock %} @@ -61,17 +60,17 @@ data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" - aria-label="Toggle navigation"> + aria-label="{% trans 'Toggle navigation' %}%"> - - diff --git a/pflaenzli/pflaenzli/templates/offer/detail.html b/pflaenzli/pflaenzli/templates/offer/detail.html index ab43a86..f027115 100644 --- a/pflaenzli/pflaenzli/templates/offer/detail.html +++ b/pflaenzli/pflaenzli/templates/offer/detail.html @@ -1,4 +1,5 @@ {% extends 'base.html' %} +{% load i18n %} {% block title %}Offer: {{ offer.title }}{% endblock %} {% block meta %} {% endblock %} {% block content %} - {% if offer.user == request.user %}{% endif %} + {% if offer.user == request.user %} + + {% endif %}
{% if offer.user == user %} - Me + {% trans "You" %} {% else %} {{ offer.user.username }} {% endif %} @@ -33,7 +36,7 @@

{% endif %}
-

Description

+

{% trans "Description" %}

{{ offer.description }}

@@ -55,34 +58,34 @@ {% else %} -

Wishes

-

{{ offer.user.username }} would like some of the following in return:

+

{% trans "Wishes" %}

+

{{ offer.user.username }} {% trans "would like some of the following in return" %}:

{% if wishes %} {% else %} - + {% endif %}
- Offer trade + {% trans "Offer trade" %} {% endif %} {% endblock %} diff --git a/pflaenzli/pflaenzli/templates/offer/edit.html b/pflaenzli/pflaenzli/templates/offer/edit.html deleted file mode 100644 index 5e5a26a..0000000 --- a/pflaenzli/pflaenzli/templates/offer/edit.html +++ /dev/null @@ -1,16 +0,0 @@ -{% extends 'base.html.twig' %} -{% block title %}Edit offer{% endblock %} -{% block content %} - {% for message in app.flashes('error') %} - - {% endfor %} -

Edit offer

- {{ form_start(offer_form) }} - {{ form_row(offer_form.title) }} - {{ form_row(offer_form.zipCode) }} - {{ form_row(offer_form.description) }} - {{ form_row(offer_form.photo, { - label: 'Choose file' - }) }} - {{ form_end(offer_form) }} -{% endblock %} diff --git a/pflaenzli/pflaenzli/templates/offer/search.html b/pflaenzli/pflaenzli/templates/offer/search.html index 8aca173..2f47cb1 100644 --- a/pflaenzli/pflaenzli/templates/offer/search.html +++ b/pflaenzli/pflaenzli/templates/offer/search.html @@ -1,17 +1,20 @@ {% extends 'base.html' %} {% load static %} +{% load i18n %} {% load crispy_forms_tags %} -{% block title %}Offers{% endblock %} +{% block title %} + {% trans 'Filter' %} +{% endblock %} {% block content %}

- Offers + {% trans 'Filter' %}

{% csrf_token %} {{ form|crispy }} - +
{% if offers %}
@@ -50,6 +53,6 @@ {% endfor %}
{% else %} - + {% endif %} {% endblock %} diff --git a/pflaenzli/pflaenzli/templates/registration/confirmation_email.html b/pflaenzli/pflaenzli/templates/registration/confirmation_email.html index 4cf4b5b..f97cffd 100644 --- a/pflaenzli/pflaenzli/templates/registration/confirmation_email.html +++ b/pflaenzli/pflaenzli/templates/registration/confirmation_email.html @@ -1,13 +1,10 @@ -

Welcome to pflänz.li!

- -

Please confirm your email

- +

{% trans "Welcome to pflänz.li" %}!

+

{% trans "Please confirm your email" %}

- Please confirm your email address by clicking the following link:

- Confirm my Email. - This link will expire in {{ expiresAtMessageKey|trans(expiresAtMessageData, 'VerifyEmailBundle') }}. -

- -

- Cheers! + {% trans "Please confirm your email address by clicking the following link" %}: +
+
+ {% trans "Confirm my email" %}. + {% trans "This link will expire in" %} {{ expiresAtMessageKey|trans(expiresAtMessageData, 'VerifyEmailBundle') }}.

+

Cheers!

diff --git a/pflaenzli/pflaenzli/templates/registration/login.html b/pflaenzli/pflaenzli/templates/registration/login.html index c5752c9..382ea9a 100644 --- a/pflaenzli/pflaenzli/templates/registration/login.html +++ b/pflaenzli/pflaenzli/templates/registration/login.html @@ -1,11 +1,14 @@ {% extends "base.html" %} +{% load i18n %} {% load crispy_forms_tags %} {% block content %} {% if next %} {% if user.is_authenticated %} -

Your account doesn't have access to this page. To proceed, please login with an account that has access.

+
+ {% trans "Your account doesn't have access to this page. To proceed, please login with an account that has access." %} +
{% else %} -

Please login to see this page.

+
{% trans "Please login to see this page." %}
{% endif %} {% endif %}

Login

@@ -16,6 +19,6 @@

- Forgot password? + {% trans "Forgot password?" %}

{% endblock %} diff --git a/pflaenzli/pflaenzli/templates/registration/password_reset_complete.html b/pflaenzli/pflaenzli/templates/registration/password_reset_complete.html index 62bcc1e..9c44067 100644 --- a/pflaenzli/pflaenzli/templates/registration/password_reset_complete.html +++ b/pflaenzli/pflaenzli/templates/registration/password_reset_complete.html @@ -1,5 +1,6 @@ {% extends 'base.html' %} +{% load i18n %} {% block content %} -

Reset password

-

New password was set successfully. Please login again.

+

{% trans "Reset password" %}

+

{% trans "New password was set successfully. Please login again." %}

{% endblock %} diff --git a/pflaenzli/pflaenzli/templates/registration/password_reset_confirm.html b/pflaenzli/pflaenzli/templates/registration/password_reset_confirm.html index 935354f..c808cfa 100644 --- a/pflaenzli/pflaenzli/templates/registration/password_reset_confirm.html +++ b/pflaenzli/pflaenzli/templates/registration/password_reset_confirm.html @@ -1,8 +1,9 @@ {% extends 'base.html' %} +{% load i18n %} {% load crispy_forms_tags %} {% block content %} -

Reset password

-

Choose a new password.

+

{% trans "Reset password" %}

+

{% trans "Choose a new password." %}

{% csrf_token %} {{ form|crispy }} diff --git a/pflaenzli/pflaenzli/templates/registration/register.html b/pflaenzli/pflaenzli/templates/registration/register.html index 39cff75..affa879 100644 --- a/pflaenzli/pflaenzli/templates/registration/register.html +++ b/pflaenzli/pflaenzli/templates/registration/register.html @@ -1,5 +1,8 @@ {% extends 'base.html.twig' %} -{% block title %}Register{% endblock %} +{% load i18n %} +{% block title %} + {% trans "Register" %} +{% endblock %} {% block meta %}{% endblock %} {% block javascripts %}{{ encore_entry_script_tags('captcha') }}{% endblock %} {% block content %} @@ -9,7 +12,7 @@ {% for message in app.flashes('error') %} {% endfor %} -

Register

+

{% trans "Register" %}

{{ form_start(registrationForm) }} {{ form_widget(registrationForm) }}
diff --git a/pflaenzli/pflaenzli/templates/registration/verify_email.html b/pflaenzli/pflaenzli/templates/registration/verify_email.html index 19453d9..a11c76b 100644 --- a/pflaenzli/pflaenzli/templates/registration/verify_email.html +++ b/pflaenzli/pflaenzli/templates/registration/verify_email.html @@ -1,8 +1,9 @@ {% extends 'base.html' %} +{% load i18n %} {% load crispy_forms_tags %} {% block content %} -

Reset password

-

Enter the email of your account, you'll then recieve a reset link.

+

{% trans "Reset password" %}

+

{% trans "Enter the email of your account, you'll then recieve a reset link." %}

{% csrf_token %} {{ form|crispy }} diff --git a/pflaenzli/pflaenzli/templates/registration/verify_email_done.html b/pflaenzli/pflaenzli/templates/registration/verify_email_done.html index 4296909..8b75da3 100644 --- a/pflaenzli/pflaenzli/templates/registration/verify_email_done.html +++ b/pflaenzli/pflaenzli/templates/registration/verify_email_done.html @@ -1,10 +1,11 @@ {% extends 'base.html' %} +{% load i18n %} {% block content %} -

Reset password

+

{% trans "Reset password" %}

- We've emailed you instructions for setting your password, if an account exists with the email you entered. You should receive them shortly. + {% trans "We've emailed you instructions for setting your password, if an account exists with the email you entered. You should receive them shortly." %}

- If you don't receive an email, please make sure you've entered the address you registered with, and check your spam folder. + {% trans "If you don't receive an email, please make sure you've entered the address you registered with, and check your spam folder." %}

{% endblock %} diff --git a/pflaenzli/pflaenzli/templates/user/detail.html b/pflaenzli/pflaenzli/templates/user/detail.html index e66488a..e532922 100644 --- a/pflaenzli/pflaenzli/templates/user/detail.html +++ b/pflaenzli/pflaenzli/templates/user/detail.html @@ -1,18 +1,19 @@ {% extends 'base.html' %} +{% load i18n %} {% block title %}User{% endblock %} {% block content %}

User details {{ user.username }}

-
Some functions do not work yet!
+
{% trans "Some functions do not work yet!" %}
-

Settings

- Edit Account +

{% trans "Settings" %}

+ {% trans "Edit Account" %}
-

Delete Account

+

{% trans "Delete Account" %}

{% endblock %} diff --git a/pflaenzli/pflaenzli/templates/user/public.html b/pflaenzli/pflaenzli/templates/user/public.html index f3063c7..7cbfd82 100644 --- a/pflaenzli/pflaenzli/templates/user/public.html +++ b/pflaenzli/pflaenzli/templates/user/public.html @@ -3,7 +3,7 @@ {% block title %}User {{ username }}{% endblock %} {% block content %}
-

{{ user.username }}'s Profile

+

{{ user.username }}'s {% trans "Profile" %}

Wishlist

@@ -12,7 +12,7 @@ {% for wish in wishes %}
  • {{ wish.title }}
  • {% endfor %} {% else %} - + {% endif %}
    @@ -46,6 +46,6 @@ {% endfor %}
    {% else %} - + {% endif %} {% endblock %} diff --git a/pflaenzli/pflaenzli/templates/user/trade/offer_email.html b/pflaenzli/pflaenzli/templates/user/trade/offer_email.html index 9bab75b..75ead91 100644 --- a/pflaenzli/pflaenzli/templates/user/trade/offer_email.html +++ b/pflaenzli/pflaenzli/templates/user/trade/offer_email.html @@ -1,5 +1,6 @@ -

    Hello {{ recipient_user.username }}

    -

    {{ sender_user.username }} wants to trade '{{ offer.title }}'!

    -

    Checkout their offers:

    -Link -

    Just reply to get in contact with {{ sender_user.username }}.

    +{% load i18n %} +

    {% trans "Hello" %} {{ recipient_user.username }}

    +

    {{ sender_user.username }} {% trans "is interested in" %} '{{ offer.title }}'!

    +

    {% trans "Checkout their offers" %}:

    +{{ request.user.username }}'s {% trans "offers" %} +

    {% trans "Contact" %} {{ sender_user.username }} {% trans "by replying to this mail" %}.

    diff --git a/pflaenzli/pflaenzli/templates/user/wish.html b/pflaenzli/pflaenzli/templates/user/wish.html index b5573a1..ac9d4b0 100644 --- a/pflaenzli/pflaenzli/templates/user/wish.html +++ b/pflaenzli/pflaenzli/templates/user/wish.html @@ -1,9 +1,12 @@ {% extends 'base.html' %} +{% load i18n %} {% load crispy_forms_tags %} -{% block title %}Whishlist{% endblock %} +{% block title %} + {% trans "Wishlist" %} +{% endblock %} {% block content %}
    -

    {{ title }} Wishlist

    +

    {{ title }}

    {% if wishes %} @@ -25,19 +28,19 @@ {% endfor %} {% else %} - + {% endif %}
    {% if form %}

    - New wish + {% trans "New wish" %}

    {% csrf_token %} {{ form|crispy }} - +
    {% endif %} diff --git a/pflaenzli/pflaenzli/views.py b/pflaenzli/pflaenzli/views.py index a976b70..f5dec0d 100644 --- a/pflaenzli/pflaenzli/views.py +++ b/pflaenzli/pflaenzli/views.py @@ -6,6 +6,8 @@ from django.contrib.auth.decorators import login_required from django.views.decorators.csrf import csrf_protect from django.views.decorators.http import require_POST from django.http import HttpResponseForbidden +from django.utils.translation import gettext_lazy as _ + from .forms import CreateOfferForm, RegistrationForm, FilterForm, WishForm @@ -38,12 +40,12 @@ def create_offer(request): offer.user = request.user offer.title = offer.title.title() offer.save() - messages.success(request, "Offer uploaded successfully!") + messages.success(request, _("Offer uploaded successfully!")) return redirect("offer_detail", offer.id) else: form = CreateOfferForm() - return render(request, "basic_form.html", {"form": form, "button_label": "Create", "title": "Create Offer"}) + return render(request, "basic_form.html", {"form": form, "button_label": _("Create"), "title": _("Create offer")}) def offer_detail(request, offer_id): @@ -71,7 +73,7 @@ def offer_delete(request, offer_id): return HttpResponseForbidden() offer.delete() - messages.success(request, "Offer deleted successfully!") + messages.success(request, _("Offer deleted successfully!")) return redirect("list_offers") @@ -88,12 +90,12 @@ def offer_edit(request, offer_id): offer.image.name = generate_unique_filename(form.cleaned_data['image'].name) offer.user = request.user offer.save() - messages.success(request, "Offer updated successfully!") + messages.success(request, _("Offer updated successfully!")) return redirect("offer_detail", offer.id) else: form = CreateOfferForm(instance=offer) - return render(request, "basic_form.html", {"form": form, "button_label": "Update", "title": "Edit Offer"}) + return render(request, "basic_form.html", {"form": form, "button_label": _("Save"), "title": _("Edit Offer")}) @login_required @@ -101,19 +103,19 @@ def wishlist(request, user_id): wishes = Wish.objects.filter(user=user_id) if user_id == request.user.id: - title = "Your" + title = _("Your wishlist") if request.method == "POST": form = WishForm(request.POST) if form.is_valid(): wish = form.save(commit=False) wish.user = request.user wish.save() - messages.success(request, "Wish added successfully!") + messages.success(request, _("Wish added successfully!")) form = WishForm() else: form = None user = get_object_or_404(PflaenzliUser, id=user_id) - title = f"{user.username}'s" + title = f"{user.username}'s {_('wishlist')}" return render(request, "user/wish.html", {"title": title, "form": form, "wishes": wishes, "own": user_id == request.user.id}) @@ -124,12 +126,12 @@ def user_edit(request): form = RegistrationForm(request.POST, instance=request.user) if form.is_valid(): form.save() - messages.success(request, "Account details updated successfully!") + messages.success(request, _("Account details updated successfully!")) return redirect("user_profile") else: form = RegistrationForm(instance=request.user) - return render(request, "basic_form.html", {"form": form, "button_label": "Save", "title": "Edit Account Details"}) + return render(request, "basic_form.html", {"form": form, "button_label": _("Save"), "title": _("Edit Account Details")}) @csrf_protect @@ -155,9 +157,9 @@ def offer_trade(request, offer_id): if sender != recipient: send_offer_email(request, offer, sender, recipient) - messages.success(request, f"{recipient.username} was successfully notified") + messages.success(request, f"{recipient.username} {_('was successfully notified')}!") else: - messages.error(request, "You can't trade with yourself!") + messages.error(request, _("You can't trade with yourself!")) return redirect("offer_detail", offer_id) @@ -180,7 +182,7 @@ def register_user(request): else: form = RegistrationForm() - return render(request, "basic_form.html", {"form": form, "button_label": "Register", "title": "Registeration"}) + return render(request, "basic_form.html", {"form": form, "button_label": _("Register"), "title": _("Registeration")}) def filter_offers(offers, form): diff --git a/pflaenzli/pflaenzli_django/settings.py b/pflaenzli/pflaenzli_django/settings.py index 7f492b0..942faf7 100644 --- a/pflaenzli/pflaenzli_django/settings.py +++ b/pflaenzli/pflaenzli_django/settings.py @@ -129,7 +129,11 @@ LOGOUT_REDIRECT_URL = "/" # Internationalization # https://docs.djangoproject.com/en/4.1/topics/i18n/ -LANGUAGE_CODE = "en-us" +LANGUAGE_CODE = 'de' +LANGUAGES = [ + ('de', 'German'), + ('en', 'English'), +] TIME_ZONE = "Europe/Zurich"