User Tools

Site Tools


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

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki