Skip to content

Commit

Permalink
PHPMailer::setLanguage(): make return value consistent
Browse files Browse the repository at this point in the history
The return value of the method was inconsistent as it could return `true` even when the requested language was not loaded/set.

Previously, the method would:
* Return `false` when a `$langcode` was matched against the regex, but the file couldn't be loaded.
* Return `true` in all other cases, including when a `$langcode` other than `'en'` was passed, but it didn't match the regex, which meant that effectively the requested language was ignored and English was loaded anyway, but the function would still return `true`.

This has now been changed to:
* Return `true` when the requested language was loaded, whether `'en'` or another language.
* Return `false` when the requested language wasn't loaded and the language defaulted to English.

Related to PHPMailer#2418 (comment)
  • Loading branch information
jrfnl committed Jul 13, 2021
1 parent 5c64f7e commit 9182613
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/PHPMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2186,14 +2186,13 @@ public function smtpClose()

/**
* Set the language for error messages.
* Returns false if it cannot load the language file.
* The default language is English.
*
* @param string $langcode ISO 639-1 2-character language code (e.g. French is "fr")
* @param string $lang_path Path to the language file directory, with trailing separator (slash).D
* Do not set this from user input!
*
* @return bool
* @return bool Returns true if the requested language was loaded, false otherwise.
*/
public function setLanguage($langcode = 'en', $lang_path = '')
{
Expand Down Expand Up @@ -2248,11 +2247,14 @@ public function setLanguage($langcode = 'en', $lang_path = '')
//Calculate an absolute path so it can work if CWD is not here
$lang_path = dirname(__DIR__) . DIRECTORY_SEPARATOR . 'language' . DIRECTORY_SEPARATOR;
}

//Validate $langcode
if (!preg_match('/^[a-z]{2}(?:_[a-zA-Z]{2})?$/', $langcode)) {
$foundlang = true;
if (!preg_match('/^[a-z]{2}(?:_[a-zA-Z]{2})?$/', $langcode) && $langcode !== 'en') {
$foundlang = false;
$langcode = 'en';
}
$foundlang = true;

$lang_file = $lang_path . 'phpmailer.lang-' . $langcode . '.php';
//There is no English translation file
if ('en' !== $langcode) {
Expand Down

0 comments on commit 9182613

Please sign in to comment.