wppaste
WordPress

PHPMailer::setLanguage( string $langcode = 'en', string $lang_path = '' ): bool

Source
wp-includes/PHPMailer/PHPMailer.php:2494
Set the language for error messages.

Description

The default language is English.

Compatibility

WordPress
core
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$langcodestringoptional
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.Default: 'en'
$lang_pathstringoptional
Path to the language file directory, with trailing separator (slash) Do not set this from user input!Default: ''

Return value

bool
Returns true if the requested language was loaded, false otherwise.

Performance profile

How much work a call to PHPMailer::setLanguage() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Light

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
23–81

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 99.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

Nothing in core calls this; the cost is only what you spend yourself.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost PHPMailer::setLanguage() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always23–67strtolower(), preg_match()
$langcode !== "en" && $foundFile !== false44–72strtolower(), preg_match(), file()
$langcode !== "en" && !$langcodes && ::fileIsAccessible() && $foundFile === false50–76strtolower(), preg_match(), ::fileIsAccessible()
$langcode !== "en" && !$langcodes && ::fileIsAccessible() && $foundFile !== false54–81strtolower(), preg_match(), ::fileIsAccessible(), file()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 99 instructions, 23–81 executed per call, 17 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 1

  • static::fileIsAccessible()

Source code

    public static function setLanguage($langcode = 'en', $lang_path = '')    {        //Backwards compatibility for renamed language codes        $renamed_langcodes = [            'br' => 'pt_br',            'cz' => 'cs',            'dk' => 'da',            'no' => 'nb',            'se' => 'sv',            'rs' => 'sr',            'tg' => 'tl',            'am' => 'hy',        ];         if (array_key_exists($langcode, $renamed_langcodes)) {            $langcode = $renamed_langcodes[$langcode];        }         //Define full set of translatable strings in English        $PHPMAILER_LANG = [            'authenticate' => 'SMTP Error: Could not authenticate.',            'buggy_php' => 'Your version of PHP is affected by a bug that may result in corrupted messages.' .                ' To fix it, switch to sending using SMTP, disable the mail.add_x_header option in' .                ' your php.ini, switch to macOS or Linux, or upgrade your PHP to version 7.0.17+ or 7.1.3+.',            'connect_host' => 'SMTP Error: Could not connect to SMTP host.',            'data_not_accepted' => 'SMTP Error: data not accepted.',            'empty_message' => 'Message body empty',            'encoding' => 'Unknown encoding: ',            'execute' => 'Could not execute: ',            'extension_missing' => 'Extension missing: ',            'file_access' => 'Could not access file: ',            'file_open' => 'File Error: Could not open file: ',            'from_failed' => 'The following From address failed: ',            'instantiate' => 'Could not instantiate mail function.',            'invalid_address' => 'Invalid address: ',            'invalid_header' => 'Invalid header name or value',            'invalid_hostentry' => 'Invalid hostentry: ',            'invalid_host' => 'Invalid host: ',            'mailer_not_supported' => ' mailer is not supported.',            'provide_address' => 'You must provide at least one recipient email address.',            'recipients_failed' => 'SMTP Error: The following recipients failed: ',            'signing' => 'Signing Error: ',            'smtp_code' => 'SMTP code: ',            'smtp_code_ex' => 'Additional SMTP info: ',            'smtp_connect_failed' => 'SMTP connect() failed.',            'smtp_detail' => 'Detail: ',            'smtp_error' => 'SMTP server error: ',            'variable_set' => 'Cannot set or reset variable: ',            'no_smtputf8' => 'Server does not support SMTPUTF8 needed to send to Unicode addresses',            'imap_recommended' => 'Using simplified address parser is not recommended. ' .                'Install the PHP IMAP extension for full RFC822 parsing.',            'deprecated_argument' => 'Deprecated Argument: ',        ];        if (empty($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        $foundlang = true;        $langcode  = strtolower($langcode);        if (            !preg_match('/^(?P<lang>[a-z]{2})(?P<script>_[a-z]{4})?(?P<country>_[a-z]{2})?$/', $langcode, $matches)            && $langcode !== 'en'        ) {            $foundlang = false;            $langcode = 'en';        }         //There is no English translation file        if ('en' !== $langcode) {            $langcodes = [];            if (!empty($matches['script']) && !empty($matches['country'])) {                $langcodes[] = $matches['lang'] . $matches['script'] . $matches['country'];            }            if (!empty($matches['country'])) {                $langcodes[] = $matches['lang'] . $matches['country'];            }            if (!empty($matches['script'])) {                $langcodes[] = $matches['lang'] . $matches['script'];

Changelog

Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/PHPMailer/PHPMailer.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.