From 037bb32d617be71daa2d8828fcf7901de56a9165 Mon Sep 17 00:00:00 2001 From: dgw Date: Sun, 23 Jan 2022 04:53:00 -0600 Subject: [PATCH] translate: update langcode() helper to support Chinese This is a semi-temporary fix for #2241. It's still not perfect (still allows using unsupported or nonexistent language codes), but there will be additional options available in Sopel 8.0 and beyond. Goal for this is to avoid adding any new dependencies in 7.1.x. --- 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 2e003038f8..d842d48560 100644 --- a/sopel/modules/translate.py +++ b/sopel/modules/translate.py @@ -11,6 +11,7 @@ import json import logging import random +import re import requests @@ -158,7 +159,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']