Skip to content

i18n: allow to provide DateTimeFormat calendar #5440

Closed

Description

@slorber

🐛 Bug Report

    new Intl.DateTimeFormat("fa", {
      day: 'numeric',
      month: 'numeric',
      year: 'numeric',
      timeZone: 'UTC',
    }).format(new Date()).replace(/[۰-۹]/g, (chr) => {
      const persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
      return persian.indexOf(chr)
    })

This code does not use the same calendar on Node.js (Gregorian) and browsers (Solar Hijri), leading to a different date being rendered in SSR and after React hydration.

We should provide an i18n locale option to force a given calendar and make sure the formatted date is consistent between SSR/client.

    new Intl.DateTimeFormat("fa", {
      day: 'numeric',
      month: 'numeric',
      year: 'numeric',
      timeZone: 'UTC',
      calendar: "gregory"
    }).format(new Date()).replace(/[۰-۹]/g, (chr) => {
      const persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']
      return persian.indexOf(chr)
    })

Issue initially reported here: #5410 (comment)

Activity

  1. added
    bug
    status: needs triage
    on Aug 30, 2021
  2. removed
    status: needs triage
    on Oct 30, 2021
  3. added
    difficulty: starter
    on Dec 15, 2021
  4. Josh-Cena commented on Jan 21, 2022

    @Josh-Cena
    Collaborator

    @slorber I've justed worked a bit on this. While it's useful to add another calendar config option, I wonder why would React hydration ever lead to a different result? Isn't the date always formatted on the server-side?

  5. slorber commented on Jan 21, 2022

    @slorber
    CollaboratorAuthor

    hmm yes it may not be a SSR problem in the end, can't be sure anymore 😅

    It could also be related to different Node.js versions using different calendars for the same locale.

    Maybe this is not an issue anymore since NodeJS has better Intl support, but it might still be useful to provide a calendar as an option 🤷‍♂️ we have seen some users don't like to use BCP47 codes for locale keys as it leads to complicated FS paths

  6. added
    domain: i18n
    on Mar 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdifficulty: starterdomain: i18n

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions