From 9dd97d06be3c2d74bf3e393c7de9db4bdf6727d6 Mon Sep 17 00:00:00 2001 From: Zhaofeng Li Date: Tue, 29 Dec 2015 23:28:17 +0800 Subject: [PATCH] Date.php: Silently fall back to en when the locale isn't supported --- src/Date.php | 8 ++++++++ tests/TranslationTest.php | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/src/Date.php b/src/Date.php index 2f14584..562c8fb 100644 --- a/src/Date.php +++ b/src/Date.php @@ -375,6 +375,14 @@ public static function setLocale($locale) // Use RFC 5646 for filenames. $resourcePath = __DIR__ . '/Lang/' . str_replace('_', '-', $locale) . '.php'; + // Check if the locale is supported + if ( ! is_readable($resourcePath)) + { + // Silently fall back to "en" + static::setLocale("en"); + return; + } + // Symfony locale format. $locale = str_replace('-', '_', $locale); diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 383ae1f..327b24f 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -165,4 +165,12 @@ public function testFormatDeclensions() $date = new Date('10 march 2015'); $this->assertSame('10 мартa 2015', $date->format('j F Y')); } + + public function testFallbackLocale() + { + Date::setLocale('doge'); + + $date = new Date(); + $this->assertSame('en', $date->getLocale()); + } }