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: by fraenkiman

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki