From 4ba13807a4aa5e3552af106c62964bd186469f97 Mon Sep 17 00:00:00 2001 From: jrfnl Date: Fri, 9 Jul 2021 09:18:39 +0200 Subject: [PATCH] PHPMailer::setLanguage(): allow for "lang(_script)?(_country)?" codes Language codes which were "language-script" based were not accepted by the regex used, which meant that the `sr_latn` script could never be loaded. After discussion in 2418, it was decided to support "script" in a language code and to support it like so: ``` 2-character language code [_] optional 4-character script code [_] optional 2-character country code ``` This combines the annotation forms of the following known standards: * https://unicode-org.github.io/cldr-staging/charts/37/summary/root.html * https://docs.oracle.com/cd/E23824_01/html/E26033/glset.html * http://www.loc.gov/standards/iso639-2/php/code_list.php This means that all of the below codes will now pass the language code validation: ``` sr sr_latn sr_rs sr_latn_rs ``` But not: ``` sr_rs_latn ``` This commit applies the above change and also adjusts the "language code fall back" logic to take language codes with a script code into account. Note: if the requested full "language-script-country" code is not available, "language-country" will take precedence over "language-script" for the fallback logic to find the appropriate translation file. Related to 2418 - observation 4 --- src/PHPMailer.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/PHPMailer.php b/src/PHPMailer.php index 25ac1791..d9a3b94d 100644 --- a/src/PHPMailer.php +++ b/src/PHPMailer.php @@ -2189,6 +2189,8 @@ class PHPMailer * The default language is English. * * @param string $langcode ISO 639-1 2-character language code (e.g. French is "fr") + * Optionally, the language code can be enhanced with a 4-character + * script annotation and/or a 2-character country annotation. * @param string $lang_path Path to the language file directory, with trailing separator (slash).D * Do not set this from user input! * @@ -2251,7 +2253,10 @@ class PHPMailer //Validate $langcode $foundlang = true; $langcode = strtolower($langcode); - if (!preg_match('/^(?P[a-z]{2})(?P_[a-z]{2})?$/', $langcode, $matches) && $langcode !== 'en') { + if ( + !preg_match('/^(?P[a-z]{2})(?P