auto detect user language from requests
This commit is contained in:
parent
e7ec475c8c
commit
ecc9bafb94
9 changed files with 72 additions and 50 deletions
|
@ -401,8 +401,6 @@ LANGUAGES = (
|
|||
("zh-hans", _("Simplified Chinese")),
|
||||
("zh-hant", _("Traditional Chinese")),
|
||||
)
|
||||
if SITE_DOMAIN == "neodb.social":
|
||||
LANGUAGES = (("zh-hans", _("Simplified Chinese")),)
|
||||
|
||||
TIME_ZONE = env("NEODB_TIMEZONE", default="Asia/Shanghai") # type: ignore
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ msgid ""
|
|||
msgstr ""
|
||||
"Project-Id-Version: PACKAGE VERSION\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2024-06-03 07:38-0400\n"
|
||||
"POT-Creation-Date: 2024-06-03 09:12-0400\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||
|
@ -19,7 +19,7 @@ msgstr ""
|
|||
msgid "English"
|
||||
msgstr "英语"
|
||||
|
||||
#: boofilsic/settings.py:401 boofilsic/settings.py:405
|
||||
#: boofilsic/settings.py:401
|
||||
msgid "Simplified Chinese"
|
||||
msgstr "简体中文"
|
||||
|
||||
|
@ -1168,8 +1168,8 @@ msgstr "简介"
|
|||
#: catalog/templates/item_review_list.html:50 common/templates/_sidebar.html:37
|
||||
#: common/templates/_sidebar.html:86 common/templates/_sidebar.html:165
|
||||
#: common/templates/_sidebar.html:265
|
||||
#: common/templates/_sidebar_anonymous.html:41
|
||||
#: common/templates/_sidebar_anonymous.html:55
|
||||
#: common/templates/_sidebar_anonymous.html:43
|
||||
#: common/templates/_sidebar_anonymous.html:58
|
||||
#: journal/templates/collection_items.html:8 journal/templates/profile.html:109
|
||||
#: journal/templates/profile.html:151 journal/templates/profile.html:187
|
||||
#: journal/templates/user_collection_list.html:51
|
||||
|
@ -1532,7 +1532,7 @@ msgid "mark all as read"
|
|||
msgstr "全部标为已读"
|
||||
|
||||
#: common/templates/_sidebar.html:30
|
||||
#: common/templates/_sidebar_anonymous.html:48
|
||||
#: common/templates/_sidebar_anonymous.html:51
|
||||
msgid "Popular Tags"
|
||||
msgstr "热门标签"
|
||||
|
||||
|
@ -2973,14 +2973,18 @@ msgid "Manually approve new followers"
|
|||
msgstr "手工审核关注者"
|
||||
|
||||
#: takahe/models.py:435
|
||||
msgid "Include profile and posts in search and discovery"
|
||||
msgstr "允许个人资料和帖文包含在搜索和发现中"
|
||||
msgid "Include profile and posts in discovery"
|
||||
msgstr "允许个人资料和帖文包含在发现中"
|
||||
|
||||
#: takahe/models.py:453
|
||||
#: takahe/models.py:438
|
||||
msgid "Include posts in search results"
|
||||
msgstr "允许个人帖文包含在搜索结果中"
|
||||
|
||||
#: takahe/models.py:456
|
||||
msgid "Profile picture"
|
||||
msgstr "头像"
|
||||
|
||||
#: takahe/models.py:460
|
||||
#: takahe/models.py:463
|
||||
msgid "Header picture"
|
||||
msgstr "背景图片"
|
||||
|
||||
|
@ -3152,39 +3156,39 @@ msgstr "登录信息已更新"
|
|||
msgid "Account mismatch."
|
||||
msgstr "账号信息不匹配。"
|
||||
|
||||
#: users/data.py:124
|
||||
#: users/data.py:127
|
||||
msgid "Generating exports."
|
||||
msgstr "正在生成导出文件。"
|
||||
|
||||
#: users/data.py:136
|
||||
#: users/data.py:139
|
||||
msgid "Export file expired. Please export again."
|
||||
msgstr "导出文件已失效,请重新导出"
|
||||
|
||||
#: users/data.py:147
|
||||
#: users/data.py:150
|
||||
msgid "Sync in progress."
|
||||
msgstr "正在同步。"
|
||||
|
||||
#: users/data.py:161
|
||||
#: users/data.py:164
|
||||
msgid "Settings saved."
|
||||
msgstr "设置已保存"
|
||||
|
||||
#: users/data.py:172
|
||||
#: users/data.py:175
|
||||
msgid "Reset completed."
|
||||
msgstr "重置已完成。"
|
||||
|
||||
#: users/data.py:181
|
||||
#: users/data.py:184
|
||||
msgid "Import in progress."
|
||||
msgstr "正在导出"
|
||||
|
||||
#: users/data.py:183
|
||||
#: users/data.py:186
|
||||
msgid "Invalid URL."
|
||||
msgstr "无效网址。"
|
||||
|
||||
#: users/data.py:197 users/data.py:222 users/data.py:237
|
||||
#: users/data.py:200 users/data.py:225 users/data.py:240
|
||||
msgid "File is uploaded and will be imported soon."
|
||||
msgstr "文件已上传,等待后台导入。"
|
||||
|
||||
#: users/data.py:200 users/data.py:240
|
||||
#: users/data.py:203 users/data.py:243
|
||||
msgid "Invalid file."
|
||||
msgstr "无效文件。"
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ msgid ""
|
|||
msgstr ""
|
||||
"Project-Id-Version: PACKAGE VERSION\n"
|
||||
"Report-Msgid-Bugs-To: \n"
|
||||
"POT-Creation-Date: 2024-06-03 07:38-0400\n"
|
||||
"POT-Creation-Date: 2024-06-03 09:12-0400\n"
|
||||
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
|
||||
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
|
||||
"Language-Team: LANGUAGE <LL@li.org>\n"
|
||||
|
@ -19,7 +19,7 @@ msgstr ""
|
|||
msgid "English"
|
||||
msgstr "英語"
|
||||
|
||||
#: boofilsic/settings.py:401 boofilsic/settings.py:405
|
||||
#: boofilsic/settings.py:401
|
||||
msgid "Simplified Chinese"
|
||||
msgstr "簡體中文"
|
||||
|
||||
|
@ -1168,8 +1168,8 @@ msgstr "簡介"
|
|||
#: catalog/templates/item_review_list.html:50 common/templates/_sidebar.html:37
|
||||
#: common/templates/_sidebar.html:86 common/templates/_sidebar.html:165
|
||||
#: common/templates/_sidebar.html:265
|
||||
#: common/templates/_sidebar_anonymous.html:41
|
||||
#: common/templates/_sidebar_anonymous.html:55
|
||||
#: common/templates/_sidebar_anonymous.html:43
|
||||
#: common/templates/_sidebar_anonymous.html:58
|
||||
#: journal/templates/collection_items.html:8 journal/templates/profile.html:109
|
||||
#: journal/templates/profile.html:151 journal/templates/profile.html:187
|
||||
#: journal/templates/user_collection_list.html:51
|
||||
|
@ -1532,7 +1532,7 @@ msgid "mark all as read"
|
|||
msgstr "全部標爲已讀"
|
||||
|
||||
#: common/templates/_sidebar.html:30
|
||||
#: common/templates/_sidebar_anonymous.html:48
|
||||
#: common/templates/_sidebar_anonymous.html:51
|
||||
msgid "Popular Tags"
|
||||
msgstr "熱門標籤"
|
||||
|
||||
|
@ -2975,14 +2975,18 @@ msgid "Manually approve new followers"
|
|||
msgstr "手工審覈關注者"
|
||||
|
||||
#: takahe/models.py:435
|
||||
msgid "Include profile and posts in search and discovery"
|
||||
msgstr "允許個人資料和帖文包含在搜索和發現中"
|
||||
msgid "Include profile and posts in discovery"
|
||||
msgstr "允許個人資料和帖文包含在發現中"
|
||||
|
||||
#: takahe/models.py:453
|
||||
#: takahe/models.py:438
|
||||
msgid "Include posts in search results"
|
||||
msgstr "允許帖文包含在搜索結果中"
|
||||
|
||||
#: takahe/models.py:456
|
||||
msgid "Profile picture"
|
||||
msgstr "頭像"
|
||||
|
||||
#: takahe/models.py:460
|
||||
#: takahe/models.py:463
|
||||
msgid "Header picture"
|
||||
msgstr "背景圖片"
|
||||
|
||||
|
@ -3154,39 +3158,39 @@ msgstr "登錄信息已更新"
|
|||
msgid "Account mismatch."
|
||||
msgstr "賬號信息不匹配。"
|
||||
|
||||
#: users/data.py:124
|
||||
#: users/data.py:127
|
||||
msgid "Generating exports."
|
||||
msgstr "正在生成導出文件。"
|
||||
|
||||
#: users/data.py:136
|
||||
#: users/data.py:139
|
||||
msgid "Export file expired. Please export again."
|
||||
msgstr "導出文件已失效,請重新導出"
|
||||
|
||||
#: users/data.py:147
|
||||
#: users/data.py:150
|
||||
msgid "Sync in progress."
|
||||
msgstr "正在同步。"
|
||||
|
||||
#: users/data.py:161
|
||||
#: users/data.py:164
|
||||
msgid "Settings saved."
|
||||
msgstr "設置已保存"
|
||||
|
||||
#: users/data.py:172
|
||||
#: users/data.py:175
|
||||
msgid "Reset completed."
|
||||
msgstr "重置已完成。"
|
||||
|
||||
#: users/data.py:181
|
||||
#: users/data.py:184
|
||||
msgid "Import in progress."
|
||||
msgstr "正在導出"
|
||||
|
||||
#: users/data.py:183
|
||||
#: users/data.py:186
|
||||
msgid "Invalid URL."
|
||||
msgstr "無效網址。"
|
||||
|
||||
#: users/data.py:197 users/data.py:222 users/data.py:237
|
||||
#: users/data.py:200 users/data.py:225 users/data.py:240
|
||||
msgid "File is uploaded and will be imported soon."
|
||||
msgstr "文件已上傳,等待後臺導入。"
|
||||
|
||||
#: users/data.py:200 users/data.py:240
|
||||
#: users/data.py:203 users/data.py:243
|
||||
msgid "Invalid file."
|
||||
msgstr "無效文件。"
|
||||
|
||||
|
|
|
@ -17,6 +17,9 @@ NEODB_SITE_LOGO=/logo.png
|
|||
NEODB_SITE_ICON=/icon.png
|
||||
NEODB_SITE_LINKS=@NiceDB=https://donotban.com/@testie,@NeoDB=https://mastodon.social/@neodb
|
||||
|
||||
# default language
|
||||
# NEODB_LANGUAGE=en
|
||||
|
||||
# To enable push notification, generate a keypair from https://web-push-codelab.glitch.me
|
||||
# TAKAHE_VAPID_PUBLIC_KEY=
|
||||
# TAKAHE_VAPID_PRIVATE_KEY=
|
||||
|
|
|
@ -156,7 +156,14 @@ class Migration(migrations.Migration):
|
|||
"discoverable",
|
||||
models.BooleanField(
|
||||
default=True,
|
||||
verbose_name="Include profile and posts in search and discovery",
|
||||
verbose_name="Include profile and posts in discovery",
|
||||
),
|
||||
),
|
||||
(
|
||||
"indexable",
|
||||
models.BooleanField(
|
||||
default=True,
|
||||
verbose_name="Include posts in search results",
|
||||
),
|
||||
),
|
||||
(
|
||||
|
|
|
@ -432,7 +432,10 @@ class Identity(models.Model):
|
|||
)
|
||||
discoverable = models.BooleanField(
|
||||
default=True,
|
||||
verbose_name=_("Include profile and posts in search and discovery"),
|
||||
verbose_name=_("Include profile and posts in discovery"),
|
||||
)
|
||||
indexable = models.BooleanField(
|
||||
default=True, verbose_name=_("Include posts in search results")
|
||||
)
|
||||
|
||||
profile_uri = models.CharField(max_length=500, blank=True, null=True)
|
||||
|
|
|
@ -9,6 +9,7 @@ from django.db.models import Min
|
|||
from django.http import HttpResponse
|
||||
from django.shortcuts import redirect, render
|
||||
from django.urls import reverse
|
||||
from django.utils import translation
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from common.config import *
|
||||
|
@ -64,6 +65,8 @@ def preferences(request):
|
|||
lang = request.POST.get("language")
|
||||
if lang in dict(settings.LANGUAGES).keys() and lang != request.user.language:
|
||||
request.user.language = lang
|
||||
translation.activate(lang)
|
||||
request.LANGUAGE_CODE = translation.get_language()
|
||||
request.user.save(update_fields=["language"])
|
||||
clear_preference_cache(request)
|
||||
return render(
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
from django.conf import settings
|
||||
from django.middleware.locale import LocaleMiddleware
|
||||
from django.utils import translation
|
||||
from django.utils.deprecation import MiddlewareMixin
|
||||
|
||||
|
||||
class LanguageMiddleware(MiddlewareMixin):
|
||||
class LanguageMiddleware(LocaleMiddleware):
|
||||
def process_request(self, request):
|
||||
user_language = settings.LANGUAGE_CODE
|
||||
user_language = None
|
||||
user = getattr(request, "user", None)
|
||||
if user and user.is_authenticated:
|
||||
user_language = getattr(user, "language", "")
|
||||
if user_language not in dict(settings.LANGUAGES).keys():
|
||||
user_language = settings.LANGUAGE_CODE
|
||||
current_language = translation.get_language()
|
||||
if user_language != current_language:
|
||||
translation.activate(user_language)
|
||||
if not user_language:
|
||||
user_language = translation.get_language_from_request(request)
|
||||
# if user_language in dict(settings.LANGUAGES).keys():
|
||||
translation.activate(user_language)
|
||||
request.LANGUAGE_CODE = translation.get_language()
|
||||
|
|
|
@ -13,7 +13,7 @@ from django.db import models
|
|||
from django.db.models import F, Manager, Q, Value
|
||||
from django.db.models.functions import Concat, Lower
|
||||
from django.urls import reverse
|
||||
from django.utils import timezone
|
||||
from django.utils import timezone, translation
|
||||
from django.utils.deconstruct import deconstructible
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from loguru import logger
|
||||
|
@ -452,7 +452,7 @@ class User(AbstractUser):
|
|||
|
||||
new_user = cls(**param)
|
||||
if "language" not in param:
|
||||
new_user.language = settings.LANGUAGE_CODE
|
||||
new_user.language = translation.get_language()
|
||||
new_user.save()
|
||||
Preference.objects.create(user=new_user)
|
||||
if new_user.username: # TODO make username required in registeration
|
||||
|
|
Loading…
Add table
Reference in a new issue