Fixed: fix translation issue and change function name

This commit is contained in:
amit sharma
2026-07-07 18:50:43 +05:30
parent fe3b4d6a61
commit e55012fd2f
2 changed files with 125 additions and 121 deletions

View File

@@ -27,8 +27,6 @@ if (!defined('_PS_VERSION_')) {
class QctmCronExpressionTranslator
{
const MODULE_NAME = 'qlocrontaskmanager';
const WEEKDAYS = array(
0 => 'Sunday',
1 => 'Monday',
@@ -55,149 +53,154 @@ class QctmCronExpressionTranslator
12 => 'December',
);
public static function translate($expression)
protected $module;
public function __construct()
{
$this->module = Module::getInstanceByName('qlocrontaskmanager');
}
public function getCronExpressionTranslation($expression)
{
$parts = preg_split('/\s+/', trim($expression));
if (count($parts) !== 5) {
return self::trans('Invalid cron expression');
return $this->module->l('Invalid cron expression');
}
list($minute, $hour, $day, $month, $weekday) = $parts;
$hasFixedTime = $this->isNumeric($hour) && $this->isNumeric($minute);
$dayMonthWildcard = ($day == '*' && $month == '*');
$monthWeekdayWildcard = ($month == '*' && $weekday == '*');
$everyMinuteWildcard = ($minute == '*' && $hour == '*');
if ($expression === '* * * * *') {
return self::trans('Every minute');
return $this->module->l('Every minute');
}
if (preg_match('/^\*\/(\d+)$/', $minute, $m)
&& $hour == '*' && $day == '*' && $month == '*' && $weekday == '*') {
return self::trans('Every %d minutes', (int) $m[1]);
if (preg_match('/^\*\/(\d+)$/', $minute, $m) && $hour == '*' && $dayMonthWildcard && $weekday == '*') {
return vsprintf($this->module->l('Every %d minutes'), array((int) $m[1]));
}
if ($minute == '0' && $hour == '*' && $day == '*' && $month == '*' && $weekday == '*') {
return self::trans('Every hour');
if ($minute == '0' && $hour == '*' && $dayMonthWildcard && $weekday == '*') {
return $this->module->l('Every hour');
}
if ($minute == '0'
&& preg_match('/^\*\/(\d+)$/', $hour, $m)
&& $day == '*' && $month == '*' && $weekday == '*') {
return self::trans('Every %d hours', (int) $m[1]);
if ($minute == '0' && preg_match('/^\*\/(\d+)$/', $hour, $m) && $dayMonthWildcard && $weekday == '*') {
return vsprintf($this->module->l('Every %d hours'), array((int) $m[1]));
}
if (self::isNumeric($hour) && self::isNumeric($minute) && $day == '*' && $month == '*' && $weekday == '*') {
return self::trans('Every day at %s', self::hourLabel($hour, $minute));
if ($hasFixedTime && $dayMonthWildcard && $weekday == '*') {
return vsprintf($this->module->l('Every day at %s'), array($this->hourLabel($hour, $minute)));
}
if (self::isNumeric($hour) && self::isNumeric($minute) && $day == '*' && $weekday == '*'
&& preg_match('/^\*\/(\d+)$/', $month, $mm)) {
return self::trans(
'Every day every %1$d months at %2$s',
array((int) $mm[1], self::hourLabel($hour, $minute))
if ($hasFixedTime && $day == '*' && $weekday == '*' && preg_match('/^\*\/(\d+)$/', $month, $mm)) {
return vsprintf(
$this->module->l('Every day every %1$d months at %2$s'),
array((int) $mm[1], $this->hourLabel($hour, $minute))
);
}
if (self::isNumeric($hour) && self::isNumeric($minute) && $day == '*' && $month == '*' && $weekday == '1-5') {
return self::trans('Every weekday at %s', self::hourLabel($hour, $minute));
if ($hasFixedTime && $dayMonthWildcard && $weekday == '1-5') {
return vsprintf($this->module->l('Every weekday at %s'), array($this->hourLabel($hour, $minute)));
}
if (self::isNumeric($hour) && self::isNumeric($minute) && $day == '*' && $month == '*' && $weekday == '0,6') {
return self::trans('Every weekend at %s', self::hourLabel($hour, $minute));
if ($hasFixedTime && $dayMonthWildcard && $weekday == '0,6') {
return vsprintf($this->module->l('Every weekend at %s'), array($this->hourLabel($hour, $minute)));
}
if (self::isNumeric($hour) && self::isNumeric($minute) && $day == '*' && $month == '*' && self::isNumeric($weekday)) {
return self::trans(
'Every %1$s at %2$s',
array(self::weekdayName($weekday), self::hourLabel($hour, $minute))
if ($hasFixedTime && $dayMonthWildcard && $this->isNumeric($weekday)) {
return vsprintf(
$this->module->l('Every %1$s at %2$s'),
array($this->weekdayName($weekday), $this->hourLabel($hour, $minute))
);
}
if (strpos($weekday, ',') !== false
&& self::isNumeric($hour) && self::isNumeric($minute) && $day == '*' && $month == '*') {
return self::trans(
'Every %1$s at %2$s',
array(self::joinNames($weekday, 'weekdayName'), self::hourLabel($hour, $minute))
if (strpos($weekday, ',') !== false && $hasFixedTime && $dayMonthWildcard) {
return vsprintf(
$this->module->l('Every %1$s at %2$s'),
array($this->joinNames($weekday, 'weekdayName'), $this->hourLabel($hour, $minute))
);
}
if (self::isNumeric($day) && self::isNumeric($hour) && self::isNumeric($minute) && $month == '*' && $weekday == '*') {
return self::trans(
'On the %1$s of every month at %2$s',
array(self::ordinal($day), self::hourLabel($hour, $minute))
if ($this->isNumeric($day) && $hasFixedTime && $monthWeekdayWildcard) {
return vsprintf(
$this->module->l('On the %1$s of every month at %2$s'),
array($this->ordinal($day), $this->hourLabel($hour, $minute))
);
}
if (preg_match('/^(\d+)-(\d+)$/', $day, $dm)
&& self::isNumeric($hour) && self::isNumeric($minute) && $month == '*' && $weekday == '*') {
return self::trans(
'Every day from the %1$s to the %2$s of each month at %3$s',
array(self::ordinal($dm[1]), self::ordinal($dm[2]), self::hourLabel($hour, $minute))
if (preg_match('/^(\d+)-(\d+)$/', $day, $dm) && $hasFixedTime && $monthWeekdayWildcard) {
return vsprintf(
$this->module->l('Every day from the %1$s to the %2$s of each month at %3$s'),
array($this->ordinal($dm[1]), $this->ordinal($dm[2]), $this->hourLabel($hour, $minute))
);
}
if (strpos($day, ',') !== false
&& self::isNumeric($hour) && self::isNumeric($minute) && $month == '*' && $weekday == '*') {
return self::trans(
'On the %1$s of each month at %2$s',
array(self::joinOrdinals($day), self::hourLabel($hour, $minute))
if (strpos($day, ',') !== false && $hasFixedTime && $monthWeekdayWildcard) {
return vsprintf(
$this->module->l('On the %1$s of each month at %2$s'),
array($this->joinOrdinals($day), $this->hourLabel($hour, $minute))
);
}
if (self::isNumeric($day) && self::isNumeric($month) && self::isNumeric($hour) && self::isNumeric($minute) && $weekday == '*') {
return self::trans(
'Every %1$s %2$s at %3$s',
array(self::monthName($month), self::ordinal($day), self::hourLabel($hour, $minute))
if ($this->isNumeric($day) && $this->isNumeric($month) && $hasFixedTime && $weekday == '*') {
return vsprintf(
$this->module->l('Every %1$s %2$s at %3$s'),
array($this->monthName($month), $this->ordinal($day), $this->hourLabel($hour, $minute))
);
}
if ($minute == '*' && $hour == '*' && $day == '*' && $month == '*' && self::isNumeric($weekday)) {
return self::trans('Every minute on %s', self::weekdayName($weekday));
if ($everyMinuteWildcard && $dayMonthWildcard && $this->isNumeric($weekday)) {
return vsprintf($this->module->l('Every minute on %s'), array($this->weekdayName($weekday)));
}
if ($minute == '*' && $hour == '*' && $day == '*' && $month == '*' && $weekday == '1-5') {
return self::trans('Every minute on weekdays');
if ($everyMinuteWildcard && $dayMonthWildcard && $weekday == '1-5') {
return $this->module->l('Every minute on weekdays');
}
if ($minute == '*' && $hour == '*' && $day == '*' && $month == '*' && $weekday == '0,6') {
return self::trans('Every minute on weekends');
if ($everyMinuteWildcard && $dayMonthWildcard && $weekday == '0,6') {
return $this->module->l('Every minute on weekends');
}
if (self::isNumeric($minute) && self::isNumeric($hour)) {
$when = self::trans('At %s', self::hourLabel($hour, $minute));
if ($hasFixedTime) {
$when = vsprintf($this->module->l('At %s'), array($this->hourLabel($hour, $minute)));
} else {
$minuteFreq = null;
if ($minute === '*') {
$minuteFreq = self::trans('Every minute');
$minuteFreq = $this->module->l('Every minute');
} elseif (preg_match('/^\*\/(\d+)$/', $minute, $m)) {
$minuteFreq = self::trans('Every %d minutes', (int) $m[1]);
$minuteFreq = vsprintf($this->module->l('Every %d minutes'), array((int) $m[1]));
}
if (preg_match('/^(\d+)-(\d+)\/(\d+)$/', $hour, $hs) && self::isNumeric($minute)) {
$when = self::trans(
'Every %1$d hours between %2$s and %3$s',
array((int) $hs[3], self::hourLabel($hs[1], $minute), self::hourLabel($hs[2], $minute))
if (preg_match('/^(\d+)-(\d+)\/(\d+)$/', $hour, $hs) && $this->isNumeric($minute)) {
$when = vsprintf(
$this->module->l('Every %1$d hours between %2$s and %3$s'),
array((int) $hs[3], $this->hourLabel($hs[1], $minute), $this->hourLabel($hs[2], $minute))
);
} elseif ($minuteFreq !== null) {
if ($hour === '*') {
$when = $minuteFreq;
} elseif (self::isNumeric($hour)) {
$when = self::trans('%1$s, during hour %2$s', array($minuteFreq, $hour));
} elseif ($this->isNumeric($hour)) {
$when = vsprintf($this->module->l('%1$s, during hour %2$s'), array($minuteFreq, $hour));
} elseif (preg_match('/^(\d+)-(\d+)$/', $hour, $hm)) {
$when = self::trans(
'%1$s between %2$s and %3$s',
array($minuteFreq, self::hourLabel($hm[1], 0), self::hourLabel($hm[2], 59))
$when = vsprintf(
$this->module->l('%1$s between %2$s and %3$s'),
array($minuteFreq, $this->hourLabel($hm[1], 0), $this->hourLabel($hm[2], 59))
);
} else {
$when = self::trans('%1$s, during hour %2$s', array($minuteFreq, $hour));
$when = vsprintf($this->module->l('%1$s, during hour %2$s'), array($minuteFreq, $hour));
}
} elseif (self::isNumeric($minute) && $hour === '*') {
$when = self::trans('Every hour, at minute %s', $minute);
} elseif (self::isNumeric($minute) && preg_match('/^(\d+)-(\d+)$/', $hour, $hm)) {
$when = self::trans(
'At minute %1$s, between %2$s and %3$s',
array($minute, self::hourLabel($hm[1], $minute), self::hourLabel($hm[2], $minute))
} elseif ($this->isNumeric($minute) && $hour === '*') {
$when = vsprintf($this->module->l('Every hour, at minute %s'), array($minute));
} elseif ($this->isNumeric($minute) && preg_match('/^(\d+)-(\d+)$/', $hour, $hm)) {
$when = vsprintf(
$this->module->l('At minute %1$s, between %2$s and %3$s'),
array($minute, $this->hourLabel($hm[1], $minute), $this->hourLabel($hm[2], $minute))
);
} else {
$when = self::trans('At minute %1$s, hour %2$s', array($minute, $hour));
$when = vsprintf($this->module->l('At minute %1$s, hour %2$s'), array($minute, $hour));
}
}
@@ -205,47 +208,53 @@ class QctmCronExpressionTranslator
if ($day !== '*') {
if (preg_match('/^(\d+)-(\d+)$/', $day, $dm)) {
$conditions[] = self::trans(
'from the %1$s to the %2$s of the month',
array(self::ordinal($dm[1]), self::ordinal($dm[2]))
$conditions[] = vsprintf(
$this->module->l('from the %1$s to the %2$s of the month'),
array($this->ordinal($dm[1]), $this->ordinal($dm[2]))
);
} elseif (strpos($day, ',') !== false) {
$conditions[] = self::trans('on the %s of the month', self::joinOrdinals($day));
} elseif (self::isNumeric($day)) {
$conditions[] = self::trans('on the %s of the month', self::ordinal($day));
$conditions[] = vsprintf($this->module->l('on the %s of the month'), array($this->joinOrdinals($day)));
} elseif ($this->isNumeric($day)) {
$conditions[] = vsprintf($this->module->l('on the %s of the month'), array($this->ordinal($day)));
} else {
$conditions[] = self::trans('on day %s of the month', $day);
$conditions[] = vsprintf($this->module->l('on day %s of the month'), array($day));
}
}
if ($month !== '*') {
if (preg_match('/^\*\/(\d+)$/', $month, $mf)) {
$conditions[] = self::trans('every %d months', (int) $mf[1]);
$conditions[] = vsprintf($this->module->l('every %d months'), array((int) $mf[1]));
} else {
if (preg_match('/^(\d+)-(\d+)$/', $month, $mm)) {
$monthDesc = self::trans('%1$s through %2$s', array(self::monthName($mm[1]), self::monthName($mm[2])));
$monthDesc = vsprintf(
$this->module->l('%1$s through %2$s'),
array($this->monthName($mm[1]), $this->monthName($mm[2]))
);
} elseif (strpos($month, ',') !== false) {
$monthDesc = self::joinNames($month, 'monthName');
$monthDesc = $this->joinNames($month, 'monthName');
} else {
$monthDesc = self::monthName($month);
$monthDesc = $this->monthName($month);
}
$conditions[] = self::trans('in %s', $monthDesc);
$conditions[] = vsprintf($this->module->l('in %s'), array($monthDesc));
}
}
if ($weekday !== '*') {
if ($weekday === '1-5') {
$weekdayDesc = self::trans('weekdays');
$weekdayDesc = $this->module->l('weekdays');
} elseif ($weekday === '0,6' || $weekday === '6,0') {
$weekdayDesc = self::trans('weekends');
$weekdayDesc = $this->module->l('weekends');
} elseif (preg_match('/^(\d+)-(\d+)$/', $weekday, $wm)) {
$weekdayDesc = self::trans('%1$s through %2$s', array(self::weekdayName($wm[1]), self::weekdayName($wm[2])));
$weekdayDesc = vsprintf(
$this->module->l('%1$s through %2$s'),
array($this->weekdayName($wm[1]), $this->weekdayName($wm[2]))
);
} elseif (strpos($weekday, ',') !== false) {
$weekdayDesc = self::joinNames($weekday, 'weekdayName');
$weekdayDesc = $this->joinNames($weekday, 'weekdayName');
} else {
$weekdayDesc = self::weekdayName($weekday);
$weekdayDesc = $this->weekdayName($weekday);
}
$conditions[] = self::trans('on %s', $weekdayDesc);
$conditions[] = vsprintf($this->module->l('on %s'), array($weekdayDesc));
}
if (empty($conditions)) {
@@ -255,34 +264,29 @@ class QctmCronExpressionTranslator
return $when . ' ' . implode(', ', $conditions);
}
public static function trans($string, $sprintf = null)
{
return Translate::getModuleTranslation(self::MODULE_NAME, $string, 'QctmCronExpressionTranslator', $sprintf);
}
public static function isNumeric($value)
public function isNumeric($value)
{
return preg_match('/^\d+$/', $value) === 1;
}
public static function weekdayName($weekday)
public function weekdayName($weekday)
{
return (self::isNumeric($weekday) && isset(self::WEEKDAYS[(int) $weekday]))
? self::trans(self::WEEKDAYS[(int) $weekday])
return ($this->isNumeric($weekday) && isset(self::WEEKDAYS[(int) $weekday]))
? $this->module->l(self::WEEKDAYS[(int) $weekday])
: $weekday;
}
public static function monthName($month)
public function monthName($month)
{
return (self::isNumeric($month) && isset(self::MONTHS[(int) $month]))
? self::trans(self::MONTHS[(int) $month])
return ($this->isNumeric($month) && isset(self::MONTHS[(int) $month]))
? $this->module->l(self::MONTHS[(int) $month])
: $month;
}
public static function hourLabel($hour, $minute = null)
public function hourLabel($hour, $minute = null)
{
$h = (int) $hour;
$suffix = $h >= 12 ? self::trans('PM') : self::trans('AM');
$suffix = $h >= 12 ? $this->module->l('PM') : $this->module->l('AM');
$h12 = $h % 12;
if ($h12 === 0) {
@@ -294,12 +298,12 @@ class QctmCronExpressionTranslator
: sprintf('%d:%02d %s', $h12, (int) $minute, $suffix);
}
public static function joinNames($csv, $nameMethod)
public function joinNames($csv, $nameMethod)
{
$names = array();
foreach (explode(',', $csv) as $token) {
$names[] = self::$nameMethod($token);
$names[] = $this->$nameMethod($token);
}
if (count($names) === 1) {
@@ -308,10 +312,10 @@ class QctmCronExpressionTranslator
$last = array_pop($names);
return implode(', ', $names) . ' ' . self::trans('and') . ' ' . $last;
return implode(', ', $names) . ' ' . $this->module->l('and') . ' ' . $last;
}
public static function ordinal($number)
public function ordinal($number)
{
$number = (int) $number;
@@ -336,12 +340,12 @@ class QctmCronExpressionTranslator
return $number . $suffix;
}
public static function joinOrdinals($csv)
public function joinOrdinals($csv)
{
$ordinals = array();
foreach (explode(',', $csv) as $token) {
$ordinals[] = self::isNumeric($token) ? self::ordinal($token) : $token;
$ordinals[] = $this->isNumeric($token) ? $this->ordinal($token) : $token;
}
if (count($ordinals) === 1) {
@@ -350,6 +354,6 @@ class QctmCronExpressionTranslator
$last = array_pop($ordinals);
return implode(', ', $ordinals) . ' ' . self::trans('and') . ' ' . $last;
return implode(', ', $ordinals) . ' ' . $this->module->l('and') . ' ' . $last;
}
}

View File

@@ -178,7 +178,7 @@ class AdminCronTaskManagerController extends ModuleAdminController
$isValidExpression = \Cron\CronExpression::isValidExpression($cronExpression);
if ($isValidExpression) {
$readable = $this->l('Runs ') . QctmCronExpressionTranslator::translate($cronExpression);
$readable = $this->l('Runs ') . (new QctmCronExpressionTranslator())->getCronExpressionTranslation($cronExpression);
} else {
$readable = $this->l('Invalid cron expression.');
}
@@ -271,7 +271,7 @@ class AdminCronTaskManagerController extends ModuleAdminController
if (\Cron\CronExpression::isValidExpression($expression)) {
$result['valid'] = true;
$result['readable'] = $this->l('Runs ') . QctmCronExpressionTranslator::translate($expression);
$result['readable'] = $this->l('Runs ') . (new QctmCronExpressionTranslator())->getCronExpressionTranslation($expression);
}
die(Tools::jsonEncode($result));
@@ -390,7 +390,7 @@ class AdminCronTaskManagerController extends ModuleAdminController
public function getCronWithReadable($cronExpression, $row)
{
$readable = \Cron\CronExpression::isValidExpression($cronExpression)
? QctmCronExpressionTranslator::translate($cronExpression)
? (new QctmCronExpressionTranslator())->getCronExpressionTranslation($cronExpression)
: $cronExpression;
return '<code>' . htmlspecialchars($cronExpression) . '</code>'