Browse Source

Ensure non empty locale when generating cache key.

Fixes #13186.
ADmad 6 years ago
parent
commit
201130ea15
2 changed files with 26 additions and 1 deletions
  1. 5 1
      src/I18n/DateFormatTrait.php
  2. 21 0
      tests/TestCase/I18n/TimeTest.php

+ 5 - 1
src/I18n/DateFormatTrait.php

@@ -184,7 +184,7 @@ trait DateFormatTrait
      *
      * @param \DateTime $date Date.
      * @param string|int|array $format Format.
-     * @param string $locale The locale name in which the date should be displayed.
+     * @param string|null $locale The locale name in which the date should be displayed.
      * @return string
      */
     protected function _formatObject($date, $format, $locale)
@@ -206,6 +206,10 @@ trait DateFormatTrait
             $calendar = IntlDateFormatter::GREGORIAN;
         }
 
+        if ($locale === null) {
+            $locale = I18n::getLocale();
+        }
+
         $timezone = $date->getTimezone()->getName();
         $key = "{$locale}.{$dateFormat}.{$timeFormat}.{$timezone}.{$calendar}.{$pattern}";
 

+ 21 - 0
tests/TestCase/I18n/TimeTest.php

@@ -482,6 +482,27 @@ class TimeTest extends TestCase
     }
 
     /**
+     * testI18nFormatUsingSystemLocale
+     *
+     * @return void
+     */
+    public function testI18nFormatUsingSystemLocale()
+    {
+        // Unset default locale for the Time class to ensure system's locale is used.
+        Time::setDefaultLocale();
+        $locale = I18n::getLocale();
+
+        $time = new Time(1556864870);
+        I18n::setLocale('ar');
+        $this->assertEquals('٢٠١٩-٠٥-٠٣', $time->i18nFormat('yyyy-MM-dd'));
+
+        I18n::setLocale('en');
+        $this->assertEquals('2019-05-03', $time->i18nFormat('yyyy-MM-dd'));
+
+        I18n::setLocale($locale);
+    }
+
+    /**
      * test formatting dates with offset style timezone
      *
      * @dataProvider classNameProvider