Skip to content

Commit

Permalink
Added custom translations for specific suffixes
Browse files Browse the repository at this point in the history
  • Loading branch information
jenssegers committed Jul 6, 2014
1 parent f64f64e commit f6b2a3c
Show file tree
Hide file tree
Showing 20 changed files with 52 additions and 44 deletions.
37 changes: 17 additions & 20 deletions src/Jenssegers/Date/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,32 +99,29 @@ public function diffForHumans(Carbon $since = null)
$difference = floor($difference / $value);
}

// Translate the unit part
$ago = $lang->choice("date::date.$unit", $difference);

// Translate the ending
// Select the suffix.
if ($relative)
{
if ($future)
{
return $lang->choice('date::date.after', $difference, array('time' => $ago));
}
else
{
return $lang->choice('date::date.before', $difference, array('time' => $ago));
}
$suffix = $future ? 'after' : 'before';
}
else
{
if ($future)
{
return $lang->choice('date::date.from now', $difference, array('time' => $ago));
}
else
{
return $lang->choice('date::date.ago', $difference, array('time' => $ago));
}
$suffix = $future ? 'from_now' : 'ago';
}

// Some languages have different unit translations when used in combination
// with a specific suffix. Here we will check if there is an optional
// translation for that specific suffix and use it if it exists.
if ($lang->get("date::date.${unit}_${suffix}") != "${unit}_${suffix}")
{
$ago = $lang->choice("date::date.${unit}_${suffix}", $difference);
}
else
{
$ago = $lang->choice("date::date.$unit", $difference);
}

return $lang->choice("date::date.$suffix", $difference, array('time' => $ago));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/lang/da/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => 'for :time siden',
'from now' => 'om :time fra nu',
'from_now' => 'om :time fra nu',
'after' => ':time efter',
'before' => ':time f酶r',
'year' => '1 氓r|:count 氓r',
Expand Down
12 changes: 10 additions & 2 deletions src/lang/de/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
|
*/

'ago' => 'vor :time|vor :timen',
'from now' => ':time von jetzt',
'ago' => 'vor :time',
'from_now' => ':time von jetzt',
'after' => ':time nach',
'before' => ':time vor',
'year' => '1 Jahr|:count Jahre',
Expand Down Expand Up @@ -45,4 +45,12 @@
'saturday' => 'Samstag',
'sunday' => 'Sonntag',

