140 lines
3.7 KiB
PHP
140 lines
3.7 KiB
PHP
<?PHP
|
|
/**
|
|
* Controls automatic translation of times.
|
|
*
|
|
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
|
*/
|
|
declare(strict_types = 1);
|
|
require_once __DIR__ . '/inc/datesByCountry.php';
|
|
|
|
/**
|
|
* Autotranslater class for times.
|
|
*/
|
|
final class NodaTimeAutotranslater {
|
|
|
|
const USECASE_MONTH = 1;
|
|
const USECASE_DAY = 2;
|
|
|
|
const LANGS_TO_LOCALES = [
|
|
'ar' => 'ar_SY.utf8',
|
|
'de' => 'de_DE.utf8',
|
|
'en' => 'en_US.utf8',
|
|
'es' => 'es_ES.utf8',
|
|
'fa' => 'fa_IR.UTF-8',
|
|
'fr' => 'fr_FR.utf8',
|
|
'hu' => 'hu_HU.utf8',
|
|
'id' => 'id_ID.utf8',
|
|
'it' => 'it_IT.utf8',
|
|
'ka' => 'ka_GE.UTF-8',
|
|
'ko' => 'ko_KR.UTF-8',
|
|
'pl' => 'pl_PL.utf8',
|
|
'pt' => 'pt_BR.utf8',
|
|
'ro' => 'ro_RO.UTF-8',
|
|
'ru' => 'ru_RU.UTF-8',
|
|
'ta' => 'ta_IN.UTF-8',
|
|
'tl' => 'tl_PH.utf8',
|
|
'tr' => 'tr_TR.utf8',
|
|
|
|
// Languages that don't really need a specific locale
|
|
'ja' => 'en_US.utf8',
|
|
'zh' => 'en_US.utf8',
|
|
];
|
|
|
|
/** @var MDMysqli */
|
|
private MDMysqli $_mysqli_noda;
|
|
/** @var integer */
|
|
private int $_znum;
|
|
/** @var MDMysqliStmt */
|
|
private MDMysqliStmt $_insertStmt;
|
|
|
|
/**
|
|
* Checks if a time is translatable.
|
|
* Translatable times have either a counting time day and month or at least a month.
|
|
*
|
|
* @param string $zeit_zaehlzeit_monat Counting time month.
|
|
*
|
|
* @return boolean
|
|
*/
|
|
public static function check_translatability(string $zeit_zaehlzeit_monat) {
|
|
|
|
if (trim($zeit_zaehlzeit_monat, ", .0") === "") {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
/**
|
|
* Runs autotranslater.
|
|
*
|
|
* @param array<integer|string> $timeInfo Time information.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function translate(array $timeInfo):void {
|
|
|
|
if (!self::check_translatability((string)$timeInfo['zeit_zaehlzeit_monat'])) {
|
|
throw new MDgenericInvalidInputsException("Non-translatable date");
|
|
}
|
|
|
|
if (trim((string)$timeInfo['zeit_zaehlzeit_tag'], ", .0") === "") {
|
|
$dateStr = "{$timeInfo['zeit_zaehlzeit_jahr']}-{$timeInfo['zeit_zaehlzeit_monat']}-05 00:00:01";
|
|
$usecase = self::USECASE_MONTH;
|
|
}
|
|
else {
|
|
$dateStr = "{$timeInfo['zeit_zaehlzeit_jahr']}-{$timeInfo['zeit_zaehlzeit_monat']}-{$timeInfo['zeit_zaehlzeit_tag']} 00:00:01";
|
|
$usecase = self::USECASE_DAY;
|
|
}
|
|
|
|
$dateGeneral = strtotime($dateStr);
|
|
|
|
foreach (self::LANGS_TO_LOCALES as $tLang => $locale) {
|
|
|
|
setlocale(LC_TIME, $locale);
|
|
if ($locale !== setlocale(LC_TIME, "0")) continue;
|
|
if ($usecase === self::USECASE_MONTH) $tLangValue = strftime(getMonthFormatByLang($tLang), $dateGeneral ?: 0);
|
|
else { # if ($usecase === self::USECASE_DAY)
|
|
$tLangValue = strftime(getDateFormatByLang($tLang), $dateGeneral ?: 0);
|
|
}
|
|
|
|
$this->_insertStmt->bind_param("iss", $this->_znum, $tLang, $tLangValue);
|
|
$this->_insertStmt->execute();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Constructor.
|
|
*
|
|
* @param MDMysqli $mysqli_noda Database connection.
|
|
* @param integer $znum Time ID.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct(MDMysqli $mysqli_noda, int $znum) {
|
|
|
|
$this->_mysqli_noda = $mysqli_noda;
|
|
$this->_znum = $znum;
|
|
|
|
$this->_insertStmt = $mysqli_noda->do_prepare("INSERT INTO `zeit_translation`
|
|
(`zeit_id`, `trans_language`, `trans_name`)
|
|
VALUES
|
|
(?, ?, ?)");
|
|
|
|
}
|
|
|
|
/**
|
|
* Destructor.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __destruct() {
|
|
|
|
$this->_insertStmt->close();
|
|
|
|
}
|
|
|
|
}
|