Add option to split and translate times with start and end dates

Close #1
This commit is contained in:
2021-01-06 23:05:26 +01:00
parent fcc63c4ea0
commit 54764e741a
2 changed files with 104 additions and 0 deletions

View File

@ -24,6 +24,7 @@ final class NodaTimeAutotranslater {
const TRANSLATABLE_CENTURY = 6;
const TRANSLATABLE_DECADE = 7;
const TRANSLATABLE_TIMESPAN_YEARS = 8;
const TRANSLATABLE_TIMESPAN_DATES = 9;
const LANGS_TO_LOCALES = [
'ar' => 'ar_SY.utf8',
@ -753,6 +754,42 @@ final class NodaTimeAutotranslater {
*/
public static function getTranslations(array $timeInfo):array {
if (!empty($timespanDates = NodaTimeSplitter::attempt_splitting_from_till($timeInfo['zeit_name']))) {
$output = [];
$start = NodaTimeSplitter::attempt_splitting($timespanDates['start_name']);
$startTimeInfo = [
"zeit_name" => $timespanDates['start_name'],
"zeit_beginn" => $start[0],
"zeit_ende" => $start[1],
"zeit_zaehlzeit_jahr" => NodaTimeSplitter::timePartsToCountingYear($start),
"zeit_zaehlzeit_monat" => $start[2],
"zeit_zaehlzeit_tag" => $start[3],
"zeit_zaehlzeit_vorzeichen" => $start[4],
];
$end = NodaTimeSplitter::attempt_splitting($timespanDates['end_name']);
$endTimeInfo = [
"zeit_name" => $timespanDates['end_name'],
"zeit_beginn" => $end[0],
"zeit_ende" => $end[1],
"zeit_zaehlzeit_jahr" => NodaTimeSplitter::timePartsToCountingYear($end),
"zeit_zaehlzeit_monat" => $end[2],
"zeit_zaehlzeit_tag" => $end[3],
"zeit_zaehlzeit_vorzeichen" => $end[4],
];
$output = [];
foreach (self::LANGS_YEARSPAN_FORMAT as $tLang => $format) {
$start_term = self::getTranslations($startTimeInfo)[$tLang];
$end_term = self::getTranslations($endTimeInfo)[$tLang];
$output[$tLang] = \sprintf($format, $start_term, $end_term);
}
return $output;
}
if (!($translation_type = self::check_translatability((string)$timeInfo['zeit_beginn'], (string)$timeInfo['zeit_ende'], (string)$timeInfo['zeit_zaehlzeit_monat']))) {
throw new MDgenericInvalidInputsException("Non-translatable date: {$timeInfo['zeit_beginn']} - {$timeInfo['zeit_ende']}");
}