From 1f4d692fb5b3c1831552076cf452e8bdf1e6b90a Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Sun, 4 Oct 2020 19:34:17 +0200 Subject: [PATCH] Enable automatic translations of times "before" a given date --- src/NodaTimeAutotranslater.php | 33 +++++++++++++ tests/NodaTimeAutotranslaterTest.php | 69 ++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/src/NodaTimeAutotranslater.php b/src/NodaTimeAutotranslater.php index 7296961..a31fe41 100644 --- a/src/NodaTimeAutotranslater.php +++ b/src/NodaTimeAutotranslater.php @@ -685,6 +685,35 @@ final class NodaTimeAutotranslater { } + /** + * Translated years or timespans below 1000 CE. + * + * @param array $timeInfo Time information. + * + * @return array + */ + public static function translateYearsBeforeEnd(array $timeInfo):array { + + if (empty(trim($timeInfo['zeit_zaehlzeit_monat'], " ,.;0"))) { + $timeInfo['zeit_ende'] = strval(intval($timeInfo['zeit_ende']) + 1); + } + $end = intval($timeInfo['zeit_ende']); + + $innerTimeInfo = $timeInfo; + $innerTimeInfo['zeit_beginn'] = $timeInfo['zeit_ende']; + + $output = []; + foreach (self::LANGS_BEFORE_START_FORMAT_YEAR as $tLang => $format) { + + $dateAlone = self::getTranslations($innerTimeInfo)[$tLang]; + + $timeName = sprintf($format, $dateAlone); + $output[$tLang] = $timeName; + } + return $output; + + } + /** * Translated years or timespans below 1000 CE. * @@ -696,6 +725,10 @@ final class NodaTimeAutotranslater { $end = intval($timeInfo['zeit_ende']); + if (substr($timeInfo['zeit_name'], 0, 4) === "Vor ") { + return self::translateYearsBeforeEnd($timeInfo); + } + $innerTimeInfo = $timeInfo; $innerTimeInfo['zeit_beginn'] = $timeInfo['zeit_ende']; diff --git a/tests/NodaTimeAutotranslaterTest.php b/tests/NodaTimeAutotranslaterTest.php index 6619b58..3569e8d 100644 --- a/tests/NodaTimeAutotranslaterTest.php +++ b/tests/NodaTimeAutotranslaterTest.php @@ -575,4 +575,73 @@ final class NodaTimeAutotranslaterTest extends TestCase { } + /** + * Test to check whether the HTML page is correctly generated. + * + * @author Joshua Ramon Enslin + * @group ValidOutput + * + * @return void + */ + public function testCanTranslateBeforeSingleYear():void { + + $timeInfo = [ + "zeit_name" => "Vor 1919", + "zeit_beginn" => "?", + "zeit_ende" => "1918", + "zeit_zaehlzeit_jahr" => "1919", + "zeit_zaehlzeit_monat" => "00", + "zeit_zaehlzeit_tag" => "00", + ]; + $output = NodaTimeAutotranslater::getTranslations($timeInfo); + self::assertEquals($output["de"], "Vor 1919"); + + } + + /** + * Test to check whether the HTML page is correctly generated. + * + * @author Joshua Ramon Enslin + * @group ValidOutput + * + * @return void + */ + public function testCanTranslateBeforeSingleMonth():void { + + $timeInfo = [ + "zeit_name" => "Vor Mai 1920", + "zeit_beginn" => "?", + "zeit_ende" => "1920", + "zeit_zaehlzeit_jahr" => "1920", + "zeit_zaehlzeit_monat" => "05", + "zeit_zaehlzeit_tag" => "00", + ]; + $output = NodaTimeAutotranslater::getTranslations($timeInfo); + self::assertEquals($output["de"], "Vor Mai 1920"); + + } + + /** + * Test to check whether the HTML page is correctly generated. + * + * @author Joshua Ramon Enslin + * @group ValidOutput + * + * @return void + */ + public function testCanTranslateBeforeSingleDay():void { + + $timeInfo = [ + "zeit_name" => "Vor 01.12.1919", + "zeit_beginn" => "?", + "zeit_ende" => "1919", + "zeit_zaehlzeit_jahr" => "1919", + "zeit_zaehlzeit_monat" => "12", + "zeit_zaehlzeit_tag" => "01", + ]; + $output = NodaTimeAutotranslater::getTranslations($timeInfo); + self::assertEquals($output["de"], "Vor 01.12.1919"); + + } + }