Locale::setLocale() logs InvalidArgumentException even for valid, installed locales during early request lifecycle

Environment:

Describe the issue

The error log is repeatedly flooded with entries like:

InvalidArgumentException: Invalid/unsupported locale "en", default locale restored in .../lib/pkp/classes/i18n/Locale.php:155

This occurs even for locales that are correctly installed and enabled on the site/journal (confirmed via site.installed_locales / supported_locales, and the corresponding physical locale folders). It appears to fire during early plugin/theme registration (e.g. ThemePlugin->init()), before the request’s journal/site context is fully available to isSupported(), causing a false-positive rejection that is logged before falling back to the primary locale.

This matches the behavior already reported at the link above (originally observed with locale "en" and "fr" triggering the same false positive in our installation), where a community member confirmed the same symptom on 3.5.0-4.

Impact

The resulting log noise makes it very difficult to spot genuine, actionable errors in production error logs, since this notice can fire dozens of times per minute on a multi-journal installation.

Current workaround

As also suggested in the forum thread, we’ve locally commented out the error_log() call in lib/pkp/classes/i18n/Locale.php, in setLocale():

php

public function setLocale($locale): void
{
    if (!$this->isLocaleValid($locale) || !$this->isSupported($locale)) {
        if ($locale) {
            //error_log((string) new InvalidArgumentException("Invalid/unsupported locale \"{$locale}\", default locale restored"));
        }
        $locale = $this->getPrimaryLocale();
    }
    ...
}

This does not fix the underlying validation timing issue, it only silences the noise, and the fallback behavior continues to work correctly.

Suggested direction for a proper fix

Per the original reporter’s note: this notice should probably only be logged if the locale is genuinely not installed anywhere on the site (not per-context), and/or the check should not run before the request context (journal) is fully resolved. Happy to test a patch if one is proposed.

Note: This report was drafted with the assistance of an AI (Claude), based on debugging carried out directly on our production error logs. The described workaround has been applied and tested on our own installation.

I had the same problem as OP, but with more serious impacts than mere log noise.

Below, with aid from Claude AI, is my story:

On my installation, this same early-lifecycle timing issue produced an uncaught fatal error that killed theme plugin registration entirely — the result was a sitewide outage where no CSS/JS ever loaded (visible skip-nav links, unstyled/outline-form layout, oversized images) and admin login was unreliable, until I patched the root cause below.

Environment

  • OJS version: 3.5.0-5
  • PHP versions tested: 8.5.9, 8.5.8, and the latest 8.4 branch (8.4.24)
  • Database: MySQL/MariaDB (mysqli driver)

The crash

PHP Fatal error (caught by Hook::call, logged as a plugin failure):
Plugin APP\plugins\themes\default\DefaultThemePlugin failed to handle the hook PluginRegistry::categoryLoaded::themes
TypeError: Locale::setDefault(): Argument #1 ($locale) must be of type string, null given in lib/pkp/classes/i18n/Locale.php:163
Stack trace:
#0 lib/pkp/classes/i18n/Locale.php(163): Locale::setDefault(NULL)
#1 lib/pkp/classes/i18n/Locale.php(143): PKP\i18n\Locale->setLocale('en')
#2 lib/pkp/classes/i18n/Locale.php(475): PKP\i18n\Locale->getLocale()
#3 lib/pkp/classes/i18n/Locale.php(114): PKP\i18n\Locale->translate('plugins.themes....', NULL, Array, NULL)
#4 lib/pkp/lib/vendor/laravel/framework/.../helpers.php(996): PKP\i18n\Locale->get(...)
#5 lib/pkp/lib/vendor/laravel/framework/.../helpers.php(1028): trans(...)
#6 plugins/themes/default/DefaultThemePlugin.php(45): __('plugins.themes....')
#7 lib/pkp/classes/plugins/ThemePlugin.php(131): DefaultThemePlugin->init()
#8 [internal function]: ThemePlugin->themeRegistered('PluginRegistry:...', Array)
#9 lib/pkp/classes/plugins/Hook.php(164): call_user_func_array(...)
...
#12 lib/pkp/classes/template/PKPTemplateManager.php(1575): PluginRegistry::loadCategory('themes', true)
...

The trace confirms this fires during exactly the code path OP identified — ThemePlugin->init() calling a translation helper (__()) during early plugin/theme registration, before the request’s journal/site context is fully resolved. In my case it’s triggered via StaticPagesPlugin’s generic-plugin registration pulling in PKPTemplateManager::getManager(), which loads the themes category before the journal context exists yet.

Root cause — why this is a hard crash rather than log noise

I confirmed via direct database inspection that both the site-wide primary_locale (site table) and the journal’s own primary_locale and every journal_settings locale key (supportedLocales, supportedFormLocales, supportedSubmissionLocales, etc.) are correctly set to en. So this is not a data/configuration problem — it’s purely a timing/ordering bug in code, compounded by a documented PHP core behavior:

setLocale() (lib/pkp/classes/i18n/Locale.php) ends with:

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

At the point this fires during theme/plugin registration, getWeblateLocaleNames() returns an empty collection (the journal’s locale metadata hasn’t loaded yet — same underlying timing gap OP described for isSupported()), so $locales is an empty array.

PHP’s built-in Locale::lookup() has a documented quirk/bug for exactly this case: when the first argument ($langtag) is an empty array, it ignores the $default parameter entirely and returns null/empty regardless of what’s passed as the fallback. See:

That null is then passed straight into \Locale::setDefault(), which requires a non-null string, producing the uncaught TypeError.

Version variance (worth noting for triage)

I hit this identically on PHP 8.5.9 and on the latest PHP 8.4 branch (8.4.24), but could not reproduce it at all on PHP 8.5.8. This isn’t a case of “PHP 8.5.9 broke it” — the underlying PHP behavior around Locale::lookup()'s handling of an empty $langtag array appears to be inconsistent across different PHP builds/point releases (consistent with the long-standing, still-open bug reports above). So this bug’s visibility will vary by which PHP build a given install happens to be running, not by a simple “avoid version X” rule.

Suggested fix

Simply adding 'en' as the fourth ($default) argument to Locale::lookup() is not sufficient — it doesn’t help when $locales is empty, for the reason above. The fix needs to short-circuit the empty-array case explicitly, rather than relying on Locale::lookup()'s default-handling:

MY TEMPORARY FIX: I edited this file in my ojs installation at Line 163:

ojs/lib/pkp/classes/i18n/Locale.php

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

// After:
\Locale::setDefault($locales ? (\Locale::lookup($locales, $locale, true, 'en') ?: 'en') : 'en');

This bypasses the problematic PHP code path entirely when $locales is empty (using 'en' — or ideally $this->getPrimaryLocale() / a configured site default — directly), and keeps a ?: 'en' safety net for the normal case as well. I’ve tested this fix on both PHP 8.4.24 and 8.5.9 and it fully resolves the crash without any other side effects.

Request to PKP

Given this can produce a full, sitewide theme/asset failure rather than just log noise — and will likely affect more installs as people upgrade PHP — could this guard be incorporated into a future OJS/pkp-lib release? It’s a small, low-risk, one-line change to lib/pkp/classes/i18n/Locale.php::setLocale().

Hi all,

See Locale::lookup() receives more than 100 language tags and causes TypeError on PHP 8.4 · Issue #13139 · pkp/pkp-lib · GitHub for the root cause. A fix should be added there in the near future.

Regards,
Alec Smecher
Public Knowledge Project Team