Skip to content

Commit

Permalink
Merge 4d6e0b9 into 88c4b27
Browse files Browse the repository at this point in the history
  • Loading branch information
artissant committed Dec 15, 2017
2 parents 88c4b27 + 4d6e0b9 commit b9fd4cf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
34 changes: 31 additions & 3 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,11 +377,26 @@ public static function getLocale()
public static function setLocale($locale)
{
// Use RFC 5646 for filenames.
$resource = __DIR__.'/Lang/'.str_replace('_', '-', $locale).'.php';
$files = array_unique([
str_replace('_', '-', $locale),
static::getLanguageFromLocale($locale),
str_replace('_', '-', static::getFallbackLocale()),
static::getLanguageFromLocale(static::getFallbackLocale()),
]);

if (! file_exists($resource)) {
static::setLocale(static::getFallbackLocale());
$found = false;

foreach ($files as $file) {
$resource = __DIR__.'/Lang/'.$file.'.php';

if (file_exists($resource)) {
$found = true;
$locale = $file;
break;
}
}

if (! $found) {
return;
}

Expand Down Expand Up @@ -505,4 +520,17 @@ public static function translateTimeString($time)

return $translated;
}

/**
* Get the language portion of the locale.
*
* @param string $locale
* @return string
*/
public static function getLanguageFromLocale($locale)
{
$parts = explode('_', str_replace('-', '_', $locale));

return $parts[0];
}
}
9 changes: 9 additions & 0 deletions tests/TranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public function testFallback()
$this->assertSame('5 years ago', $date->ago());
}

public function testFallbackWithRegion()
{
Date::setFallbackLocale('en_US');
Date::setLocale('xx');

$date = Date::parse('-5 years');
$this->assertSame('5 years ago', $date->ago());
}

public function testMultiplePluralForms()
{
Date::setLocale('hr');
Expand Down

0 comments on commit b9fd4cf

Please sign in to comment.