I18n
The “I18n” class provides internationalization and localization features for MintyPHP, including formatting for currency, dates, times, and translation management for multiple locales.
Price
I18n::price($price, $minDecimals = 2, $maxDecimals = 2): string
Format a price with currency symbol (€).
Parameters:
$price- The price value (float, int, or null)$minDecimals- Minimum decimal places (default: 2)$maxDecimals- Maximum decimal places (default: 2)
Returns formatted price string with currency symbol, or empty string if null.
Example:
echo I18n::price(1234.56);
// Output (English): € 1,234.56
// Output (German): € 1.234,56
Currency
I18n::currency($currency, $minDecimals = 2, $maxDecimals = 2): string
Format a currency value without currency symbol.
Parameters:
$currency- The currency value (float, int, or null)$minDecimals- Minimum decimal places (default: 2)$maxDecimals- Maximum decimal places (default: 2)
Returns formatted currency string without symbol, or empty string if null.
Example:
echo I18n::currency(1234.56);
// Output (English): 1,234.56
// Output (German): 1.234,56
// Output (Dutch): 1.234,56
Controlling decimal precision:
echo I18n::currency(1234.5, 2, 3);
// Output: 1,234.50 (padded to min 2 decimals)
echo I18n::currency(1234.567, 2, 3);
// Output: 1,234.567 (displays up to 3 decimals)
Date
I18n::date($str): string
Format a date string according to locale.
Example:
echo I18n::date('2024-03-15');
// Output (English): 03/15/2024
// Output (German): 15.03.2024
// Output (Dutch): 15-03-2024
Datetime
I18n::datetime($str): string
Format a datetime string according to locale.
Example:
echo I18n::datetime('2024-03-15 14:30:45');
// Output (English): 03/15/2024 14:30:45
// Output (German): 15.03.2024 14:30:45
// Output (Dutch): 15-03-2024 14:30:45
Time
I18n::time($hours, $minutes, $seconds = 0): string
Format a time value.
Example:
echo I18n::time(14, 30, 45);
// Output: 14:30:45
echo I18n::time(14, 30);
// Output: 14:30:00
Duration
I18n::duration($seconds, $trim = false): string
Format a duration in seconds as HH:MM:SS.
Parameters:
$seconds- Duration in seconds$trim- Whether to trim leading zeros (default: false)
Example:
echo I18n::duration(3665);
// Output: 01:01:05
echo I18n::duration(125);
// Output: 00:02:05
echo I18n::duration(125, true);
// Output: 02:05 (trimmed)
echo I18n::duration(3665, true);
// Output: 01:01:05 (not trimmed, has hours)
Weekday
I18n::weekDay($dayOfWeek): string
Get the localized name of a weekday.
Parameters:
$dayOfWeek- Day of week (0-7, where 0 and 7 are Sunday)
Example:
echo I18n::weekDay(1);
// Output (English): Monday
// Output (German): Montag
// Output (Dutch): maandag
echo I18n::weekDay(0);
// Output (English): Sunday
// Output (German): Sonntag
Month Name
I18n::monthName($monthOfYear): string
Get the localized name of a month.
Parameters:
$monthOfYear- Month number (1-12)
Example:
echo I18n::monthName(1);
// Output (English): January
// Output (German): Januar
// Output (Dutch): januari
echo I18n::monthName(12);
// Output (English): December
// Output (German): Dezember
Datetime Short
I18n::datetimeShort($str): string
Format a datetime string in a shortened format. Smart formatting that shows:
- Year if different from current year
- Time (HH:MM) if within 24 hours
- Day abbreviation + time if within 7 days
- Day abbreviation + date otherwise
Example:
// Assuming today is 2024-03-20
echo I18n::datetimeShort('2023-03-15 10:30:00');
// Output: 3/15/2023 (different year)
echo I18n::datetimeShort('2024-03-20 10:30:00');
// Output: 10:30 (within 24 hours)
echo I18n::datetimeShort('2024-03-18 10:30:00');
// Output: Mo 10:30 (within 7 days)
echo I18n::datetimeShort('2024-03-10 10:30:00');
// Output: Su 3/10 (older than 7 days)
Translate
I18n::translate($id): string
Translate a string ID to localized text. Loads translation files from i18n/{domain}_{locale}.json on demand.
Parameters:
$id- The translation ID (dot-notation supported)
Returns the translated text, or the ID itself if translation not found.
Example:
echo I18n::translate('welcome.message');
// Loads from: i18n/default_en.json
// Returns: "Welcome to our website!"
echo I18n::translate('errors.not_found');
// Returns: "Page not found" (or the key if not in translation file)
Translation file format (i18n/default_en.json):
{
"welcome.message": "Welcome to our website!",
"errors.not_found": "Page not found",
"errors.forbidden": "Access denied"
}
Configuration
The I18n class can be configured by setting static properties in config/config.php:
use MintyPHP\I18n;
I18n::$locale = 'de'; // Current locale (e.g., 'en', 'de', 'nl')
I18n::$defaultLocale = 'en'; // Fallback locale
I18n::$domain = 'default'; // Translation domain
Locale
The active locale determines formatting for currency, dates, and translations.
I18n::$locale = 'nl'; // Dutch formatting
Default Locale
The fallback locale used when translations are not found in the current locale.
I18n::$defaultLocale = 'en';
Domain
The translation domain used for loading translation files. Multiple domains allow separate translation files for different parts of your application.
I18n::$domain = 'admin'; // Loads i18n/admin_en.json
Supported Locales
MintyPHP supports extensive locale formatting for:
Western European:
en- Englishde- Germanfr- Frenchnl- Dutches- Spanishit- Italianpt- Portuguese
Nordic:
dk- Danishse- Swedishfi- Finnish
Eastern European:
pl- Polishbg- Bulgarianro- Romanianlv- Latvianlt- Lithuanianee- Estonian
Each locale has proper formatting for:
- Thousand separators and decimal points
- Date and time formats
- Weekday and month names
Usage with Translation Function
The global t() function is a shortcut for translation with sprintf-style formatting:
function t() {
$arguments = func_get_args();
$arguments[0] = I18n::translate($arguments[0]);
return call_user_func_array('sprintf', $arguments);
}
Usage:
echo t('welcome.user', $username);
// If translation is "Welcome %s!", outputs: "Welcome John!"
echo t('cart.items', $count);
// If translation is "You have %d items", outputs: "You have 5 items"
Complete Example
use MintyPHP\I18n;
// Configure locale
I18n::$locale = 'de';
I18n::$defaultLocale = 'en';
// Format prices
$price = 1299.99;
echo I18n::price($price); // € 1.299,99
// Format dates
$date = '2024-03-15';
echo I18n::date($date); // 15.03.2024
$datetime = '2024-03-15 14:30:45';
echo I18n::datetime($datetime); // 15.03.2024 14:30:45
// Get localized names
echo I18n::weekDay(1); // Montag
echo I18n::monthName(3); // März
// Format durations
echo I18n::duration(7265); // 02:01:05
echo I18n::duration(125, true); // 02:05
// Translate strings
echo I18n::translate('welcome.message');
// Returns translated text from i18n/default_de.json
UTC Date/Time Methods
The I18n class also provides UTC-aware variants:
I18n::dateUtc($str): string // Format UTC date
I18n::datetimeUtc($str): string // Format UTC datetime
I18n::timeUtc($h, $m, $s): string // Format UTC time
These methods automatically convert UTC times to the local timezone before formatting.
Best Practices
-
Set locale early: Configure
I18n::$localein your config file or based on user preferences. -
Use translation files: Store all user-facing strings in translation files, not hardcoded in PHP.
-
Consistent formatting: Use I18n methods consistently throughout your application for all dates, times, and currency values.
- Translation file organization: Use dot-notation for translation IDs to organize them logically:
{ "nav.home": "Home", "nav.about": "About", "errors.404": "Page not found", "errors.500": "Server error" } -
Handle null values: The price() and currency() methods return empty strings for null values, making them safe for optional fields.
- Cache translations: Translation files are loaded once per request and cached in memory.
MintyPHP