Skip to content

Commit 1519cd4

Browse files
committed
feat: Added documentation for multi-lingual notifications
1 parent 1e33549 commit 1519cd4

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,104 @@ notifications = get_notifications(user).prefetch_related(
387387

388388
The email channel (EmailChannel) will send real-time emails using Django’s built-in `send_mail` method. This is a blocking function call, meaning that while a connection with the SMTP server is made and the email is sent off, the process that’s sending the notification has to wait. This is not ideal, but easily solved by using something like [django-mailer](https://github.com/pinax/django-mailer/), which provides a queueing backend for `send_mail`. This means that sending email is no longer a blocking action.
389389

390+
## Multilingual Notifications
391+
392+
For applications that support multiple languages, you have two main approaches to handle translatable notification content.
393+
394+
### Approach 1: store parameters per language in metadata
395+
396+
Store translated parameters for each language in the `metadata` field and use Django's translation system in `get_text()`:
397+
398+
```python
399+
from django.utils.translation import gettext as _, get_language
400+
401+
@register
402+
class CommentNotificationType(NotificationType):
403+
key = "comment"
404+
name = "Comments"
405+
description = "When someone comments on your content"
406+
407+
def get_text(self, notification):
408+
current_lang = get_language()
409+
# Get parameters for current language, fallback to English
410+
lang_params = notification.metadata.get(current_lang, notification.metadata.get("en", {}))
411+
412+
return _("%(commenter_name)s commented on %(page_title)s") % lang_params
413+
414+
# When creating the notification
415+
from django.conf import settings
416+
from django.utils.translation import activate, get_language
417+
418+
def create_multilingual_notification(recipient, commenter, page):
419+
current_lang = get_language()
420+
multilingual_metadata = {}
421+
422+
# Store parameters for each language
423+
for lang_code, _ in settings.LANGUAGES:
424+
activate(lang_code)
425+
multilingual_metadata[lang_code] = {
426+
"commenter_name": commenter.get_full_name(),
427+
"page_title": page.get_title(), # Assumes this returns translated title
428+
}
429+
430+
activate(current_lang) # Restore original language
431+
432+
send_notification(
433+
recipient=recipient,
434+
notification_type=CommentNotificationType,
435+
actor=commenter,
436+
target=page,
437+
metadata=multilingual_metadata
438+
)
439+
```
440+
441+
**Pros**: Best query performance
442+
**Cons**: Parameters are "frozen" when notification is created, more database storage needed
443+
444+
### Approach 2: dynamic translation via target
445+
446+
Use the `target` relationship to access current translated data:
447+
448+
```python
449+
@register
450+
class CommentNotificationType(NotificationType):
451+
key = "comment"
452+
name = "Comments"
453+
description = "When someone comments on your content"
454+
455+
def get_text(self, notification):
456+
from django.utils.translation import gettext as _
457+
458+
# Access current language data from the target
459+
if notification.target:
460+
return _("%(commenter)s commented on %(page_title)s") % {
461+
"commenter": notification.actor.get_full_name(),
462+
"page_title": notification.target.get_title() # Assumes this returns translated title
463+
}
464+
return self.description
465+
466+
# Usage is simple - just send the notification
467+
send_notification(
468+
recipient=page.author,
469+
notification_type=CommentNotificationType,
470+
actor=commenter,
471+
target=page
472+
)
473+
```
474+
475+
**Pros**: Always current data, minimal storage, simpler code
476+
**Cons**: Requires proper prefetching for performance
477+
478+
### Performance considerations
479+
480+
| Approach | Storage Overhead | Query Performance | Translation Freshness |
481+
|----------|------------------|-------------------|----------------------|
482+
| Approach 1 | Moderate | Excellent | Frozen when created |
483+
| Approach 2 | None | Good (with prefetching) | Always current |
484+
485+
- Use **approach 1** if you have performance-critical displays and can accept that text is frozen when the notification is created
486+
- Use **approach 2** if you need always-current data
487+
390488
## Example App
391489

392490
An example app is provided, which shows how to create a custom notification type, how to send a notification, it has a nice looking notification center with unread notifications as well as an archive of all read notifications, plus a settings view where you can manage notification preferences.

0 commit comments

Comments
 (0)