Upgrade OJS 3.3.0.20 → 3.5.0-5 fails on PHP 8.4: Locale::setDefault(): Argument #1 ($locale) must be of type string, null given

Hi all,

we are upgrading an OJS 3.3.0 installation directly to OJS 3.5.0-5 on PHP 8.4.27 (ICU 76.1). php tools/upgrade.php upgrade aborts during the 3.4.0 migrations, in I5716_EmailTemplateAssignments:

PHP Fatal error: Uncaught TypeError: Locale::setDefault(): Argument #1 ($locale) must be of type string, null given in lib/pkp/classes/i18n/Locale.php:163
#0 lib/pkp/classes/i18n/Locale.php(163): Locale::setDefault()
#1 …/Illuminate/Support/Facades/Facade.php(363): PKP\i18n\Locale->setLocale()
#2 lib/pkp/classes/migration/upgrade/v3_4_0/I5716_EmailTemplateAssignments.php(235): …
#3 lib/pkp/classes/migration/upgrade/v3_4_0/I5716_EmailTemplateAssignments.php(55): …->addDefaultTemplateNames()
#4 lib/pkp/classes/install/Installer.php(445): …->up()
…
#9 tools/upgrade.php(22): PKP\cliTool\UpgradeTool->execute()

Root cause

PKP\i18n\Locale::setLocale() does:

$locales = array_keys($this->getWeblateLocaleNames());
\Locale::setDefault(\Locale::lookup($locales, $locale, true));

lib/weblateLanguages/languages.json contains the tag und (“Unknown language”). With $canonicalize = true, Locale::lookup() canonicalizes every candidate tag. ICU canonicalizes und to an empty string, so the whole lookup fails with U_ILLEGAL_ARGUMENT_ERROR (“unable to canonicalize lang_tag”). Because und is always in the candidate list, this happens for every requested locale, including en and de.

What lookup() returns on that failure depends on the PHP version (all tested with ICU 76.1):

  • PHP 8.2 / 8.3: returns “”. setDefault(‘’) succeeds silently, so the lookup never actually works but nothing crashes.
  • PHP 8.4 / 8.5: returns null, which causes the TypeError above.

Minimal reproduction:

php -r ‘var_dump(Locale::lookup([“und”, “de”], “de”, true));’

PHP 8.3: string(0) “” PHP 8.4: NULL

Without und in the list, or with $canonicalize = false, the lookup returns de as expected on PHP 8.4.

Workaround

In lib/pkp/classes/i18n/Locale.php, line 163:

\Locale::setDefault(\Locale::lookup($locales, $locale, true) ?: $locale);

A proper fix would probably exclude und (or any tag that canonicalizes to an empty string) from the candidate list. Note that this is not limited to the upgrade: every setLocale() call on PHP 8.4 is affected.

Is this already known or tracked in pkp-lib? I’m happy to open a GitHub issue if not.

Thanks!

Hi @s.tahn,

I believe this is #13139, already fixed in OJS/OMP/OPS 3.5.0-6 (just being released now).

Regards,
Alec Smecher
Public Knowledge Project Team

Hi Alec,

thanks a lot for the quick reply and for pointing me to the bug report. I hadn’t come across it before posting, sorry about that.

I’ve now tested the upgrade with the new version, and it completed successfully on PHP 8.4 without the Locale::setDefault() error. So the issue is resolved for me.

Thanks again for your help!

Best regards,
Stephan