-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #564 from Ilhasoft/feature/add_middleware_to_chang…
…e_language Feature/add middleware to change language
- Loading branch information
Showing
5 changed files
with
44 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.utils import translation | ||
from django.utils.deprecation import MiddlewareMixin | ||
|
||
|
||
class UserLanguageMiddleware(MiddlewareMixin): | ||
def process_response(self, request, response): | ||
user = getattr(request, "user", None) | ||
if not user or user.is_anonymous: | ||
return self.get_response(request) | ||
|
||
user_language = getattr(user, "language", None) | ||
if not user_language: | ||
return self.get_response(request) | ||
|
||
translation.activate(user_language) | ||
|
||
response = self.get_response(request) | ||
return response |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# Generated by Django 2.2.19 on 2021-03-31 14:14 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [("authentication", "0007_auto_20210323_0035")] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name="user", | ||
name="language", | ||
field=models.CharField( | ||
blank=True, | ||
choices=[("en", "English"), ("pt-br", "Brazilian Portuguese")], | ||
max_length=5, | ||
null=True, | ||
), | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters