Add automatic translation of decade names

This commit is contained in:
Joshua Ramon Enslin 2020-09-24 11:47:54 +02:00 committed by Stefan Rohde-Enslin
parent a4a94a8f8a
commit d7e2c7f4ed

View File

@ -23,6 +23,7 @@ final class NodaTimeAutotranslater {
const TRANSLATABLE_ONLY_YEAR = 5;
const TRANSLATABLE_CENTURY = 6;
const TRANSLATABLE_DECADE = 7;
const TRANSLATABLE_TIMESPAN_YEARS = 8;
const LANGS_TO_LOCALES = [
'ar' => 'ar_SY.utf8',
@ -245,6 +246,52 @@ final class NodaTimeAutotranslater {
'zh' => '%s-%s世紀',
];
const LANGS_DECADE_FORMAT = [
'ar' => '%s-%s',
'de' => '%ser Jahre',
'en' => '%ss',
'es' => '%s-%s',
'fa' => 'دهه %s',
'fr' => 'Années %s',
'hu' => '%s-as évek',
'id' => 'Tahun %s-an',
'it' => '%ss',
'ka' => '%s-იანი წლები',
'ko' => '%s 년대',
'pl' => '%s roku',
'pt' => 'Década de %s',
'ro' => 'Anii %s',
'ru' => '%s-е годы',
'ta' => '%s கள்',
'tl' => '%ss',
'tr' => '%s\'ler',
'ja' => '%s年代',
'zh' => '%s年代',
];
const LANGS_DECADES_FORMAT = [
'ar' => '%s-%s',
'de' => '%s-%ser Jahre',
'en' => '%s-%ss',
'es' => '%s-%s',
'fa' => 'دهه %s-%s',
'fr' => 'Années %s-%s',
'hu' => '%s-%s-as évek',
'id' => 'Tahun %s-an sampai tahun %s-an',
'it' => '%s-%ss',
'ka' => '%s-%s-იანი წლები',
'ko' => '%s-%s 년대',
'pl' => '%s-%s roku',
'pt' => 'Décadas de %s-%s',
'ro' => 'Anii %s-%s',
'ru' => '%s-%s-е годы',
'ta' => '%s-%s கள்',
'tl' => '%s-%ss',
'tr' => '%s-%s\'ler',
'ja' => '%s年代から%s年代',
'zh' => '%s年代-%s年代',
];
/** @var MDMysqli */
private MDMysqli $_mysqli_noda;
/** @var integer */
@ -279,12 +326,19 @@ final class NodaTimeAutotranslater {
return self::TRANSLATABLE_CENTURY;
}
if ((intval($zeit_ende) + 1) % 10 === 0 and intval($zeit_beginn) % 10 === 0) {
return self::TRANSLATABLE_DECADE;
}
if (intval($zeit_ende) < 0 && intval($zeit_beginn) < 0 || intval($zeit_ende) < 1000) {
return self::TRANSLATABLE_AS_YEAR_WITH_SUFFIX;
}
if ($zeit_ende === $zeit_beginn and trim($zeit_zaehlzeit_monat, ", .0") === "") {
return self::TRANSLATABLE_ONLY_YEAR;
}
if ($zeit_ende !== $zeit_beginn and trim($zeit_zaehlzeit_monat, ", .0") === "") {
return self::TRANSLATABLE_TIMESPAN_YEARS;
}
// Conditions speaking against translatability.
if (trim($zeit_zaehlzeit_monat, ", .0") === "") {
return self::TRANSLATABLE_NOT;
@ -388,6 +442,26 @@ final class NodaTimeAutotranslater {
}
/**
* Translated only years: 1994.
*
* @param array<integer|string> $timeInfo Time information.
*
* @return array<string>
*/
public static function translateTimespanYears(array $timeInfo):array {
$start = intval($timeInfo['zeit_beginn']);
$end = intval($timeInfo['zeit_ende']);
$output = [];
foreach (self::LANGS_YEARSPAN_FORMAT as $tLang => $format) {
$output[$tLang] = sprintf($format, (string)abs($start), (string)abs($end));
}
return $output;
}
/**
* Translates century names: 19. Jahrhundert.
*
@ -422,6 +496,40 @@ final class NodaTimeAutotranslater {
}
/**
* Translates decade names: 1920er Jahre.
*
* @param array<integer|string> $timeInfo Time information.
*
* @return array<string>
*/
public static function translateYearsAsDecade(array $timeInfo):array {
// Beginn: 1500. (1501 - 1) / 100 + 1 = 16. 16th century is the time.
$start_cen = (intval($timeInfo['zeit_beginn']));
// End: 1600. 16th century is the time.
$end_cen = (intval($timeInfo['zeit_ende']));
$suffixMode = self::getSuffixModeForYearsWSuffix((intval($timeInfo['zeit_beginn']) - 1), intval($timeInfo['zeit_ende']));
$output = [];
if ($start_cen === $end_cen - 9) {
foreach (self::LANGS_DECADE_FORMAT as $tLang => $format) {
$tLangValue = sprintf($format, (string)$start_cen, (string)$end_cen);
$output[$tLang] = self::applyBcBceFormat($tLang, $tLangValue, $suffixMode);
}
}
else {
foreach (self::LANGS_DECADES_FORMAT as $tLang => $format) {
$tLangValue = sprintf($format, (string)$start_cen, (string)($end_cen - 9));
$output[$tLang] = self::applyBcBceFormat($tLang, $tLangValue, $suffixMode);
}
}
return $output;
}
/**
* Translated years or timespans below 1000 CE.
*
@ -504,6 +612,14 @@ final class NodaTimeAutotranslater {
return self::translateYearsAsCentury($timeInfo);
}
if ($translation_type === self::TRANSLATABLE_DECADE) {
return self::translateYearsAsDecade($timeInfo);
}
if ($translation_type === self::TRANSLATABLE_TIMESPAN_YEARS) {
return self::translateTimespanYears($timeInfo);
}
if ($translation_type === self::TRANSLATABLE_AS_YEAR_WITH_SUFFIX) {
return self::translateYearsWithSuffix($timeInfo);
}