lorisleiva/cron-translator
Convert CRON expressions into human-readable schedules. Translate standard 5-field CRON strings like “*/2 * * * *” into phrases such as “Every 2 minutes”, with optional locale and 24-hour time formatting. Supports many languages (en, fr, de, es, etc.).
Installation:
composer require lorisleiva/cron-translator
Add to composer.json under require or require-dev depending on your needs.
First Use Case: Translate a cron expression in your Laravel app:
use Lorisleiva\CronTranslator\CronTranslator;
$humanReadable = CronTranslator::translate('* * * * *'); // => "Every minute"
Where to Look First:
tests/ for edge cases and validation patterns.src/CronTranslator.php for customization or extension points.Basic Translation in Controllers/Views:
// In a controller
public function showCronSchedule()
{
$cron = '* * * * *';
$translation = CronTranslator::translate($cron);
return view('schedules.show', compact('translation'));
}
Localization for Multilingual Apps:
// Dynamically fetch user locale (e.g., from session)
$locale = app()->getLocale();
$translation = CronTranslator::translate('0 16 * * 1', $locale);
24-Hour Time Formatting:
// For APIs or technical contexts
$translation = CronTranslator::translate('30 18 * * *', 'en', true); // => "Every day at 18:30"
Integration with Laravel Scheduling:
Use alongside Artisan::schedule() to document cron jobs:
// In app/Console/Kernel.php
protected function schedule(Schedule $schedule)
{
$schedule->command('cleanup')->cron('0 3 * * *');
$humanReadable = CronTranslator::translate('0 3 * * *'); // => "Every day at 3:00am"
Log::info("Scheduled cleanup: {$humanReadable}");
}
Batch Translation for Admin Panels:
public function index()
{
$cronJobs = [
'* * * * *',
'0 16 * * 1',
'0 0 1 1 *',
];
$translations = collect($cronJobs)->map(fn($cron) => CronTranslator::translate($cron));
return view('admin.cron_jobs', compact('translations'));
}
Customizing Translations: Override or extend translations via service provider:
// app/Providers/AppServiceProvider.php
public function boot()
{
$translator = new CronTranslator();
$translator->setCustomTranslations([
'en' => [
'minute' => 'tick', // Customize "minute" to "tick"
],
]);
}
Validation + Translation: Combine with Laravel Validation for user inputs:
$validator = Validator::make($request->all(), [
'cron' => 'required|cron_format',
]);
if ($validator->fails()) {
return response()->json(['error' => 'Invalid cron format']);
}
$translation = CronTranslator::translate($request->cron);
Caching Translations: Cache translations for performance in high-traffic apps:
$cacheKey = "cron_translation_{$cron}_{$locale}";
$translation = Cache::remember($cacheKey, now()->addHours(1), function() use ($cron, $locale) {
return CronTranslator::translate($cron, $locale);
});
API Responses: Return translations in API responses for clarity:
return response()->json([
'cron' => '0 16 * * 1',
'description' => CronTranslator::translate('0 16 * * 1'),
]);
Unsupported Cron Syntax:
* * * * *) and extended cron (e.g., */2, 1-5).L (last day of month), W (nearest weekday), ? (no specific value), or year fields in some contexts.Locale Fallbacks:
en. Always test edge cases:
$translation = CronTranslator::translate('* * * * *', 'xx'); // Falls back to English
Time Format Confusion:
true for 24-hour) defaults to false (12-hour). Explicitly set it for consistency:
// Avoid ambiguity
CronTranslator::translate('30 18 * * *', 'en', true); // 18:30
Performance with Complex Expressions:
1,2,3/5 * * * *) may impact performance.Weekday Ambiguity:
0 0 * * 0 can mean Sunday (0) or every day (*). The package resolves this contextually, but clarify in docs:
// Sunday at midnight
CronTranslator::translate('0 0 * * 0'); // => "Every Sunday at 12:00am"
Validate Cron Syntax First:
Use a library like drbico/cron-expression to validate before translation:
use Drbico\Cron\CronExpression;
try {
$expr = CronExpression::factory($cron);
$translation = CronTranslator::translate($cron);
} catch (\Exception $e) {
return "Invalid cron: {$e->getMessage()}";
}
Check for Typos in Locales:
frn instead of fr) will fall back to English.$supportedLocales = ['en', 'fr', 'de', /* ... */];
if (!in_array($locale, $supportedLocales)) {
$locale = 'en'; // Fallback
}
Debug Translation Logic:
src/CronTranslator.php to understand how expressions are parsed.Add Custom Locales:
Extend the translator with new locales by modifying the translations array:
$translator = new CronTranslator();
$translator->addTranslation('it', [
'minute' => 'minuto',
'hour' => 'ora',
// ... other translations
]);
Override Translation Logic:
Subclass CronTranslator and override translate() or helper methods:
class CustomCronTranslator extends CronTranslator
{
protected function translateMinute($minute)
{
return "custom minute: {$minute}";
}
}
Integrate with Laravel Localization: Sync translations with Laravel’s language files for consistency:
// In a service provider
$translator = new CronTranslator();
$translator->setTranslations(app('translator')->getLoader()->load('cron'));
Document Cron Examples: Store common cron expressions and their translations in a config file:
// config/cron.php
'examples' => [
'daily_at_9am' => ['cron' => '0 9 * * *', 'description' => 'Every day at 9:00am'],
],
Use in Blade Directives: Create a Blade directive for reusable translations:
// In a service provider
Blade::directive('cron', function ($expression) {
return "<?php echo Lorisleiva\CronTranslator\CronTranslator::translate({$expression}); ?>";
});
Usage:
@cron('* * * * *') // Renders "Every minute"
Log Translations for Auditing: Log cron translations alongside job executions:
Log::info('Job executed', [
'cron' => $job->cron,
'description' => CronTranslator::translate($job->cron),
]);
Test Edge Cases: Write tests for:
How can I help you explore Laravel packages today?