CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-react-aria--i18n

Comprehensive internationalization support for React applications with locale-aware hooks and utilities

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

date-time-formatting.mddocs/

Date and Time Formatting

Locale-aware date and time formatting with calendar support, timezone handling, and automatic locale updates.

Capabilities

useDateFormatter Hook

Provides localized date formatting for the current locale with automatic caching and locale updates.

/**
 * Provides localized date formatting for the current locale. Automatically updates when the locale changes,
 * and handles caching of the date formatter for performance.
 * @param options - Formatting options extending Intl.DateTimeFormatOptions
 * @returns DateFormatter instance from @internationalized/date
 */
function useDateFormatter(options?: DateFormatterOptions): DateFormatter;

interface DateFormatterOptions extends Intl.DateTimeFormatOptions {
  /** Calendar system to use (e.g., 'gregory', 'buddhist', 'islamic') */
  calendar?: string;
}

Usage Examples:

import { useDateFormatter, I18nProvider } from "@react-aria/i18n";

// Basic date formatting
function DateDisplay() {
  const formatter = useDateFormatter({ 
    dateStyle: "long" 
  });
  
  return <p>{formatter.format(new Date())}</p>;
  // US English: "December 25, 2023"
  // Spanish: "25 de diciembre de 2023"
}

// Time formatting with specific options
function TimeDisplay() {
  const formatter = useDateFormatter({
    timeStyle: "medium",
    hour12: false
  });
  
  return <p>{formatter.format(new Date())}</p>;
  // "14:30:00"
}

// Custom format with multiple fields
function CustomDateFormat() {
  const formatter = useDateFormatter({
    weekday: "long",
    year: "numeric",
    month: "long",
    day: "numeric"
  });
  
  return <p>{formatter.format(new Date())}</p>;
  // "Monday, December 25, 2023"
}

// Calendar-specific formatting
function BuddhistCalendarDate() {
  const formatter = useDateFormatter({
    calendar: "buddhist",
    dateStyle: "full"
  });
  
  return <p>{formatter.format(new Date())}</p>;
  // Uses Buddhist calendar era
}

// Responsive to locale changes
function LocaleAwareDateDisplay() {
  return (
    <div>
      <I18nProvider locale="en-US">
        <DateDisplay />
      </I18nProvider>
      <I18nProvider locale="ja-JP">
        <DateDisplay />
      </I18nProvider>
    </div>
  );
}

DateFormatter Methods

The DateFormatter instance provides additional methods beyond basic formatting.

interface DateFormatter {
  /** Format a date to a string */
  format(date: Date): string;
  /** Format a date to an array of parts with type information */
  formatToParts(date: Date): Intl.DateTimeFormatPart[];
  /** Format a date range between two dates */
  formatRange(startDate: Date, endDate: Date): string;
  /** Format a date range to parts */
  formatRangeToParts(startDate: Date, endDate: Date): Intl.DateTimeFormatPart[];
  /** Get resolved formatting options */
  resolvedOptions(): Intl.ResolvedDateTimeFormatOptions;
}

Advanced Usage Examples:

function AdvancedDateFormatting() {
  const formatter = useDateFormatter({
    year: "numeric",
    month: "long",
    day: "numeric"
  });
  
  const date = new Date();
  const startDate = new Date();
  const endDate = new Date(Date.now() + 7 * 24 * 60 * 60 * 1000); // 7 days later
  
  return (
    <div>
      {/* Basic formatting */}
      <p>Date: {formatter.format(date)}</p>
      
      {/* Parts formatting for custom styling */}
      <p>
        Parts: {formatter.formatToParts(date).map((part, i) => (
          <span key={i} className={`date-${part.type}`}>
            {part.value}
          </span>
        ))}
      </p>
      
      {/* Date range formatting */}
      <p>Range: {formatter.formatRange(startDate, endDate)}</p>
      
      {/* Resolved options inspection */}
      <p>Locale: {formatter.resolvedOptions().locale}</p>
    </div>
  );
}

Calendar System Support

Support for various calendar systems beyond the Gregorian calendar.

Supported Calendar Systems:

  • gregory - Gregorian calendar (default)
  • buddhist - Buddhist calendar
  • chinese - Chinese calendar
  • coptic - Coptic calendar
  • dangi - Korean Dangi calendar
  • ethioaa - Ethiopian calendar
  • ethiopic - Ethiopian calendar (Amete Alem)
  • hebrew - Hebrew calendar
  • indian - Indian national calendar
  • islamic - Islamic calendar
  • islamic-umalqura - Islamic Umm al-Qura calendar
  • islamic-tbla - Islamic tabular calendar
  • islamic-civil - Islamic civil calendar
  • islamic-rgsa - Islamic calendar (Saudi Arabia)
  • iso8601 - ISO 8601 calendar
  • japanese - Japanese calendar
  • persian - Persian calendar
  • roc - Republic of China calendar
// Examples with different calendar systems
function MultiCalendarDisplay() {
  const gregorianFormatter = useDateFormatter({
    calendar: "gregory",
    dateStyle: "long"
  });
  
  const islamicFormatter = useDateFormatter({
    calendar: "islamic",
    dateStyle: "long"
  });
  
  const buddhistFormatter = useDateFormatter({
    calendar: "buddhist", 
    dateStyle: "long"
  });
  
  const date = new Date();
  
  return (
    <div>
      <p>Gregorian: {gregorianFormatter.format(date)}</p>
      <p>Islamic: {islamicFormatter.format(date)}</p>  
      <p>Buddhist: {buddhistFormatter.format(date)}</p>
    </div>
  );
}

Install with Tessl CLI

npx tessl i tessl/npm-react-aria--i18n

docs

context-locale.md

date-time-formatting.md

index.md

number-list-formatting.md

server-support.md

string-localization.md

tile.json