Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spanish translation and test #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions spec/Smirik/PHPDateTimeAgo/DateTimeAgoSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,4 +231,43 @@ function it_checks_polish()
$this->get(new \DateTime('-95 years'))->shouldBe('95 lat temu');
}

function it_checks_spanish()
{
$translator = new \Smirik\PHPDateTimeAgo\TextTranslator\SpanishTextTranslator();
$translator->enableWeeksMonthsYears(TRUE);
$this->setTextTranslator($translator);

$this->get(new \DateTime())->shouldBe('ahora');
$this->get(new \DateTime('-5 seconds'))->shouldReturn('ahora');
$this->get(new \DateTime('-25 seconds'))->shouldReturn('ahora');
$this->get(new \DateTime('-59 seconds'))->shouldReturn('ahora');

$this->get(new \DateTime('-1 minutes'))->shouldBe('hace 1 minuto');
$this->get(new \DateTime('-3 minutes'))->shouldBe('hace 3 minutos');
$this->get(new \DateTime('-25 minutes'))->shouldBe('hace 25 minutos');
$this->get(new \DateTime('-59 minutes'))->shouldBe('hace 59 minutos');
$this->get(new \DateTime('-61 minutes'))->shouldBe('hace 1 hora');

$this->get(new \DateTime('-121 minutes'))->shouldBe('hace 2 horas');

$this->get(new \DateTime('-24 hours'))->shouldBe('hace 1 día');
$this->get(new \DateTime('-2 days'))->shouldBe('hace 2 días');
$this->get(new \DateTime('-5 days'))->shouldBe('hace 5 días');
$this->get(new \DateTime('-6 days'))->shouldBe('hace 6 días');

$this->get(new \DateTime('-7 days'))->shouldBe('hace 1 semana');
$this->get(new \DateTime('-18 days'))->shouldBe('hace 2 semanas');
$this->get(new \DateTime('-29 days'))->shouldBe('hace 4 semanas');

$this->get(new \DateTime('-30 days'))->shouldBe('hace 1 mes');
$this->get(new \DateTime('2015-09-08'), new \DateTime('2015-08-06'))->shouldBe('hace 1 mes');
$this->get(new \DateTime('-80 days'))->shouldBe('hace 2 meses');
$this->get(new \DateTime('-364 days'))->shouldBe('hace 11 meses');

$this->get(new \DateTime('-365 days'))->shouldBe('hace 1 año');
$this->get(new \DateTime('-729 days'))->shouldBe('hace 1 año');
$this->get(new \DateTime('-730 days'))->shouldBe('hace 2 años');
$this->get(new \DateTime('-95 years'))->shouldBe('hace 95 años');
}

}
133 changes: 133 additions & 0 deletions src/Smirik/PHPDateTimeAgo/TextTranslator/SpanishTextTranslator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
<?php

namespace Smirik\PHPDateTimeAgo\TextTranslator;

class SpanishTextTranslator extends AbstractTextTranslator {

protected $pre_word = 'hace';
protected $minute_words = array('minuto', 'minutos');
protected $hour_words = array('hora', 'horas');
protected $day_words = array('día', 'días');
protected $week_words = array('semana', 'semanas');
protected $month_words = array('mes', 'meses');
protected $year_words = array('año', 'años');

protected $weeks_months_years = FALSE;



/**
* {@inheritdoc}
*/
function now()
{
return "ahora";
}

/**
* Gets used by minutes(), hours(), days(), weeks(), months() and years(), does the actual
* formatting work.
* @param $number
* @param $typeWords
* @return string
*/
protected function format($number, $typeWords)
{
$typeWord = $typeWords[$this->pluralization($number)];

return $this->pre_word . ' ' . $number . ' ' . $typeWord;
}

/**
* {@inheritdoc}
*/
public function minutes($minutes)
{
return $this->format($minutes, $this->minute_words);
}

/**
* {@inheritdoc}
*/
public function hours($hours)
{
return $this->format($hours, $this->hour_words);
}

/**
* {@inheritdoc}
*/
public function days($days)
{
return $this->format($days, $this->day_words);
}

/**
* {@inheritdoc}
*/
public function weeks($days)
{
return $this->format($days, $this->week_words);
}

/**
* {@inheritdoc}
*/
public function months($days)
{
return $this->format($days, $this->month_words);
}

/**
* {@inheritdoc}
*/
public function years($days)
{
return $this->format($days, $this->year_words);
}

/**
* Pluralize the number. Returns key in related array (minute_words, hour_words, day_words)
* @param integer $number
* @return integer
*/
protected function pluralization($number)
{
return ($number == 1) ? 0 : 1;
}

/**
* {@inheritdoc}
*/
public function supportsWeeks()
{
return $this->weeks_months_years;
}

/**
* {@inheritdoc}
*/
public function supportsMonths()
{
return $this->weeks_months_years;
}

/**
* {@inheritdoc}
*/
public function supportsYears()
{
return $this->weeks_months_years;
}

/**
* Toggle whether to render weeks/months/years or fall back to date format for longer periods.
*
* @param bool $enable
*/
public function enableWeeksMonthsYears($enable)
{
$this->weeks_months_years = $enable;
}

}