From 329e774779d7fc196d42ba88c522668e2dda2789 Mon Sep 17 00:00:00 2001 From: Jeroen Boschman Date: Sun, 30 Oct 2016 16:38:59 +0100 Subject: [PATCH] add possibility to use translated short notations * use short notation translations if available * add short notation translations in German and Dutch * update tests --- src/Date.php | 11 ++++++++++- src/Lang/de.php | 21 +++++++++++++++++++++ src/Lang/nl.php | 21 +++++++++++++++++++++ tests/AutomaticTest.php | 25 ++++++++++++++++++++----- tests/TranslationTest.php | 2 +- 5 files changed, 73 insertions(+), 7 deletions(-) diff --git a/src/Date.php b/src/Date.php index a0dc3a2..53a610d 100644 --- a/src/Date.php +++ b/src/Date.php @@ -243,7 +243,16 @@ public function format($format) // Short notations. if (in_array($character, ['D', 'M'])) { - $translated = mb_substr($translated, 0, 3); + $toTranslate = strtolower($original); + $shortTranslated = $lang->trans($toTranslate); + + if ($shortTranslated === $toTranslate) { + // use the first 3 characters as short notation + $translated = mb_substr($translated, 0, 3); + } else { + // use translated version + $translated = $shortTranslated; + } } // Add to replace list. diff --git a/src/Lang/de.php b/src/Lang/de.php index f347da9..8cae7ae 100644 --- a/src/Lang/de.php +++ b/src/Lang/de.php @@ -37,6 +37,19 @@ 'november' => 'November', 'december' => 'Dezember', + 'jan' => 'Jan', + 'feb' => 'Feb', + 'mar' => 'Mär', + 'apr' => 'Apr', + 'may' => 'Mai', + 'jun' => 'Jun', + 'jul' => 'Jul', + 'aug' => 'Aug', + 'sep' => 'Sep', + 'oct' => 'Okt', + 'nov' => 'Nov', + 'dec' => 'Dez', + 'monday' => 'Montag', 'tuesday' => 'Dienstag', 'wednesday' => 'Mittwoch', @@ -45,6 +58,14 @@ 'saturday' => 'Samstag', 'sunday' => 'Sonntag', + 'mon' => 'Mo', + 'tue' => 'Di', + 'wed' => 'Mi', + 'thu' => 'Do', + 'fri' => 'Fr', + 'sat' => 'Sa', + 'sun' => 'So', + 'year_diff' => '1 Jahr|:count Jahren', 'month_diff' => '1 Monat|:count Monaten', 'week_diff' => '1 Woche|:count Wochen', diff --git a/src/Lang/nl.php b/src/Lang/nl.php index 76cbcef..c50bed3 100644 --- a/src/Lang/nl.php +++ b/src/Lang/nl.php @@ -37,6 +37,19 @@ 'november' => 'november', 'december' => 'december', + 'jan' => 'jan', + 'feb' => 'feb', + 'mar' => 'mrt', + 'apr' => 'apr', + 'may' => 'mei', + 'jun' => 'jun', + 'jul' => 'jul', + 'aug' => 'aug', + 'sep' => 'sep', + 'oct' => 'okt', + 'nov' => 'nov', + 'dec' => 'dec', + 'monday' => 'maandag', 'tuesday' => 'dinsdag', 'wednesday' => 'woensdag', @@ -45,4 +58,12 @@ 'saturday' => 'zaterdag', 'sunday' => 'zondag', + 'mon' => 'ma', + 'tue' => 'di', + 'wed' => 'wo', + 'thu' => 'do', + 'fri' => 'vr', + 'sat' => 'za', + 'sun' => 'zo', + ]; diff --git a/tests/AutomaticTest.php b/tests/AutomaticTest.php index 7bf497a..dc41017 100644 --- a/tests/AutomaticTest.php +++ b/tests/AutomaticTest.php @@ -37,11 +37,18 @@ public function testTranslatesMonths() $date = new Date("1 $month"); $date->setLocale($language); + // Full $translation = $selector->choose($translations[$month], 0, $language); - $this->assertTrue(isset($translation)); - $this->assertEquals($translation, $date->format('F'), "Language: $language"); // Full - $this->assertEquals(mb_substr($translation, 0, 3), $date->format('M'), "Language: $language"); // Short + $this->assertEquals($translation, $date->format('F'), "Language: $language"); + + // Short + $monthShortEnglish = mb_substr($month, 0, 3); + if (isset($translations[$monthShortEnglish])) { + $this->assertEquals($translations[$monthShortEnglish], $date->format('M'), "Language: $language"); + } else { + $this->assertEquals(mb_substr($translation, 0, 3), $date->format('M'), "Language: $language"); + } } } } @@ -66,9 +73,17 @@ public function testTranslatesDays() $date = new Date($day); $date->setLocale($language); + // Full $this->assertTrue(isset($translations[$day])); - $this->assertEquals($translations[$day], $date->format('l'), "Language: $language"); // Full - $this->assertEquals(mb_substr($translations[$day], 0, 3), $date->format('D'), "Language: $language"); // Short + $this->assertEquals($translations[$day], $date->format('l'), "Language: $language"); + + // Short + $dayShortEnglish = mb_substr($day, 0, 3); + if (isset($translations[$dayShortEnglish])) { + $this->assertEquals($translations[$dayShortEnglish], $date->format('D'), "Language: $language"); + } else { + $this->assertEquals(mb_substr($translations[$day], 0, 3), $date->format('D'), "Language: $language"); + } } } } diff --git a/tests/TranslationTest.php b/tests/TranslationTest.php index 605225c..044b86d 100644 --- a/tests/TranslationTest.php +++ b/tests/TranslationTest.php @@ -159,7 +159,7 @@ public function testFormatTranslated() $this->assertSame('l 28 F 2013 21:58:16', $date->format('\l j \F Y H:i:s')); $date = new Date(1367186296); - $this->assertSame('zon 28 apr 2013 21:58:16', $date->format('D j M Y H:i:s')); + $this->assertSame('zo 28 apr 2013 21:58:16', $date->format('D j M Y H:i:s')); } public function testFormatDeclensions()