Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translate: update langcode() helper to support Chinese #2242

Merged
merged 1 commit into from
Feb 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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())
Exirel marked this conversation as resolved.
Show resolved Hide resolved
return all((prefixed, short_enough, fits_pattern))

args = ['auto', 'en']

Expand Down