From c9a1a74bcea7d8b2f3abec48cfc4b7bc9e4fa185 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Sun, 4 Oct 2020 18:21:33 +0200 Subject: [PATCH] Enable autotranslating of times 'after' a certain date --- src/NodaTimeAutotranslater.php | 39 ++++++++++++++ src/NodaTimeSplitter.php | 4 +- tests/NodaTimeAutotranslaterTest.php | 78 ++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 2 deletions(-) diff --git a/src/NodaTimeAutotranslater.php b/src/NodaTimeAutotranslater.php index 921f6dd..344e1c0 100644 --- a/src/NodaTimeAutotranslater.php +++ b/src/NodaTimeAutotranslater.php @@ -625,6 +625,41 @@ final class NodaTimeAutotranslater { } + /** + * Translated years or timespans below 1000 CE. + * + * @param array $timeInfo Time information. + * + * @return array + */ + public static function translateYearsAfterStart(array $timeInfo):array { + + if (empty(trim($timeInfo['zeit_zaehlzeit_monat'], " ,.;0"))) { + $timeInfo['zeit_beginn'] = strval(intval($timeInfo['zeit_beginn']) - 1); + } + else if (empty(trim($timeInfo['zeit_zaehlzeit_tag'], " ,.;0"))) { + $timeInfo['zeit_zaehlzeit_monat'] = strval(intval($timeInfo['zeit_zaehlzeit_monat']) - 1); + } + else { + $timeInfo['zeit_zaehlzeit_tag'] = strval(intval($timeInfo['zeit_zaehlzeit_tag']) - 1); + } + # else throw new Exception("Unhandled split case. Counting time is: " . var_export($timeInfo, true)); + $innerTimeInfo = $timeInfo; + $innerTimeInfo['zeit_ende'] = $timeInfo['zeit_beginn']; + $output = []; + + foreach (self::LANGS_AFTER_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. * @@ -636,6 +671,10 @@ final class NodaTimeAutotranslater { $start = intval($timeInfo['zeit_beginn']); + if (substr($timeInfo['zeit_name'], 0, 5) === "Nach ") { + return self::translateYearsAfterStart($timeInfo); + } + $innerTimeInfo = $timeInfo; $innerTimeInfo['zeit_ende'] = $timeInfo['zeit_beginn']; $output = []; diff --git a/src/NodaTimeSplitter.php b/src/NodaTimeSplitter.php index f3b0ef8..778deda 100644 --- a/src/NodaTimeSplitter.php +++ b/src/NodaTimeSplitter.php @@ -578,10 +578,10 @@ final class NodaTimeSplitter { $month = "0" . substr($datum, 0, 1); return [$start, $start, $month, "00", "+", ""]; } - if (preg_match("/^[0-9]{4}\.[0-9]{2}$/", $datum)) { // Hungarian Y-m + if (preg_match("/^[0-9]{4}\.[0-9]{2}(\.|)$/", $datum)) { // Hungarian Y-m $start = substr($datum, 0, 4); $month = substr($datum, 5, 2); - return [$start, $start, $month, "00", "+", ""]; + if (intval($month) < 13) return [$start, $start, $month, "00", "+", ""]; } if (preg_match("/^[0-9]{4}\-[0-9]{2}$/", $datum)) { // Time spans: 1945-46 $start = substr($datum, 0, 4); diff --git a/tests/NodaTimeAutotranslaterTest.php b/tests/NodaTimeAutotranslaterTest.php index afc4720..03ed871 100644 --- a/tests/NodaTimeAutotranslaterTest.php +++ b/tests/NodaTimeAutotranslaterTest.php @@ -310,6 +310,7 @@ final class NodaTimeAutotranslaterTest extends TestCase { public function testCanTranslateCenturiesBeforeCommonEra():void { $timeInfo = [ + "zeit_name" => "20.-19. Jahrhundert v. Chr.", "zeit_beginn" => "-2000", "zeit_ende" => "-1801", "zeit_zaehlzeit_jahr" => "1900", @@ -332,6 +333,7 @@ final class NodaTimeAutotranslaterTest extends TestCase { public function testCanTranslateYYYYUntilYYYY():void { $timeInfo = [ + "zeit_name" => "1000-1150", "zeit_beginn" => "1000", "zeit_ende" => "1150", "zeit_zaehlzeit_jahr" => "1075", @@ -354,6 +356,7 @@ final class NodaTimeAutotranslaterTest extends TestCase { public function testCanTranslateSingleYear():void { $timeInfo = [ + "zeit_name" => "1000", "zeit_beginn" => "1000", "zeit_ende" => "1000", "zeit_zaehlzeit_jahr" => "1000", @@ -376,6 +379,7 @@ final class NodaTimeAutotranslaterTest extends TestCase { public function testCanTranslateSingleThreeDigitYearBeforeCommonEra():void { $timeInfo = [ + "zeit_name" => "100 v. Chr.", "zeit_beginn" => "-100", "zeit_ende" => "-100", "zeit_zaehlzeit_jahr" => "100", @@ -398,6 +402,7 @@ final class NodaTimeAutotranslaterTest extends TestCase { public function testCanTranslateSingleYearBeforeCommonEra():void { $timeInfo = [ + "zeit_name" => "1000 v. Chr.", "zeit_beginn" => "-1000", "zeit_ende" => "-1000", "zeit_zaehlzeit_jahr" => "1000", @@ -420,6 +425,7 @@ final class NodaTimeAutotranslaterTest extends TestCase { public function testCanTranslateYYYYUntilYYYYBefore1000():void { $timeInfo = [ + "zeit_name" => "800-850 n. Chr.", "zeit_beginn" => "800", "zeit_ende" => "850", "zeit_zaehlzeit_jahr" => "0825", @@ -442,6 +448,7 @@ final class NodaTimeAutotranslaterTest extends TestCase { public function testCanTranslateYYYYUntilYYYYBeforeCommonEra():void { $timeInfo = [ + "zeit_name" => "1156-1102 v. Chr.", "zeit_beginn" => "-1156", "zeit_ende" => "-1102", "zeit_zaehlzeit_jahr" => "1075", @@ -464,6 +471,7 @@ final class NodaTimeAutotranslaterTest extends TestCase { public function testCanTranslateSinceSingleDay():void { $timeInfo = [ + "zeit_name" => "Seit 01.05.1920", "zeit_beginn" => "1920", "zeit_ende" => "?", "zeit_zaehlzeit_jahr" => "1920", @@ -486,6 +494,7 @@ final class NodaTimeAutotranslaterTest extends TestCase { public function testCanTranslateSinceSingleMonth():void { $timeInfo = [ + "zeit_name" => "Seit Mai 1920", "zeit_beginn" => "1920", "zeit_ende" => "?", "zeit_zaehlzeit_jahr" => "1920", @@ -497,4 +506,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 testCanTranslateAfterSingleYear():void { + + $timeInfo = [ + "zeit_name" => "Nach 1919", + "zeit_beginn" => "1920", + "zeit_ende" => "?", + "zeit_zaehlzeit_jahr" => "1919", + "zeit_zaehlzeit_monat" => "00", + "zeit_zaehlzeit_tag" => "00", + ]; + $output = NodaTimeAutotranslater::getTranslations($timeInfo); + self::assertEquals($output["de"], "Nach 1919"); + + } + + /** + * Test to check whether the HTML page is correctly generated. + * + * @author Joshua Ramon Enslin + * @group ValidOutput + * + * @return void + */ + public function testCanTranslateAfterSingleMonth():void { + + $timeInfo = [ + "zeit_name" => "Nach April 1920", + "zeit_beginn" => "1920", + "zeit_ende" => "?", + "zeit_zaehlzeit_jahr" => "1920", + "zeit_zaehlzeit_monat" => "05", + "zeit_zaehlzeit_tag" => "00", + ]; + $output = NodaTimeAutotranslater::getTranslations($timeInfo); + self::assertEquals($output["de"], "Nach April 1920"); + + } + + /** + * Test to check whether the HTML page is correctly generated. + * + * @author Joshua Ramon Enslin + * @group ValidOutput + * + * @return void + */ + public function testCanTranslateAfterSingleDay():void { + + $timeInfo = [ + "zeit_name" => "Nach 01.12.1919", + "zeit_beginn" => "1919", + "zeit_ende" => "?", + "zeit_zaehlzeit_jahr" => "1919", + "zeit_zaehlzeit_monat" => "12", + "zeit_zaehlzeit_tag" => "02", + ]; + $output = NodaTimeAutotranslater::getTranslations($timeInfo); + self::assertEquals($output["de"], "Nach 01.12.1919"); + + } + }