From 5d24895f3ba0d4018d287dc3358780971c4d9359 Mon Sep 17 00:00:00 2001 From: dgw Date: Sun, 6 Feb 2022 15:29:56 -0600 Subject: [PATCH] Backport pull request #2242 from sopel-irc/translate-zh-flags translate: update `langcode()` helper to support Chinese --- sopel/modules/translate.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/sopel/modules/translate.py b/sopel/modules/translate.py index 898a3290af..1d6d0b4cfb 100644 --- a/sopel/modules/translate.py +++ b/sopel/modules/translate.py @@ -12,6 +12,7 @@ import json import logging import random +import re import sys import requests @@ -163,7 +164,16 @@ def tr2(bot, trigger): return def langcode(p): - return p.startswith(':') and (2 < len(p) < 10) and p[1:].isalpha() + # TODO: We'd be much better off just using the langcodes PyPI package + # also TODO: it'd be nice not to require the : prefix, which using + # langcodes would make easier in lieu of adding an API key to fetch + # the list of supported languages + prefixed = p.startswith(':') + # the longest codes Google uses (ca. Jan 2022) are zh-CN and zh-TW + short_enough = (2 < len(p) < 8) + # pesky - in Chinese codes forced a switch from .isalpha(); see #2241 + fits_pattern = re.match(r':[a-z\-]+', p.lower()) + return all((prefixed, short_enough, fits_pattern)) args = ['auto', 'en']