Skip to content

Commit

Permalink
Merge pull request #2242 from sopel-irc/translate-zh-flags
Browse files Browse the repository at this point in the history
translate: update `langcode()` helper to support Chinese
  • Loading branch information
dgw authored Feb 6, 2022
2 parents e223044 + 037bb32 commit c812145
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion sopel/modules/translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import json
import logging
import random
import re

import requests

Expand Down Expand Up @@ -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']

Expand Down

0 comments on commit c812145

Please sign in to comment.