Skip to content

Commit

Permalink
generalize some language utils and add pt-BR
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name authored and alphatownsman committed Nov 30, 2024
1 parent 0e32bb2 commit 5600329
Show file tree
Hide file tree
Showing 11 changed files with 144 additions and 96 deletions.
4 changes: 1 addition & 3 deletions catalog/book/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@
from catalog.common.models import (
LIST_OF_ONE_PLUS_STR_SCHEMA,
LOCALE_CHOICES_JSONFORM,
SCRIPT_CHOICES,
LanguageListField,
)
from common.models.lang import get_current_locales
from common.models.misc import uniq
from common.models import uniq

from .utils import *

Expand Down
13 changes: 3 additions & 10 deletions catalog/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,7 @@
from polymorphic.models import PolymorphicModel

from catalog.common import jsondata
from common.models import (
LANGUAGE_CHOICES,
LOCALE_CHOICES,
PREFERRED_LOCALES,
SCRIPT_CHOICES,
)
from common.models.lang import get_current_locales
from common.models.misc import uniq
from common.models import LANGUAGE_CHOICES, LOCALE_CHOICES, get_current_locales, uniq

from .utils import item_cover_path, resource_cover_path

Expand Down Expand Up @@ -614,7 +607,7 @@ def class_name(self) -> str:

def get_localized_title(self) -> str | None:
if self.localized_title:
locales = get_current_locales() + PREFERRED_LOCALES
locales = get_current_locales()
for loc in locales:
v = next(
filter(lambda t: t["lang"] == loc, self.localized_title), {}
Expand All @@ -624,7 +617,7 @@ def get_localized_title(self) -> str | None:

def get_localized_description(self) -> str | None:
if self.localized_description:
locales = get_current_locales() + PREFERRED_LOCALES
locales = get_current_locales()
for loc in locales:
v = next(
filter(lambda t: t["lang"] == loc, self.localized_description), {}
Expand Down
14 changes: 13 additions & 1 deletion catalog/common/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
from django.test import TestCase

from common.models import detect_language
from common.models import (
LANGUAGE_CHOICES,
LOCALE_CHOICES,
SCRIPT_CHOICES,
SITE_DEFAULT_LANGUAGE,
SITE_PREFERRED_LANGUAGES,
SITE_PREFERRED_LOCALES,
detect_language,
)


class CommonTestCase(TestCase):
Expand All @@ -13,3 +21,7 @@ def test_detect_lang(self):
self.assertEqual(lang, "zh-cn")
lang = detect_language("巫师3:狂猎 The Witcher 3: Wild Hunt")
self.assertEqual(lang, "zh-cn")

def test_lang_list(self):
self.assertGreaterEqual(len(SITE_PREFERRED_LANGUAGES), 1)
self.assertGreaterEqual(len(SITE_PREFERRED_LOCALES), 1)
6 changes: 2 additions & 4 deletions catalog/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from catalog.models import *
from common.forms import PreviewImageInput
from common.models import DEFAULT_CATALOG_LANGUAGE, detect_language, uniq
from common.models import SITE_DEFAULT_LANGUAGE, detect_language, uniq

CatalogForms = {}

Expand Down Expand Up @@ -77,9 +77,7 @@ def migrate_initial(self):
for t in self.instance.other_title:
titles.append({"lang": detect_language(t), "text": t})
if not titles:
titles = [
{"lang": DEFAULT_CATALOG_LANGUAGE, "text": "<no title>"}
]
titles = [{"lang": SITE_DEFAULT_LANGUAGE, "text": "<no title>"}]
self.initial["localized_title"] = uniq(titles) # type:ignore
if (
"localized_description" in self.Meta.fields
Expand Down
5 changes: 2 additions & 3 deletions catalog/jobs/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@

from boofilsic.settings import MIN_MARKS_FOR_DISCOVER
from catalog.models import *
from common.models import BaseJob, JobManager
from common.models.lang import PREFERRED_LOCALES
from common.models import SITE_PREFERRED_LOCALES, BaseJob, JobManager
from journal.models import (
Collection,
Comment,
Expand Down Expand Up @@ -43,7 +42,7 @@ def get_popular_marked_item_ids(self, category, days, exisiting_ids):
qs = qs.filter(local=True)
if settings.DISCOVER_FILTER_LANGUAGE:
q = None
for loc in PREFERRED_LOCALES:
for loc in SITE_PREFERRED_LOCALES:
if q:
q = q | Q(item__metadata__localized_title__contains=[{"lang": loc}])
else:
Expand Down
8 changes: 4 additions & 4 deletions catalog/sites/apple_music.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from catalog.common import *
from catalog.models import *
from common.models.lang import (
DEFAULT_CATALOG_LANGUAGE,
PREFERRED_LANGUAGES,
SITE_DEFAULT_LANGUAGE,
SITE_PREFERRED_LANGUAGES,
detect_language,
)
from common.models.misc import uniq
Expand Down Expand Up @@ -56,7 +56,7 @@ def id_to_url(cls, id_value):

def get_locales(self):
locales = {}
for lang in PREFERRED_LANGUAGES:
for lang in SITE_PREFERRED_LANGUAGES:
match lang:
case "zh":
locales.update({"zh": ["cn", "tw", "hk", "sg"]})
Expand Down Expand Up @@ -98,7 +98,7 @@ def scrape(self):
localized_title.append({"lang": tl, "text": title})
if brief:
localized_desc.append({"lang": tl, "text": brief})
if lang == DEFAULT_CATALOG_LANGUAGE or not matched_content:
if lang == SITE_DEFAULT_LANGUAGE or not matched_content:
matched_content = content
break
except Exception:
Expand Down
4 changes: 2 additions & 2 deletions catalog/sites/steam.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from catalog.common import *
from catalog.models import *
from common.models.lang import PREFERRED_LANGUAGES
from common.models.lang import SITE_PREFERRED_LANGUAGES
from journal.models.renderers import html_to_text

from .igdb import search_igdb_by_3p_url
Expand All @@ -16,7 +16,7 @@

def _get_preferred_languages():
langs = {}
for la in PREFERRED_LANGUAGES:
for la in SITE_PREFERRED_LANGUAGES:
if la == "zh":
langs.update({"zh-cn": "zh-CN", "zh-tw": "zh-TW"})
# zh-HK data is not good
Expand Down
4 changes: 2 additions & 2 deletions catalog/sites/tmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from catalog.common import *
from catalog.movie.models import *
from catalog.tv.models import *
from common.models.lang import PREFERRED_LANGUAGES
from common.models.lang import SITE_PREFERRED_LANGUAGES

from .douban import *

Expand All @@ -37,7 +37,7 @@ def _get_language_code():

def _get_preferred_languages():
langs = {}
for lang in PREFERRED_LANGUAGES:
for lang in SITE_PREFERRED_LANGUAGES:
if lang == "zh":
langs.update({"zh-cn": "zh-CN", "zh-tw": "zh-TW", "zh-hk": "zh-HK"})
else:
Expand Down
7 changes: 4 additions & 3 deletions common/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from .cron import BaseJob, JobManager
from .lang import (
DEFAULT_CATALOG_LANGUAGE,
LANGUAGE_CHOICES,
LOCALE_CHOICES,
PREFERRED_LANGUAGES,
PREFERRED_LOCALES,
SCRIPT_CHOICES,
SITE_DEFAULT_LANGUAGE,
SITE_PREFERRED_LANGUAGES,
SITE_PREFERRED_LOCALES,
detect_language,
get_current_locales,
)
from .misc import uniq
Loading

0 comments on commit 5600329

Please sign in to comment.