From 43f74145736657f849718969bfd1343205b473e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Fri, 6 Sep 2024 21:15:08 +0200 Subject: [PATCH] UwU suppowt --- .gitignore | 1 + docker/Dockerfile | 5 ++- src/c3nav/mapdata/fields.py | 44 +++++++++++++++++++++++ src/c3nav/settings.py | 21 +++++++++-- src/genuwu.py | 69 +++++++++++++++++++++++++++++++++++++ src/requirements/uwu.txt | 2 ++ 6 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 src/genuwu.py create mode 100644 src/requirements/uwu.txt diff --git a/.gitignore b/.gitignore index 234cc32f..db625dcb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,7 @@ loadproduction.sh runtileserver.sh test.py test.svg +en_UW .idea diff --git a/docker/Dockerfile b/docker/Dockerfile index 22bcbee0..1c923271 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -69,6 +69,7 @@ RUN --mount=type=cache,target=/pip-cache \ -r requirements/rsvg.txt \ -r requirements/sentry.txt \ -r requirements/metrics.txt \ + -r requirements/uwu.txt \ -r requirements/server-asgi.txt && \ pip install --cache-dir /pip-cache uwsgi @@ -94,7 +95,9 @@ ENV C3NAV_DEBUG="" \ USER c3nav WORKDIR /app -RUN /app/env/bin/python manage.py compilemessages && \ +RUN /app/env/bin/python manage.py makemessages -l en_UW && \ + /app/env/bin/python genuwu.py && \ + /app/env/bin/python manage.py compilemessages && \ /app/env/bin/python manage.py collectstatic -l --no-input && \ /app/env/bin/python manage.py compress && \ rm -r /data/* diff --git a/src/c3nav/mapdata/fields.py b/src/c3nav/mapdata/fields.py index 998720cf..71b06785 100644 --- a/src/c3nav/mapdata/fields.py +++ b/src/c3nav/mapdata/fields.py @@ -1,6 +1,8 @@ import json import logging +import re import typing +from itertools import chain from django.conf import settings from django.core.exceptions import ValidationError @@ -19,6 +21,30 @@ from c3nav.mapdata.utils.json import format_geojson logger = logging.getLogger('c3nav') +if "en-UW" in settings.SELECTED_LANGUAGES: + from uwuipy import Uwuipy + uwu = Uwuipy( + stutter_chance=0, + face_chance=0, + action_chance=0, + exclamation_chance=0, + nsfw_actions=False, + power=4, + ) + uwu_more = Uwuipy( + stutter_chance=0, + action_chance=0, + exclamation_chance=0, + power=4, + ) + uwu_most = Uwuipy( + stutter_chance=0, + action_chance=0.05, + face_chance=0.1, + nsfw_actions=False, + power=4, + ) + def validate_geometry(geometry: BaseGeometry): if not isinstance(geometry, BaseGeometry): @@ -145,11 +171,29 @@ class JSONField(models.TextField): return self.get_prep_value(value) +special_pattern = r'(%%|%(\([^)]*\))?[^a-z]*[a-z]|<[^>]*>|\{[^}]*\})' + + def get_i18n_value(i18n_dict, fallback_language, fallback_any, fallback_value): lang = get_language() if i18n_dict: if lang in i18n_dict: return i18n_dict[lang] + if lang == "en-uw" and "en" in i18n_dict: + owiginal = i18n_dict["en"] + stripped_owiginal = re.sub(special_pattern, '{}', owiginal) + specials = [item[0] for item in re.findall(special_pattern, owiginal)] + num_wowds = len(stripped_owiginal.split("(")[0].split()) + if num_wowds >= 8: + twanslated = uwu_most.uwuify(stripped_owiginal) + elif num_wowds >= 3: + twanslated = uwu_more.uwuify(stripped_owiginal) + else: + twanslated = uwu.uwuify(stripped_owiginal) + twanslated = twanslated.replace('***', '*').replace(r'\<', '<').replace(r'\>', '>') + if specials: + twanslated = ''.join(chain(*zip(twanslated.split('{}'), specials + [""]))) + return twanslated if fallback_language in i18n_dict: return i18n_dict[fallback_language] if fallback_any: diff --git a/src/c3nav/settings.py b/src/c3nav/settings.py index 38c580cd..f5620c50 100644 --- a/src/c3nav/settings.py +++ b/src/c3nav/settings.py @@ -444,10 +444,27 @@ LOCALE_PATHS = ( PROJECT_DIR / 'locale', ) -LANGUAGES = [ +EXTRA_LANG_INFO = { + 'en-UW': { + 'bidi': False, + 'code': 'en-UW', + 'name': 'Engwish UwU', + 'name_local': u'Engwish UwU', #unicode codepoints here + }, +} + +# Add custom languages not provided by Django +import django.conf.locale +LANG_INFO = dict(django.conf.locale.LANG_INFO, **EXTRA_LANG_INFO) +django.conf.locale.LANG_INFO = LANG_INFO + +SELECTED_LANGUAGES = frozenset(config.get('c3nav', 'languages', fallback='en,de').split(',')) +LANGUAGES = [(code, name) for code, name in [ ('en', _('English')), + ('en-UW', _('Engwish UwU')), ('de', _('German')), -] +] if code in SELECTED_LANGUAGES] +TILE_CACHE_SERVER = config.get('c3nav', 'languagestile_cache_server', fallback=None) template_loaders = ( 'django.template.loaders.filesystem.Loader', diff --git a/src/genuwu.py b/src/genuwu.py new file mode 100644 index 00000000..c05bef5d --- /dev/null +++ b/src/genuwu.py @@ -0,0 +1,69 @@ +import re +import sys +from itertools import chain, zip_longest +from pathlib import Path + +import polib + +file = Path(__file__).resolve().parent / "c3nav" / "locale" / "en_UW" / "LC_MESSAGES" / "django.po" +if not file.exists(): + print('Run makemessages -l en_UW first!') + sys.exit(1) + +po = polib.pofile(file) + +from uwuipy import Uwuipy +uwu = Uwuipy( + seed=1337, + stutter_chance=0, + face_chance=0, + action_chance=0, + exclamation_chance=0, + nsfw_actions=False, + power=4, +) +uwu_more = Uwuipy( + seed=1337, + stutter_chance=0, + action_chance=0, + exclamation_chance=0, + power=4, +) +uwu_most = Uwuipy( + seed=1337, + stutter_chance=0, + action_chance=0.05, + face_chance=0.1, + nsfw_actions=False, + power=4, +) + +special_pattern = r'(%%|%(\([^)]*\))?[^a-z]*[a-z]|<[^>]*>|\{[^}]*\})' + + +def uwuify(owiginal): + stripped_owiginal = re.sub(special_pattern, '{}', owiginal) + specials = [item[0] for item in re.findall(special_pattern, owiginal)] + num_wowds = len(stripped_owiginal.split("(")[0].split()) + if num_wowds >= 8: + twanslated = uwu_most.uwuify(stripped_owiginal) + elif num_wowds >= 3: + twanslated = uwu_more.uwuify(stripped_owiginal) + else: + twanslated = uwu.uwuify(stripped_owiginal) + twanslated = twanslated.replace('***', '*').replace(r'\<', '<').replace(r'\>', '>') + if specials: + twanslated = ''.join(chain(*zip(twanslated.split('{}'), specials+[""]))) + return twanslated + + +po.metadata["Plural-Forms"] = "nplurals=2; plural=(n != 1);" +for entry in po: + if entry.msgid_plural: + entry.msgstr_plural[0] = uwuify(entry.msgid) + entry.msgstr_plural[1] = uwuify(entry.msgid_plural) + else: + entry.msgstr = uwuify(entry.msgid) + +po.save(file) + diff --git a/src/requirements/uwu.txt b/src/requirements/uwu.txt new file mode 100644 index 00000000..683bb49b --- /dev/null +++ b/src/requirements/uwu.txt @@ -0,0 +1,2 @@ +uwuify==1.3.1 +polib==1.2.0 \ No newline at end of file