Skip to content

Add localization activation ability #275

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions django_rq/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,8 @@

# Token for querying statistics
API_TOKEN = getattr(settings, 'RQ_API_TOKEN', '')

# Activate locale for each task
RQ_USE_LOCALIZATION = getattr(settings, 'RQ_USE_LOCALIZATION', False)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we rename this to RQ_USE_L10N to keep it consistent with Django's naming scheme? RQ_LANGUAGE_CODE defaults to Django's LANGUAGE_CODE setting, should we also change this to default to Django's USE_L10N setting?

# Use specified language code to activate locale
RQ_LANGUAGE_CODE = getattr(settings, 'RQ_LANGUAGE_CODE', settings.LANGUAGE_CODE)
13 changes: 10 additions & 3 deletions django_rq/workers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from django.conf import settings as django_settings
from django.utils import six
from django.utils.translation import activate
from rq import Worker
from rq.utils import import_attribute

from django.conf import settings
from django.utils import six

from . import settings
from .jobs import get_job_class
from .queues import filter_connection_params, get_connection, get_queues

Expand Down Expand Up @@ -38,6 +39,11 @@ def get_worker_class(worker_class=None):
return worker_class


def activate_localization():
if getattr(settings, 'RQ_USE_LOCALIZATION', False):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please move this variable to DjangoRQ's settings.py?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can simplify this to if settings.RQ_USE_LOCALIZATION:

activate(getattr(settings, 'RQ_LANGUAGE_CODE', django_settings.LANGUAGE_CODE))


def get_worker(*queue_names, **kwargs):
"""
Returns a RQ worker for all queues or specified ones.
Expand All @@ -49,6 +55,7 @@ def get_worker(*queue_names, **kwargs):
# normalize queue_class to what get_queues returns
queue_class = queues[0].__class__
worker_class = get_worker_class(kwargs.pop('worker_class', None))
activate_localization()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can simply do this (and delete activate_localization()):

if settings.RQ_USE_LOCALIZATION:
    activate(settings. RQ_LANGUAGE_CODE)

return worker_class(queues,
connection=queues[0].connection,
exception_handlers=get_exception_handlers() or None,
Expand Down