doc:plugins:calendar
Calendar Widget
Consistent Day Name Abbreviation
This guide explains how to generate and display localized day names in the Calendar widget, ensuring consistent abbreviations across different locales (e.g., German, Japanese). The key idea is to always fetch the full day name with '%A'
and then truncate it to the desired length using $day_name_length
.
Why Not '%a'?
'%a'
returns system-defined short names (e.g., βMoβ/βDiβ in German or βζζβ/βη«ζβ in Japanese), which can vary by environment and length.- Using
'%A'
ensures you always start with the full name (e.g., βMontagβ, βDienstagβ, βζζζ₯β, βη«ζζ₯β). Truncating with mb_substr gives you precise control over abbreviation length.
Implementation Steps
1. Fetch full day names
// Initialize day names array $day_names = array(); for ($n = 0, $t = (3 + $first_day) * 86400; $n < 7; $n++, $t += 86400) { // '%A' always returns the full day name in current locale $day_names[$n] = ucfirst(date_strformat('%A', $t)); }
2. Output with controlled abbreviation
// Output table headers for weekdays foreach ($day_names as $fullName) { // Truncate to $day_name_length characters if ($day_name_length > 0) { $label = mb_substr($fullName, 0, $day_name_length, $characterset); } else { // 0 means show full name $label = $fullName; } // Use the full name for the 'abbr' attribute $calendar .= '<th abbr="'.htmlentities($fullName).'">' .htmlentities($label).'</th>'; }
3. Adjust $day_name_length
- 3 β Three-character labels: e.g., βMonβ, βDieβ, βζ°΄ζβ
- 1 β Single-character labels: e.g., βMβ, βDβ, βη«β
- 0 β Full names: e.g., βMondayβ, βDienstagβ, βζζζ₯β
Examples
German (locale=βde-deβ, $day_name_length = 3):
- Full: Montag, Dienstag
- Truncated: Mon, Die
Japanese (locale=βja-jpβ, $day_name_length = 1):
- Full: ζζζ₯, η«ζζ₯
- Truncated: ζ, η«
Benefits
- Consistency: Always truncates to exactly
$day_name_length
characters, regardless of locale.
- Control: You decide the abbreviation scheme rather than relying on system defaults.
- Accessibility: The full name remains in the abbr attribute for screen readers and tooltips.
Integration
Ensure $characterset
matches your locale encoding and that set_locale()
is called appropriately.
doc/plugins/calendar.txt Β· Last modified: 2025/05/30 19:15 by fraenkiman