Skip to content

Commit

Permalink
add possibility to use translated short notations
Browse files Browse the repository at this point in the history
* use short notation translations if available
* add short notation translations in German and Dutch
* update tests
  • Loading branch information
Boschman committed Oct 30, 2016
1 parent b842ccb commit 8b6a7ce
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 7 deletions.
16 changes: 15 additions & 1 deletion src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,21 @@ public function format($format)

// Short notations.
if (in_array($character, ['D', 'M'])) {
$translated = mb_substr($translated, 0, 3);

$toTranslate = strtolower($original);
$sortTranslated = $lang->trans($toTranslate);

if ($sortTranslated === $toTranslate) {

// use the first 3 characters as short notation
$translated = mb_substr($translated, 0, 3);

} else {

// use translated version
$translated = $sortTranslated;

}
}

// Add to replace list.
Expand Down
21 changes: 21 additions & 0 deletions src/Lang/de.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand Down
21 changes: 21 additions & 0 deletions src/Lang/nl.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -45,4 +58,12 @@
'saturday' => 'zaterdag',
'sunday' => 'zondag',

'mon' => 'ma',
'tue' => 'di',
'wed' => 'wo',
'thu' => 'do',
'fri' => 'vr',
'sat' => 'za',
'sun' => 'zo',

];
25 changes: 20 additions & 5 deletions tests/AutomaticTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
}
}
Expand All @@ -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");
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/TranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 8b6a7ce

Please sign in to comment.