From 79eed4acc53e755b28e7d4a1418c06be720a28d1 Mon Sep 17 00:00:00 2001
From: Dave Long <dave@longwaveconsulting.com>
Date: Tue, 23 Apr 2024 09:44:54 +0100
Subject: [PATCH] Issue #1561214 by mfb, sun, fietserwin, drzraf, sdrycroft,
 nicxvan: Bootstrap sets C locale, but does not set UTF-8 character encoding

---
 core/lib/Drupal/Core/DrupalKernel.php             |  2 +-
 .../Core/DrupalKernel/DrupalKernelTest.php        | 15 +++++++++++++++
 core/tests/bootstrap.php                          |  2 +-
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/core/lib/Drupal/Core/DrupalKernel.php b/core/lib/Drupal/Core/DrupalKernel.php
index 7422596ba10c..f994cf0f0e86 100644
--- a/core/lib/Drupal/Core/DrupalKernel.php
+++ b/core/lib/Drupal/Core/DrupalKernel.php
@@ -1001,7 +1001,7 @@ public static function bootEnvironment($app_root = NULL) {
 
     // Set sane locale settings, to ensure consistent string, dates, times and
     // numbers handling.
-    setlocale(LC_ALL, 'C');
+    setlocale(LC_ALL, 'C.UTF-8', 'C');
 
     // Set appropriate configuration for multi-byte strings.
     mb_internal_encoding('utf-8');
diff --git a/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php b/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php
index 0ae129a8268e..ec94d2fc52ef 100644
--- a/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php
+++ b/core/tests/Drupal/KernelTests/Core/DrupalKernel/DrupalKernelTest.php
@@ -12,6 +12,8 @@
 use Prophecy\Argument;
 use Symfony\Component\HttpFoundation\Request;
 
+// cspell:ignore äöüßαβγδεζηθικλμνξοσὠ
+
 /**
  * Tests DIC compilation to disk.
  *
@@ -295,4 +297,17 @@ public function testResetContainer() {
     $this->assertSame($request_stack, $container->get('request_stack'));
   }
 
+  /**
+   * Tests system locale.
+   */
+  public function testLocale(): void {
+    $utf8_string = 'äöüßαβγδεζηθικλμνξοσὠ';
+    // Test environment locale should be UTF-8.
+    $this->assertSame($utf8_string, escapeshellcmd($utf8_string));
+    $request = Request::createFromGlobals();
+    $kernel = $this->getTestKernel($request);
+    // Kernel environment locale should be UTF-8.
+    $this->assertSame($utf8_string, escapeshellcmd($utf8_string));
+  }
+
 }
diff --git a/core/tests/bootstrap.php b/core/tests/bootstrap.php
index 50bacbe2b007..05479c1d0e58 100644
--- a/core/tests/bootstrap.php
+++ b/core/tests/bootstrap.php
@@ -147,7 +147,7 @@ class_alias('\Drupal\Tests\DocumentElement', '\Behat\Mink\Element\DocumentElemen
 // Set sane locale settings, to ensure consistent string, dates, times and
 // numbers handling.
 // @see \Drupal\Core\DrupalKernel::bootEnvironment()
-setlocale(LC_ALL, 'C');
+setlocale(LC_ALL, 'C.UTF-8', 'C');
 
 // Set appropriate configuration for multi-byte strings.
 mb_internal_encoding('utf-8');
-- 
GitLab