Skip to content

Commit

Permalink
Merge pull request #3001 from briannesbitt/feature/rounding-with-inte…
Browse files Browse the repository at this point in the history
…rval

Add locale and translator options to forHumans and diffForHumans
  • Loading branch information
kylekatarnls committed Apr 18, 2024
2 parents 93a0bac + c6ac6df commit 7219739
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 8 deletions.
15 changes: 10 additions & 5 deletions src/Carbon/CarbonInterval.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
use ReflectionException;
use ReturnTypeWillChange;
use RuntimeException;
use Symfony\Contracts\Translation\TranslatorInterface;
use Throwable;

/**
Expand Down Expand Up @@ -1705,7 +1706,9 @@ protected function getForHumansParameters($syntax = null, $short = false, $parts
':optional-space' => $optionalSpace,
];

return [$syntax, $short, $parts, $options, $join, $aUnit, $altNumbers, $interpolations, $minimumUnit, $skip];
$translator ??= isset($locale) ? Translator::get($locale) : null;

return [$syntax, $short, $parts, $options, $join, $aUnit, $altNumbers, $interpolations, $minimumUnit, $skip, $translator];
}

protected static function getRoundingMethodFromOptions(int $options): ?string
Expand Down Expand Up @@ -1822,7 +1825,9 @@ public function getValuesSequence(): array
* ` - if $join is missing, a space will be used as glue
* - 'minimumUnit' entry determines the smallest unit of time to display can be long or
* ` short form of the units, e.g. 'hour' or 'h' (default value: s)
* if int passed, it add modifiers:
* - 'locale' language in which the diff should be output (has no effect if 'translator' key is set)
* - 'translator' a custom translator to use to translator the output.
* if int passed, it adds modifiers:
* Possible values:
* - CarbonInterface::DIFF_ABSOLUTE no modifiers
* - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier
Expand All @@ -1838,7 +1843,8 @@ public function getValuesSequence(): array
*/
public function forHumans($syntax = null, $short = false, $parts = self::NO_LIMIT, $options = null): string
{
[$syntax, $short, $parts, $options, $join, $aUnit, $altNumbers, $interpolations, $minimumUnit, $skip] = $this
/* @var TranslatorInterface|null $translator */
[$syntax, $short, $parts, $options, $join, $aUnit, $altNumbers, $interpolations, $minimumUnit, $skip, $translator] = $this
->getForHumansParameters($syntax, $short, $parts, $options);

$interval = [];
Expand All @@ -1852,8 +1858,7 @@ public function forHumans($syntax = null, $short = false, $parts = self::NO_LIMI
$transId = $relativeToNow ? ($isFuture ? 'from_now' : 'ago') : ($isFuture ? 'after' : 'before');
$declensionMode = null;

/** @var Translator $translator */
$translator = $this->getLocalTranslator();
$translator ??= $this->getLocalTranslator();

$handleDeclensions = function ($unit, $count, $index = 0, $parts = 1) use ($interpolations, $transId, $translator, $altNumbers, $absolute, &$declensionMode) {
if (!$absolute) {
Expand Down
4 changes: 3 additions & 1 deletion src/Carbon/Traits/Difference.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,9 @@ public function secondsUntilEndOfDay(): float
* - 'other' entry (see above)
* - 'minimumUnit' entry determines the smallest unit of time to display can be long or
* ` short form of the units, e.g. 'hour' or 'h' (default value: s)
* if int passed, it add modifiers:
* - 'locale' language in which the diff should be output (has no effect if 'translator' key is set)
* - 'translator' a custom translator to use to translator the output.
* if int passed, it adds modifiers:
* Possible values:
* - CarbonInterface::DIFF_ABSOLUTE no modifiers
* - CarbonInterface::DIFF_RELATIVE_TO_NOW add ago/from now modifier
Expand Down
2 changes: 1 addition & 1 deletion src/Carbon/Traits/IntervalRounding.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function roundWith(DateInterval|string|float|int $precision, callable|
$unit = 'second';

if ($precision instanceof DateInterval) {
$precision = (string) CarbonInterval::instance($precision)->locale('');
$precision = CarbonInterval::instance($precision)->forHumans(['locale' => 'en']);
}

if (\is_string($precision) && preg_match('/^\s*(?<precision>\d+)?\s*(?<unit>\w+)(?<other>\W.*)?$/', $precision, $match)) {
Expand Down
33 changes: 32 additions & 1 deletion tests/Carbon/LocalizationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace Tests\Carbon;

use Carbon\Carbon;
use Carbon\CarbonInterface;
use Carbon\CarbonInterval;
use Carbon\Language;
use Carbon\Translator;
Expand Down Expand Up @@ -462,14 +463,44 @@ public function testAddCustomTranslation()
// en_Boring inherit en because it starts with "en", see symfony-translation behavior
$this->assertSame('3 boring days 4 hours', $diff);

Carbon::setLocale('en');

$diff = Carbon::parse('2018-01-01')
->diffForHumans('2018-01-04 04:00', [
'syntax' => CarbonInterface::DIFF_ABSOLUTE,
'parts' => 2,
'locale' => 'de',
]);

$this->assertSame('3 Tage 4 Stunden', $diff);

$translator->resetMessages();

$this->assertSame([], $translator->getMessages());

Carbon::setLocale('en');
$this->assertSame('en', Carbon::getLocale());
}

public function testLocaleOption()
{
$translator = Translator::get('en_Boring');
$translator->setTranslations([
'day' => ':count boring day|:count boring days',
]);

$diff = Carbon::parse('2018-01-01')
->diffForHumans('2018-01-04 04:00', [
'syntax' => CarbonInterface::DIFF_ABSOLUTE,
'parts' => 2,
'locale' => 'en_Boring',
]);

$translator->setLocale('en');
$translator->resetMessages();

$this->assertSame('3 boring days 4 hours', $diff);
}

public function testCustomWeekStart()
{
Carbon::setLocale('ru');
Expand Down

0 comments on commit 7219739

Please sign in to comment.