Skip to content

Commit

Permalink
Translate ordinal words when parsing string
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Oct 30, 2022
1 parent 64706e6 commit 7e2c7bc
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Carbon/Lang/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,13 @@
'first_day_of_week' => 1,
'day_of_first_week_of_year' => 4,
'list' => [', ', ' und '],
'ordinal_words' => [
'of' => 'im',
'first' => 'erster',
'second' => 'zweiter',
'third' => 'dritter',
'fourth' => 'vierten',
'fifth' => 'fünfter',
'last' => 'letzten',
],
];
9 changes: 9 additions & 0 deletions src/Carbon/Lang/fr.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,13 @@
'first_day_of_week' => 1,
'day_of_first_week_of_year' => 4,
'list' => [', ', ' et '],
'ordinal_words' => [
'of' => 'de',
'first' => 'premier',
'second' => 'deuxième',
'third' => 'troisième',
'fourth' => 'quatrième',
'fifth' => 'cinquième',
'last' => 'dernier',
],
];
9 changes: 9 additions & 0 deletions src/Carbon/Lang/it.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,13 @@
'first_day_of_week' => 1,
'day_of_first_week_of_year' => 4,
'list' => [', ', ' e '],
'ordinal_words' => [
'of' => 'di',
'first' => 'primo',
'second' => 'secondo',
'third' => 'terzo',
'fourth' => 'quarto',
'fifth' => 'quinto',
'last' => 'ultimo',
],
];
9 changes: 9 additions & 0 deletions src/Carbon/Lang/pt.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,13 @@
'first_day_of_week' => 1,
'day_of_first_week_of_year' => 4,
'list' => [', ', ' e '],
'ordinal_words' => [
'of' => 'de',
'first' => 'primeira',
'second' => 'segunda',
'third' => 'terceira',
'fourth' => 'quarta',
'fifth' => 'quinta',
'last' => 'última',
],
];
14 changes: 14 additions & 0 deletions src/Carbon/Traits/Localization.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,13 @@ public static function translateTimeString($timeString, $from = null, $to = null
$weekdays = $messages['weekdays'] ?? [];
$meridiem = $messages['meridiem'] ?? ['AM', 'PM'];

if (isset($messages['ordinal_words'])) {
$timeString = static::replaceOrdinalWords(
$timeString,
$key === 'from' ? array_flip($messages['ordinal_words']) : $messages['ordinal_words']
);
}

if ($key === 'from') {
foreach (['months', 'weekdays'] as $variable) {
$list = $messages[$variable.'_standalone'] ?? null;
Expand Down Expand Up @@ -821,4 +828,11 @@ private static function getTranslationArray($translation, $length, $timeString):

return $list;
}

private static function replaceOrdinalWords(string $timeString, array $ordinalWords): string
{
return preg_replace_callback('/(?<![a-z])[a-z]+(?![a-z])/i', function (array $match) use ($ordinalWords) {
return $ordinalWords[mb_strtolower($match[0])] ?? $match[0];
}, $timeString);
}
}
9 changes: 9 additions & 0 deletions tests/Jenssegers/DateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,4 +189,13 @@ public function testTranslateTimeString()

$this->assertSame('Foobar', Carbon::translateTimeString('Foobar', 'xx'));
}

public function testTranslateTimeStringWithOrdinalWords()
{
$date = Carbon::translateTimeString('Premier lundi de mai', 'fr', 'en');
$this->assertSame('first monday of may', mb_strtolower($date));

$date = Carbon::translateTimeString('Premier lundi de mai', 'fr', 'es');
$this->assertSame('primer lunes de mayo', mb_strtolower($date));
}
}

0 comments on commit 7e2c7bc

Please sign in to comment.