Validate against time errors in autogenerating translations for times

Close #30
This commit is contained in:
2025-04-04 20:03:59 +02:00
parent bb2b1c2c32
commit 8491b62a83
2 changed files with 99 additions and 6 deletions

View File

@ -13,7 +13,7 @@ final class NodaTimeAutotranslater {
// TODO: Move these to NodaTimeAutotranslaterLocales
const LANGS_SYLLABLE_CLEANING = [
public const LANGS_SYLLABLE_CLEANING = [
"hu" => [
"10-as évek" => "10-es évek",
"40-as évek" => "40-es évek",
@ -463,13 +463,13 @@ final class NodaTimeAutotranslater {
}
/**
* Gets translations for a given entry type.
* Prepares translations for each available language.
*
* @param array<integer|string> $timeInfo Time information.
*
* @return array<string>
*/
public static function getTranslations(array $timeInfo):array {
public static function prepareTranslations(array $timeInfo):array {
if (!empty($timeInfo['zeit_name']) and strlen((string)$timeInfo['zeit_name']) > 10 and !empty($timespanDates = NodaTimeSplitter::attempt_splitting_from_till((string)$timeInfo['zeit_name']))) {
@ -604,6 +604,78 @@ final class NodaTimeAutotranslater {
}
/**
* Validates correctness of years in translation strings.
*
* @param string|integer $start Start year.
* @param string|integer $end End year.
* @param array<string, string> $translations Translations.
*
* @return boolean
*/
public static function validateTranslations(string|int $start, string|int $end, array $translations):bool {
$start = ltrim((string)$start, ' 0-');
$end = ltrim((string)$end, ' 0-');
// Edge cases: Centuries and decades have special translations
// and can thus not be validated properly
// Century BCE
if (substr($start, -1) === "0" && substr($end, -1) === '1' && $start > $end) {
return true;
}
// Century CE
if (substr($start, -1) === "1" && substr($end, -1) === '0' && $start < $end) {
return true;
}
// Decade
if (substr($start, -1) === "0" && substr($end, -1) === '9' && $start < $end) {
return true;
}
// 1920 + ? can be both Since 1920 and After 1919, so validation
// is impossible there, too
if ($start === '?' || $end === '?') return true;
// Unset unvalidatable languages
unset($translations['ar'], $translations['fa']);
if ($start !== '?') {
foreach ($translations as $t) {
if (!str_contains($t, $start)) {
return false;
}
}
}
if ($end !== '?' && $start !== $end) {
foreach ($translations as $t) {
if (!str_contains($t, $end)) {
return false;
}
}
}
return true;
}
/**
* Gets translations for a given entry type.
*
* @param array<integer|string> $timeInfo Time information.
*
* @return array<string>
*/
public static function getTranslations(array $timeInfo):array {
$output = self::prepareTranslations($timeInfo);
if (self::validateTranslations($timeInfo['zeit_beginn'], $timeInfo['zeit_ende'], $output) === false) return [];
return $output;
}
/**
* Runs autotranslater.
*