Transfer remaining class constants in autotranslater to enum

This commit is contained in:
Joshua Ramon Enslin 2022-09-10 13:23:35 +02:00
parent ea832e2160
commit 59c858fb0f
Signed by: jrenslin
GPG Key ID: 46016F84501B70AE
2 changed files with 329 additions and 234 deletions

View File

@ -14,198 +14,6 @@ final class NodaTimeAutotranslater {
// TODO: Move these to NodaTimeAutotranslaterLocales
const LANGS_SINCE_START_FORMAT_YEAR = [
'ar' => '%s-',
'de' => 'Seit %s',
'en' => 'Since %s',
'es' => 'Desde %s',
'fa' => '%s-',
'fr' => 'Depuis %s',
'hu' => '%s-től',
'id' => 'Sejak %s',
'it' => 'Dal %s',
'ka' => '%s წლიდან',
'ko' => '%s부터',
'pl' => 'Od %s r.',
'pt' => '%s dC',
'ro' => 'Din %s',
'ru' => 'С %s г.',
'ta' => '%s முதல்',
'tl' => 'Mula noong %s',
'tr' => '%s-',
// Languages that don't really need a specific locale
'ja' => '%s以来',
'zh' => '自%s以來',
];
const LANGS_AFTER_START_FORMAT_YEAR = [
'ar' => '%s-',
'de' => 'Nach %s',
'en' => 'After %s',
'es' => 'Despues de %s',
'fa' => '%s-',
'fr' => 'Après %s',
'hu' => '%s után',
'id' => 'Setelah %s',
'it' => 'Dopo il %s',
'ka' => '%s წლის შემდეგ',
'ko' => '%s 년 이후',
'pl' => 'Po %s r.',
'pt' => 'Depois de %s',
'ro' => 'După %s',
'ru' => 'после %s г.',
'ta' => '%s க்குப் பிறகு',
'tl' => 'Pagkaraan ng %s',
'tr' => '%s-',
// Languages that don't really need a specific locale
'ja' => '%s以降',
'zh' => '%s以后',
];
const LANGS_UNTIL_START_FORMAT_YEAR = [
'ar' => '-%s',
'de' => 'Bis %s',
'en' => 'Until %s',
'es' => 'Hasta %s',
'fa' => '-%s',
'fr' => 'Jusqu\'en %s',
'hu' => '%s-ig',
'id' => 'Sampai %s',
'it' => 'Fino al %s',
'ka' => '%s წლამდე',
'ko' => '%s까지',
'pl' => 'do %s roku',
'pt' => 'até %s',
'ro' => 'până în %s',
'ru' => 'до %s г.',
'ta' => '%s வரை',
'tl' => 'Hanggang %s',
'tr' => '%s-',
// Languages that don't really need a specific locale
'ja' => '%sまで',
'zh' => '直到%s',
];
const LANGS_BEFORE_START_FORMAT_YEAR = [
'ar' => '-%s',
'de' => 'Vor %s',
'en' => 'Before %s',
'es' => 'Antes de %s',
'fa' => '-%s',
'fr' => 'Avant %s',
'hu' => '%s előtt',
'id' => 'Sebelum %s',
'it' => 'Prima del %s',
'ka' => '%s წლამდე',
'ko' => '%s 년 이전',
'pl' => 'Przed %s',
'pt' => 'Antes de %s',
'ro' => 'înainte de %s',
'ru' => 'до %s г.',
'ta' => '%s க்கு முன்',
'tl' => 'Bago ang %s',
'tr' => '%s-',
// Languages that don't really need a specific locale
'ja' => '%s以前',
'zh' => '%s之前',
];
const LANGS_CENTURY_FORMAT = [
'ar' => 'القرن ال %s',
'de' => '%s. Jahrhundert',
'en' => '%s. century',
'es' => 'Siglo %s',
'fa' => 'قرن %s',
'fr' => '%sème siècle',
'hu' => '%s. század',
'id' => 'Abad ke-%s',
'it' => '%sesimo secolo',
'ka' => 'მე -%s საუკუნე',
'ko' => '%s 세기',
'pl' => '%s wiek',
'pt' => 'Século %s',
'ro' => 'Secolul al %s-lea',
'ru' => '%s век',
'ta' => '%s ஆம் நூற்றாண்டு',
'tl' => 'Ika-%s na siglo',
'tr' => '%s. yüzyıl',
'ja' => '%s世紀',
'zh' => '%s世紀',
];
const LANGS_CENTURIES_FORMAT = [
'ar' => 'القرن ال %s-%s',
'de' => '%s.-%s. Jahrhundert',
'en' => '%s.-%s. century',
'es' => 'Siglo %s-%s',
'fa' => 'قرن %s-%s',
'fr' => '%s-%sème siècle',
'hu' => '%s.-%s. század',
'id' => 'Abad ke-%s-%s',
'it' => '%s-%sesimo secolo',
'ka' => 'მე -%s-%s საუკუნე',
'ko' => '%s-%s 세기',
'pl' => '%s-%s wiek',
'pt' => 'Século %s-%s',
'ro' => 'Secolul al %s-%s-lea',
'ru' => '%s-%s век',
'ta' => '%s-%s ஆம் நூற்றாண்டு',
'tl' => 'Ika-%s hanggang ika-%s na siglo',
'tr' => '%s.-%s. yüzyıl',
'ja' => '%s世紀-%s世紀',
'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年代',
];
const LANGS_SYLLABLE_CLEANING = [
"hu" => [
"10-as évek" => "10-es évek",
@ -307,21 +115,21 @@ final class NodaTimeAutotranslater {
/**
* Applies suffix format to a time string.
*
* @param string $tLang Two digit ANSI language code.
* @param NodaTimeAutotranslaterLocales $tLang Two digit ANSI language code.
* @param string $timeName Time name.
* @param NodaTimeAutotranslaterSuffixMode $suffixMode Suffix mode.
*
* @return string
*/
public static function applyBcBceFormat(string $tLang, string $timeName, NodaTimeAutotranslaterSuffixMode $suffixMode):string {
private static function _applyBcBceFormat(NodaTimeAutotranslaterLocales $tLang, string $timeName, NodaTimeAutotranslaterSuffixMode $suffixMode):string {
switch ($suffixMode) {
case NodaTimeAutotranslaterSuffixMode::ANY:
return $timeName;
case NodaTimeAutotranslaterSuffixMode::CE:
return \sprintf(NodaTimeAutotranslaterLocales::fromString($tLang)->eraFormatCeForSprintf(), $timeName);
return \sprintf($tLang->eraFormatCeForSprintf(), $timeName);
case NodaTimeAutotranslaterSuffixMode::BCE:
return \sprintf(NodaTimeAutotranslaterLocales::fromString($tLang)->eraFormatBceForSprintf(), $timeName);
return \sprintf($tLang->eraFormatBceForSprintf(), $timeName);
default:
throw new Exception("Unknown case encountered for time translations.");
}
@ -351,6 +159,8 @@ final class NodaTimeAutotranslater {
$output = [];
$cases = NodaTimeAutotranslaterLocales::cases();
// TODO: Run gettranslations first, then loop
foreach ($cases as $tLang) {
if ($suffixMode === NodaTimeAutotranslaterSuffixMode::BCE) {
@ -371,7 +181,7 @@ final class NodaTimeAutotranslater {
else $year = \sprintf($tLang->formatYearspanForSprintf(), (string)\abs($start), (string)\abs($end));
}
$output[$tLang->name] = self::applyBcBceFormat($tLang->name, $year, $suffixMode);
$output[$tLang->name] = self::_applyBcBceFormat($tLang, $year, $suffixMode);
}
return $output;
@ -446,17 +256,17 @@ final class NodaTimeAutotranslater {
$output = [];
if ($start_cen === $end_cen) {
foreach (self::LANGS_CENTURY_FORMAT as $tLang => $format) {
$tLangValue = \sprintf($format, (string)\abs($start_cen));
$output[$tLang] = self::applyBcBceFormat($tLang, $tLangValue, $suffixMode);
$cases = NodaTimeAutotranslaterLocales::cases();
foreach ($cases as $tLang) {
if ($start_cen === $end_cen) {
$tLangValue = \sprintf($tLang->formatCenturyForSprintf(), (string)\abs($start_cen));
}
}
else {
foreach (self::LANGS_CENTURIES_FORMAT as $tLang => $format) {
$tLangValue = \sprintf($format, (string)\abs($start_cen), (string)\abs($end_cen));
$output[$tLang] = self::applyBcBceFormat($tLang, $tLangValue, $suffixMode);
else {
$tLangValue = \sprintf($tLang->formatCenturiesForSprintf(), (string)\abs($start_cen), (string)\abs($end_cen));
}
$output[$tLang->name] = self::_applyBcBceFormat($tLang, $tLangValue, $suffixMode);
}
return $output;
@ -480,20 +290,21 @@ final class NodaTimeAutotranslater {
$output = [];
if ($start_cen === $end_cen - 9) {
foreach (self::LANGS_DECADE_FORMAT as $tLang => $format) {
$tLangValue = \sprintf($format, (string)$start_cen, (string)$end_cen);
if (!empty(self::LANGS_SYLLABLE_CLEANING[$tLang])) $tLangValue = \strtr($tLangValue, self::LANGS_SYLLABLE_CLEANING[$tLang]);
$output[$tLang] = self::applyBcBceFormat($tLang, $tLangValue, $suffixMode);
$cases = NodaTimeAutotranslaterLocales::cases();
foreach ($cases as $tLang) {
if ($start_cen === $end_cen - 9) {
$tLangValue = \sprintf($tLang->formatDecadeForSprintf(), (string)$start_cen, (string)$end_cen);
if (!empty(self::LANGS_SYLLABLE_CLEANING[$tLang->name])) $tLangValue = \strtr($tLangValue, self::LANGS_SYLLABLE_CLEANING[$tLang->name]);
}
}
else {
foreach (self::LANGS_DECADES_FORMAT as $tLang => $format) {
$tLangValue = \sprintf($format, (string)$start_cen, (string)($end_cen - 9));
if (!empty(self::LANGS_SYLLABLE_CLEANING[$tLang])) $tLangValue = \strtr($tLangValue, self::LANGS_SYLLABLE_CLEANING[$tLang]);
$output[$tLang] = self::applyBcBceFormat($tLang, $tLangValue, $suffixMode);
else {
$tLangValue = \sprintf($tLang->formatDecadesForSprintf(), (string)$start_cen, (string)($end_cen - 9));
if (!empty(self::LANGS_SYLLABLE_CLEANING[$tLang->name])) $tLangValue = \strtr($tLangValue, self::LANGS_SYLLABLE_CLEANING[$tLang->name]);
}
$output[$tLang->name] = self::_applyBcBceFormat($tLang, $tLangValue, $suffixMode);
}
return $output;
}
@ -515,12 +326,15 @@ final class NodaTimeAutotranslater {
$innerTimeInfo['zeit_ende'] = $timeInfo['zeit_beginn'];
$output = [];
foreach (self::LANGS_AFTER_START_FORMAT_YEAR as $tLang => $format) {
$innerTls = self::getTranslations($innerTimeInfo);
$dateAlone = self::getTranslations($innerTimeInfo)[$tLang];
$cases = NodaTimeAutotranslaterLocales::cases();
foreach ($cases as $tLang) {
$timeName = \sprintf($format, $dateAlone);
$output[$tLang] = $timeName;
$dateAlone = $innerTls[$tLang->name];
$timeName = \sprintf($tLang->formatYearsAfterStartForSprintf(), $dateAlone);
$output[$tLang->name] = $timeName;
}
return $output;
@ -546,12 +360,15 @@ final class NodaTimeAutotranslater {
$innerTimeInfo['zeit_ende'] = $timeInfo['zeit_beginn'];
$output = [];
foreach (self::LANGS_SINCE_START_FORMAT_YEAR as $tLang => $format) {
$innerTls = self::getTranslations($innerTimeInfo);
$dateAlone = self::getTranslations($innerTimeInfo)[$tLang];
$cases = NodaTimeAutotranslaterLocales::cases();
foreach ($cases as $tLang) {
$timeName = \sprintf($format, $dateAlone);
$output[$tLang] = $timeName;
$dateAlone = $innerTls[$tLang->name];
$timeName = \sprintf($tLang->formatYearsSinceStartForSprintf(), $dateAlone);
$output[$tLang->name] = $timeName;
}
return $output;
@ -576,12 +393,17 @@ final class NodaTimeAutotranslater {
$innerTimeInfo['zeit_beginn'] = $timeInfo['zeit_ende'];
$output = [];
foreach (self::LANGS_BEFORE_START_FORMAT_YEAR as $tLang => $format) {
$dateAlone = self::getTranslations($innerTimeInfo)[$tLang];
$innerTls = self::getTranslations($innerTimeInfo);
$cases = NodaTimeAutotranslaterLocales::cases();
foreach ($cases as $tLang) {
$dateAlone = $innerTls[$tLang->name];
$timeName = \sprintf($tLang->formatYearsBeforeStartForSprintf(), $dateAlone);
$output[$tLang->name] = $timeName;
$timeName = \sprintf($format, $dateAlone);
$output[$tLang] = $timeName;
}
return $output;
@ -606,12 +428,16 @@ final class NodaTimeAutotranslater {
$innerTimeInfo['zeit_beginn'] = $timeInfo['zeit_ende'];
$output = [];
foreach (self::LANGS_UNTIL_START_FORMAT_YEAR as $tLang => $format) {
$dateAlone = self::getTranslations($innerTimeInfo)[$tLang];
$innerTls = self::getTranslations($innerTimeInfo);
$timeName = \sprintf($format, $dateAlone);
$output[$tLang] = $timeName;
$cases = NodaTimeAutotranslaterLocales::cases();
foreach ($cases as $tLang) {
$dateAlone = $innerTls[$tLang->name];
$timeName = \sprintf($tLang->formatYearsUntilStartForSprintf(), $dateAlone);
$output[$tLang->name] = $timeName;
}
return $output;
@ -743,7 +569,7 @@ final class NodaTimeAutotranslater {
);
if (($tLangValue = $fmt->format($dateGeneral)) === false) continue;
$output[$tLang->name] = self::applyBcBceFormat($tLang->name, $tLangValue, $suffixMode);
$output[$tLang->name] = self::_applyBcBceFormat($tLang, $tLangValue, $suffixMode);
}

View File

@ -260,6 +260,275 @@ enum NodaTimeAutotranslaterLocales {
}
/**
* Returns the appropriate format for a timespan since a start year.
* Expects to be used as a format for sprintf.
* Example use case: Since 1921.
*
* @return string
*/
public function formatYearsSinceStartForSprintf():string {
return match($this) {
self::ar => '%s-',
self::de => 'Seit %s',
self::en => 'Since %s',
self::es => 'Desde %s',
self::fa => '%s-',
self::fr => 'Depuis %s',
self::hu => '%s-től',
self::id => 'Sejak %s',
self::it => 'Dal %s',
self::ka => '%s წლიდან',
self::ko => '%s부터',
self::pl => 'Od %s r.',
self::pt => '%s dC',
self::ro => 'Din %s',
self::ru => 'С %s г.',
self::ta => '%s முதல்',
self::tl => 'Mula noong %s',
self::tr => '%s-',
self::ja => '%s以来',
self::zh => '自%s以來',
};
}
/**
* Returns the appropriate format for a timespan after a start year.
* Expects to be used as a format for sprintf.
* Example use case: After 1921.
*
* @return string
*/
public function formatYearsAfterStartForSprintf():string {
return match($this) {
self::ar => '%s-',
self::de => 'Nach %s',
self::en => 'After %s',
self::es => 'Despues de %s',
self::fa => '%s-',
self::fr => 'Après %s',
self::hu => '%s után',
self::id => 'Setelah %s',
self::it => 'Dopo il %s',
self::ka => '%s წლის შემდეგ',
self::ko => '%s 년 이후',
self::pl => 'Po %s r.',
self::pt => 'Depois de %s',
self::ro => 'După %s',
self::ru => 'после %s г.',
self::ta => '%s க்குப் பிறகு',
self::tl => 'Pagkaraan ng %s',
self::tr => '%s-',
self::ja => '%s以降',
self::zh => '%s以后',
};
}
/**
* Returns the appropriate format for a timespan until an end year.
* Expects to be used as a format for sprintf.
* Example use case: Until 1921.
*
* @return string
*/
public function formatYearsUntilStartForSprintf():string {
return match($this) {
self::ar => '-%s',
self::de => 'Bis %s',
self::en => 'Until %s',
self::es => 'Hasta %s',
self::fa => '-%s',
self::fr => 'Jusqu\'en %s',
self::hu => '%s-ig',
self::id => 'Sampai %s',
self::it => 'Fino al %s',
self::ka => '%s წლამდე',
self::ko => '%s까지',
self::pl => 'do %s roku',
self::pt => 'até %s',
self::ro => 'până în %s',
self::ru => 'до %s г.',
self::ta => '%s வரை',
self::tl => 'Hanggang %s',
self::tr => '%s-',
self::ja => '%sまで',
self::zh => '直到%s',
};
}
/**
* Returns the appropriate format for a century name.
* Example use case: 19th century.
*
* @return string
*/
public function formatCenturyForSprintf():string {
return match($this) {
self::ar => 'القرن ال %s',
self::de => '%s. Jahrhundert',
self::en => '%s. century',
self::es => 'Siglo %s',
self::fa => 'قرن %s',
self::fr => '%sème siècle',
self::hu => '%s. század',
self::id => 'Abad ke-%s',
self::it => '%sesimo secolo',
self::ka => 'მე -%s საუკუნე',
self::ko => '%s 세기',
self::pl => '%s wiek',
self::pt => 'Século %s',
self::ro => 'Secolul al %s-lea',
self::ru => '%s век',
self::ta => '%s ஆம் நூற்றாண்டு',
self::tl => 'Ika-%s na siglo',
self::tr => '%s. yüzyıl',
self::ja => '%s世紀',
self::zh => '%s世紀',
};
}
/**
* Returns the appropriate format for a name spanning multiple centuries.
* Example use case: 19th-20th centuries.
*
* @return string
*/
public function formatCenturiesForSprintf():string {
return match($this) {
self::ar => 'القرن ال %s-%s',
self::de => '%s.-%s. Jahrhundert',
self::en => '%s.-%s. century',
self::es => 'Siglo %s-%s',
self::fa => 'قرن %s-%s',
self::fr => '%s-%sème siècle',
self::hu => '%s.-%s. század',
self::id => 'Abad ke-%s-%s',
self::it => '%s-%sesimo secolo',
self::ka => 'მე -%s-%s საუკუნე',
self::ko => '%s-%s 세기',
self::pl => '%s-%s wiek',
self::pt => 'Século %s-%s',
self::ro => 'Secolul al %s-%s-lea',
self::ru => '%s-%s век',
self::ta => '%s-%s ஆம் நூற்றாண்டு',
self::tl => 'Ika-%s hanggang ika-%s na siglo',
self::tr => '%s.-%s. yüzyıl',
self::ja => '%s世紀-%s世紀',
self::zh => '%s-%s世紀',
};
}
/**
* Returns the appropriate format for a name spanning a single decade.
* Example use case: 1920s.
*
* @return string
*/
public function formatDecadeForSprintf():string {
return match($this) {
self::ar => '%s-%s',
self::de => '%ser Jahre',
self::en => '%ss',
self::es => '%s-%s',
self::fa => 'دهه %s',
self::fr => 'Années %s',
self::hu => '%s-as évek',
self::id => 'Tahun %s-an',
self::it => '%ss',
self::ka => '%s-იანი წლები',
self::ko => '%s 년대',
self::pl => '%s roku',
self::pt => 'Década de %s',
self::ro => 'Anii %s',
self::ru => '%s-е годы',
self::ta => '%s கள்',
self::tl => '%ss',
self::tr => '%s\'ler',
self::ja => '%s年代',
self::zh => '%s年代',
};
}
/**
* Returns the appropriate format for a name spanning a some decades.
* Example use case: 1920s-1930s.
*
* @return string
*/
public function formatDecadesForSprintf():string {
return match($this) {
self::ar => '%s-%s',
self::de => '%s-%ser Jahre',
self::en => '%s-%ss',
self::es => '%s-%s',
self::fa => 'دهه %s-%s',
self::fr => 'Années %s-%s',
self::hu => '%s-%s-as évek',
self::id => 'Tahun %s-an sampai tahun %s-an',
self::it => '%s-%ss',
self::ka => '%s-%s-იანი წლები',
self::ko => '%s-%s 년대',
self::pl => '%s-%s roku',
self::pt => 'Décadas de %s-%s',
self::ro => 'Anii %s-%s',
self::ru => '%s-%s-е годы',
self::ta => '%s-%s கள்',
self::tl => '%s-%ss',
self::tr => '%s-%s\'ler',
self::ja => '%s年代から%s年代',
self::zh => '%s年代-%s年代',
};
}
/**
* Returns the appropriate format for a timespan before an end year.
* Expects to be used as a format for sprintf.
* Example use case: Before 1921.
*
* @return string
*/
public function formatYearsBeforeStartForSprintf():string {
return match($this) {
self::ar => '-%s',
self::de => 'Vor %s',
self::en => 'Before %s',
self::es => 'Antes de %s',
self::fa => '-%s',
self::fr => 'Avant %s',
self::hu => '%s előtt',
self::id => 'Sebelum %s',
self::it => 'Prima del %s',
self::ka => '%s წლამდე',
self::ko => '%s 년 이전',
self::pl => 'Przed %s',
self::pt => 'Antes de %s',
self::ro => 'înainte de %s',
self::ru => 'до %s г.',
self::ta => '%s க்கு முன்',
self::tl => 'Bago ang %s',
self::tr => '%s-',
self::ja => '%s以前',
self::zh => '%s之前',
};
}
/**
* Returns the appropriate format for formatting a full date (Y-m-d) in a given language
* using strftime.