From fe8ea04d18bd5479e5389fcce1e91a6c88137d61 Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Sat, 7 May 2022 14:19:06 -0700 Subject: [PATCH] Add support for i18n and l10n The majority of the words on the site are user-generated, but I've tried to surround everything else with localization calls. I don't know another language so this will have to do until someone decides to translate it lol Signed-off-by: Alek Ratzloff --- board/models.py | 8 +++++--- board/templates/board/board_detail.html | 13 ++++++++++--- board/templates/board/post_detail.html | 9 ++++++++- board/templates/board/post_snippet.html | 14 +++++++++----- threadchat/settings.py | 2 +- 5 files changed, 33 insertions(+), 13 deletions(-) diff --git a/board/models.py b/board/models.py index c3286cf..ae196b0 100644 --- a/board/models.py +++ b/board/models.py @@ -9,6 +9,7 @@ from django.core.files.base import ContentFile from django.dispatch import receiver from django.urls import reverse from django.utils import timezone +from django.utils.translation import gettext as _ from PIL import Image from io import BytesIO @@ -152,11 +153,11 @@ class Post(models.Model): # Make sure there is at least some content self.text = self.text.strip() if not (self.text or self.image): - raise ValidationError("Please either write a message or upload an image") + raise ValidationError(_("Please either write a message or upload an image")) # Image upload size check if self.image and self.image.size > settings.MAX_UPLOAD_SIZE: raise ValidationError( - "Image supplied is too large. Maximum image size is %(max)s", + _("Image supplied is too large. Maximum image size is %(max)s"), params={"max": settings.MAX_UPLOAD_SIZE}, ) # Rate limiting for posts @@ -169,7 +170,8 @@ class Post(models.Model): if delta < self.board.post_cooldown: cooldown = self.board.post_cooldown - delta raise ValidationError( - f"Please wait {int(cooldown.total_seconds())} seconds before posting again" + _(f"Please wait %(cooldown)s seconds before posting again"), + params={"cooldown": int(cooldown.total_seconds())}, ) diff --git a/board/templates/board/board_detail.html b/board/templates/board/board_detail.html index bf56a8d..88d1f65 100644 --- a/board/templates/board/board_detail.html +++ b/board/templates/board/board_detail.html @@ -1,5 +1,6 @@ {% extends "board/base.html" %} {% load post_body %} +{% load l10n %} {% block title %} {% with title=board.url %} @@ -22,15 +23,21 @@
 
-

Create a new thread

+

{% localize on %}Create a new thread{% endlocalize %}

{% csrf_token %} {{ form.as_table }} - - + + + + +
 Max image size: {{ max_upload_size|measure_bytes }}
 
  + {% localize on %}Max image size{% endlocalize %}: + {{ max_upload_size|measure_bytes }} +
 
diff --git a/board/templates/board/post_detail.html b/board/templates/board/post_detail.html index 93c4310..98e4b8c 100644 --- a/board/templates/board/post_detail.html +++ b/board/templates/board/post_detail.html @@ -1,5 +1,6 @@ {% extends "board/base.html" %} {% load post_body %} +{% load l10n %} {% block title %} {% with title=board.url %} @@ -25,7 +26,13 @@ {% csrf_token %} {{ form.as_table }} - + + + +
 Max image size: {{ max_upload_size|measure_bytes }}
  + {% localize on %}Max image size{% endlocalize %}: + {{ max_upload_size|measure_bytes }} +
 
diff --git a/board/templates/board/post_snippet.html b/board/templates/board/post_snippet.html index a0d93d8..f20bf0c 100644 --- a/board/templates/board/post_snippet.html +++ b/board/templates/board/post_snippet.html @@ -1,10 +1,12 @@ {% load post_body %} +{% load l10n %}
{# Image #} {% if post.thumbnail %} {# Image info #}
- File: {{post.original_image_name}} + {% localize on %}File{% endlocalize %}: + {{post.original_image_name}} ({{post.image.size|measure_bytes}}, {{post.image_width}}x{{post.image_height}})
@@ -23,17 +25,19 @@ {% if post.subject %} {{post.subject}} {% endif %} -by {{post.name|default:"Anonymous"}} -at {{post.created}} +{% localize on %}by{% endlocalize %} +{{post.name|default:"Anonymous"}} +{% localize on %}at{% endlocalize %} +{{post.created}} {% if reply_link %} - [Reply] + [{% localize on %}Reply{% endlocalize %}] {% endif %} {# "X replies elided" dialog for OPs on the board #} {% if replies_elided > 0 %}
- ({{replies_elided}} replies elided, click reply to view) + ({% localize on %}{{replies_elided}} replies elided, click reply to view{% endlocalize %}) {% endif %} diff --git a/threadchat/settings.py b/threadchat/settings.py index 70417ef..a9918ed 100644 --- a/threadchat/settings.py +++ b/threadchat/settings.py @@ -108,7 +108,7 @@ AUTH_PASSWORD_VALIDATORS = [ LANGUAGE_CODE = "en-us" -TIME_ZONE = "UTC" +TIME_ZONE = "US/Pacific" USE_I18N = True