'year_ago' => '1 Jahr|:count Jahren',
'month_ago' => '1 Monat|:count Monaten',
'week_ago' => '1 Woche|:count Wochen',
'day_ago' => '1 Tag|:count Tagen',
'hour_ago' => '1 Stunde|:count Stunden',
'minute_ago' => '1 Minute|:count Minuten',
'second_ago' => '1 Sekunde|:count Sekunden',

);
2 changes: 1 addition & 1 deletion src/lang/el/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => '蟺蟻委谓 伪蟺慰 :time',
'from now' => '蟽蔚 :time 伪蟺慰 蟿蠋蟻伪',
'from_now' => '蟽蔚 :time 伪蟺慰 蟿蠋蟻伪',
'after' => ':time 渭蔚蟿维',
'before' => ':time 蟺蟻委谓',
'year' => '1 蠂蟻蠈谓慰蟼|:count 蠂蟻蠈谓喂伪',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/en/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => ':time ago',
'from now' => ':time from now',
'from_now' => ':time from now',
'after' => ':time after',
'before' => ':time before',
'year' => '1 year|:count years',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/eo/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => 'anta怒 :time',
'from now' => 'je :time',
'from_now' => 'je :time',
'after' => ':time poste',
'before' => ':time anta怒e',
'year' => '1 jaro|:count jaroj',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/es/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => 'Hace :time',
'from now' => 'Dentro de :time',
'from_now' => 'Dentro de :time',
'after' => ':time despu茅s',
'before' => ':time antes',
'year' => '1 a帽o|:count a帽os',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/eu/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => 'Orain dela :time',
'from now' => ':time barru',
'from_now' => ':time barru',
'after' => ':time geroago',
'before' => ':time lehenago',
'year' => 'Urte 1|:count urte',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/fi/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => ':time sitten',
'from now' => ':time t盲st盲 hetkest盲',
'from_now' => ':time t盲st盲 hetkest盲',
'after' => '',
'before' => '',
'year' => '1 vuosi|:count vuotta',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/fr/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => 'Il y a :time',
'from now' => 'Dans :time',
'from_now' => 'Dans :time',
'after' => ':time apr猫s',
'before' => ':time avant',
'year' => '1 an|:count ans',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/hr/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => 'Prije :time',
'from now' => ':time od sad',
'from_now' => ':time od sad',
'after' => 'Nakon :time',
'before' => 'Prije :time',
'year' => ':count godinu|:count godine|:count godina',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/hu/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => ':time kor谩bban',
'from now' => ':time k茅s艖bb',
'from_now' => ':time k茅s艖bb',
'after' => ':time k茅s艖bb',
'before' => ':time kor谩bban',
'year' => '1 茅vvel|:count 茅vvel',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/it/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => ':time fa',
'from now' => 'tra :time',
'from_now' => 'tra :time',
'after' => ':time dopo',
'before' => ':time prima',
'year' => '1 anno|:count anni',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/nl/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => ':time geleden',
'from now' => ':time vanaf nu',
'from_now' => ':time vanaf nu',
'after' => ':time na',
'before' => ':time voor',
'year' => '1 jaar|:count jaren',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/pl/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => ':time temu',
'from now' => ':time od teraz',
'from_now' => ':time od teraz',
'after' => ':time przed',
'before' => ':time po',
'year' => '1 rok|:count lata|:count lat',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/pt/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => ':time atr谩s',
'from now' => 'Em :time',
'from_now' => 'Em :time',
'after' => ':time depois',
'before' => ':time antes',
'year' => '1 ano|:count anos',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/sr/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => 'Pre :time',
'from now' => ':time od sada',
'from_now' => ':time od sada',
'after' => 'Nakon :time',
'before' => 'Pre :time',
'year' => ':count godina|:count godine|:count godina',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/sv/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => ':time sedan',
'from now' => ':time fr氓n nu',
'from_now' => ':time fr氓n nu',
'after' => '',
'before' => '',
'year' => '1 氓r|:count 氓r',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/tr/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
*/

'ago' => ':time 枚nce',
'from now' => ':time sonra',
'from_now' => ':time sonra',
'after' => '',
'before' => '',
'year' => ':count y谋l',
Expand Down
13 changes: 8 additions & 5 deletions tests/TranslationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function testMultiplePluralForms()
$this->assertSame("Prije 5 godina", $date->ago());
}

public function testMultipleAgo()
public function testCustomSuffix()
{
Date::setLocale('de');

Expand All @@ -46,6 +46,9 @@ public function testMultipleAgo()

$date = Date::parse('-5 months');
$this->assertSame("vor 5 Monaten", $date->ago());

$date = Date::parse('-5 seconds');
$this->assertSame("vor 5 Sekunden", $date->ago());
}

public function testTranslatesMonths()
Expand Down Expand Up @@ -113,7 +116,7 @@ public function testTranslatesDiffForHumans()
{
$items = array(
'ago',
'from now',
'from_now',
'after',
'before',
'year',
Expand All @@ -139,13 +142,13 @@ public function testTranslatesDiffForHumans()
continue;
}

if (in_array($item, array('ago', 'from now', 'after', 'before')))
if (in_array($item, array('ago', 'from_now', 'after', 'before')))
{
$this->assertContains(':time', $translations[$item]);
$this->assertContains(':time', $translations[$item], "Language: $language");
}
else
{
$this->assertContains(':count', $translations[$item]);
$this->assertContains(':count', $translations[$item], "Language: $language");
}
}
}
Expand Down

0 comments on commit f6b2a3c

Please sign in to comment.