Skip to content

Commit

Permalink
Merge pull request #205 from limenet/master
Browse files Browse the repository at this point in the history
Added functionality to translate a pluralized string
  • Loading branch information
jenssegers committed Jun 5, 2016
2 parents 7870e43 + 0fa3305 commit 5a3e618
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
32 changes: 31 additions & 1 deletion src/Date.php
Expand Up @@ -437,7 +437,37 @@ public static function translateTimeString($time)

$lines = array_intersect_key($all['messages'], array_flip((array) $keys));

// Some languages (e.g. Russian) have different translations
// for month and day names.
// This requires us to split the strings first.

$aa = array_map(function ($line) {
if (strpos($line, '|') === false) {
// There is only option to translate this string.
return [$line];
} else {
// There are multiple options, delimited by a pipe.
$options = explode('|', $line);

return array_map(function ($option) {
// First remove ':count'.
$option = trim(str_replace(':count', null, $option));

// Secondly remove the number parameter.
$option = preg_replace('/({\d+(,(\d+|Inf))?}|\[\d+(,(\d+|Inf))?\])/', null, $option);

return $option;
}, $options);
}
}, $lines);

$translated = str_ireplace($lines, array_keys($lines), $time);

// Replace the translated words with the English words
return str_ireplace($lines, array_keys($lines), $time);
foreach ($aa as $key => $values) {
$translated = str_ireplace($values, $key, $translated);
}

return $translated;
}
}
11 changes: 11 additions & 0 deletions tests/TranslationTest.php
Expand Up @@ -172,4 +172,15 @@ public function testFormatDeclensions()
$date = new Date('10 march 2015');
$this->assertSame('10 мартa 2015', $date->format('j F Y'));
}

public function testTranslateTimeString()
{
Date::setLocale('ru');
$date = Date::translateTimeString('понедельник 21 март 2015');
$this->assertSame('monday 21 march 2015', $date);

Date::setLocale('de');
$date = Date::translateTimeString('Montag 21 März 2015');
$this->assertSame('monday 21 march 2015', $date);
}
}

0 comments on commit 5a3e618

Please sign in to comment.