bpo-31159: fix language switch regex on unknown yet built languages.#3039
bpo-31159: fix language switch regex on unknown yet built languages.#3039vstinner merged 1 commit intopython:masterfrom
Conversation
This fix a regex issue (a missing non-matching group around an 'or' list) and the specific possible case where a translation is built but not yet in known by the picker, but not explicitly listing possible languages in the regex.
| // or '' if not found. | ||
| function language_segment_from_url(url) { | ||
| var language_regexp = '\.org/(' + Object.keys(all_languages).join('|') + '/)'; | ||
| var language_regexp = '\.org/([a-z]{2}(?:-[a-z]{2})?/)'; |
There was a problem hiding this comment.
I would prefer to keep using all_languages and just add (?: xxx ) to the regex.
There was a problem hiding this comment.
At first I would prefer to keep it too, but I though:
var language_regexp = '\.org/((?:' + Object.keys(all_languages).join('|') + '|([a-z]{2}(?:-[a-z]{2})?)/)';
was pretty unreadable/unmaintainable just to match a language tag in an URL.
Sure we can enhance it by building a list of "language tag regexes" based on all_languages and adding the [a-z]{2}(?:-[a-z]{2})? to the end of it, then concatenating them all using the join('|'). Still a bit huge but more maintainable.
In every cases we have to keep the wildcardy part [a-z]{2}(?:-[a-z]{2})? to allow matching still-unknown languages (ones that are built, but not yet in the switcher, which is a supported case, see: https://www.python.org/dev/peps/pep-0545/#add-translation-to-the-language-switcher).
Or... we may add an exhaustive list of languages, not used to build the switcher but used to build the regex, containing every language tag we expect in the future, so when a translation is built, the switcher already knows it.
In any cases I find ([a-z]{2}(?:-[a-z]{2})?/) easier to read and maintain, even if it has a little chance to collide with a version in the future: if we introduce a version like dev but containing only two letters (or four letters separated by a dash). But I don't see this happen.
|
Ok, it makes sense. I merged your PR. |
…ython#3039) This fix a regex issue (a missing non-matching group around an 'or' list) and the specific possible case where a translation is built but not yet in known by the picker, but not explicitly listing possible languages in the regex. (cherry picked from commit 122081d)
#3051) * bpo-31159: fix language switch regex on unknown yet built languages. (#3039) This fix a regex issue (a missing non-matching group around an 'or' list) and the specific possible case where a translation is built but not yet in known by the picker, but not explicitly listing possible languages in the regex. (cherry picked from commit 122081d) * bpo-31149: Doc: Add Japanese to the language switcher. (#3028) (cherry picked from commit c82b7f3)
python#3051) * bpo-31159: fix language switch regex on unknown yet built languages. (python#3039) This fix a regex issue (a missing non-matching group around an 'or' list) and the specific possible case where a translation is built but not yet in known by the picker, but not explicitly listing possible languages in the regex. (cherry picked from commit 122081d) * bpo-31149: Doc: Add Japanese to the language switcher. (python#3028) (cherry picked from commit c82b7f3) (cherry picked from commit e8e7fba)
#3051) (#3081) * bpo-31159: fix language switch regex on unknown yet built languages. (#3039) This fix a regex issue (a missing non-matching group around an 'or' list) and the specific possible case where a translation is built but not yet in known by the picker, but not explicitly listing possible languages in the regex. (cherry picked from commit 122081d) * bpo-31149: Doc: Add Japanese to the language switcher. (#3028) (cherry picked from commit c82b7f3) (cherry picked from commit e8e7fba)
This fix a regex issue (a missing non-matching group around an 'or'
list) and the specific possible case where a translation is built but
not yet in known by the picker, but not explicitly listing possible
languages in the regex.
https://bugs.python.org/issue31159