From 184c1a88fec4b00e78ed236af3acd3e49a94b239 Mon Sep 17 00:00:00 2001 From: Jim Safley Date: Tue, 16 Jul 2024 13:05:15 -0400 Subject: [PATCH] Fix default locale when using debug (fix #2212) --- application/src/Mvc/MvcListeners.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/application/src/Mvc/MvcListeners.php b/application/src/Mvc/MvcListeners.php index 5c95eddd8..4d6cc00e0 100644 --- a/application/src/Mvc/MvcListeners.php +++ b/application/src/Mvc/MvcListeners.php @@ -135,15 +135,31 @@ public function bootstrapLocale(MvcEvent $event) // The translator already loaded the configured locale. $locale = $translator->getDelegatedTranslator()->getLocale(); } - if (extension_loaded('intl')) { - \Locale::setDefault($locale); - } + $this->setDefaultLocale($locale); $translator->getDelegatedTranslator()->setLocale($locale); // Enable automatic translation for validation error messages. AbstractValidator::setDefaultTranslator($translator); } + /** + * Set the default locale. + * + * Note that we only set a defualt locale when the intl extension is loaded + * and when the locale isn't "debug". The "debug" locale is a special case + * used by Omeka to detect translated strings on the page wihout switching + * to another language. Otherwise there will be a "Found unconstructed + * IntlDateFormatter" error. + * + * @param string $locale + */ + public function setDefaultLocale($locale) + { + if (extension_loaded('intl') && 'debug' !== $locale) { + \Locale::setDefault($locale); + } + } + /** * Redirect all requests to install route if Omeka is not installed. * @@ -354,9 +370,7 @@ public function preparePublicSite(MvcEvent $event) // locale. $locale = $services->get('Omeka\Settings\Site')->get('locale'); if ($locale) { - if (extension_loaded('intl')) { - \Locale::setDefault($locale); - } + $this->setDefaultLocale($locale); $services->get('MvcTranslator')->getDelegatedTranslator()->setLocale($locale); } }