4
votes

How can use non-Gregorian dates and calendars?

I adjust Drupal setting to my native language (fa) and native location (Asia/Theran) but month name and number of years also in Gregorian yet (however there are translated to my native language-persian).

I already use format_date() or date() in theme files, but this function is not locale-aware, which means that it always outputs English language data, such as day and month names and ordinal number suffixes.

I want to show the month names in the native language (e.g. Chinese, Islamic, Hebrew, Farsi/Persian) and use the national calendar (e.g. Irani, Shamsi, Jalali).

I want to understand is there any module or hardcoding solution in Drupal to show dates and calendars in other non-Gregorian calendars.

Any suggestions?

  • 1
    Have you tried passing in a $langcode? What are you system.date settings in configuration? – mradcliffe Mar 20 '16 at 19:35
  • 3
    format_date is deprecated anyway, don't use it - use \Drupal::service('date.formatter')->format() instead. That's a service, which you can replace, so nothing stopping you from 'replacing' this with your own or a different class. You'll need to make sure it stays compatible with the rest of Drupal, of course – Clive Mar 22 '16 at 15:23
  • This change record may be helpful: drupal.org/node/1834108 – mradcliffe Mar 24 '16 at 15:07
8
votes

I'm hoping that someone else can Optimize this solution and make it better.
I'm ready to receive Any suggestion or criticism or any Other solution for this Challenge.

Features:

  • Does not requires any hacking or patching core /frontend solution ,display when dates are rendered

  • Does not change any date in database

  • Compatible and tested with PHP versions 5.3 and hereinafter

  • Supports Iranian/Persian/Shamsi/Jalali , Arabic/Hijri/Islamic, Thai, Hebrew... Calendar systems

  • Better Performance becuse Doesn't outbreak the drupal core

Method 1, Using "IntelDateFormatter" Class:

First of all you must be sure that version of your php that installed is Newer than 5.3 and 'IntlDateFormatter' Extension was exist.
For use Traditional(National/Native) Date/Calender in Drupal 8 in nodes and Posted Comments with use of IntlDateFormatter class, add below codes to "THEME_NAME.theme":

For Nodes:

function THEME_NAME_preprocess_node(&$variables) {
    $locale = "fa_IR@calendar=persian";
    $dateType = IntlDateFormatter::NONE;//type of date formatting.
    $timeType = IntlDateFormatter::NONE;//if time format setting to none, will give you date itself
    $timezone = 'Asia/Tehran';
    $calendar = IntlDateFormatter::TRADITIONAL;//Non-Gregorian Calendar
    $pattern = 'y/M/d';

    $fmt = new IntlDateFormatter($locale, $dateType, $timeType, $timezone, $calendar, $pattern);
    $date = $variables['node']->getCreatedTime();    
    $variables['date'] = $fmt->format($date);
}

For Comments:

function THEME_NAME_preprocess_comment(&$variables) {
    $fmt = new IntlDateFormatter(
    "fa_IR@calendar=persian",
    IntlDateFormatter::NONE,
    IntlDateFormatter::NONE,
    'Asia/Tehran',
    IntlDateFormatter::TRADITIONAL,
    'y/M/d'
    );

    // Getting the node creation time stamp from the comment object.
    $date = $variables['comment']->getCreatedTime();
    $variables['created'] = $fmt->format($date);
}

Information about "IntlDateFormatter" Parameters:
locale:
Locale to use when formatting or parsing or NULL to use the value specified in the ini setting, Link:http://www.w3.org/International/articles/language-tags

datetype:
Date type to use (NONE, FULL, LONG, MEDIUM, SHORT). This is one of the IntlDateFormatter constants.

timetype:
Time type to use (NONE, FULL, LONG, MEDIUM, SHORT). This is one of the IntlDateFormatter constants.

timezone:
Time zone ID, default is system default,
Link:http://www.php.net/manual/en/timezones.php.

calendar:
Calendar to use for formatting or parsing; default is Gregorian. This is one of the IntlDateFormatter calendar constants.
The calendars ICU allows you to play with include:

  • Japanese (@calendar=japanese)
  • Buddhist (@calendar=buddhist)
  • Chinese (@calendar=chinese)
  • Persian (@calendar=persian)
  • Indian (@calendar=indian)
  • Islamic (@calendar=islamic)
  • Hebrew (@calendar=hebrew)
  • Coptic (@calendar=coptic)
  • Ethiopic (@calendar=ethiopic)

Link:http://userguide.icu-project.org/datetime/calendar

pattern:
Optional pattern to use when formatting or parsing. Possible patterns are documented at: http://userguide.icu-project.org/formatparse/datetime


METHOD 2,Using External Functions (like "jdf" for persian language):

First download this function form: http://jdf.scr.ir/download/
and then put downloaded file to your theme folder and add this code to above of "Theme_Name.theme"

include ('jdf.php');

and then add below code to appropriate place:

function Theme_Name_preprocess_node(&$variables) {
   $date = $variables['node']->getCreatedTime();
   $variables['date'] = jdate("l j F Y", strtotime($date));
}

I'm hoping that someone else can build on this and make it better.

2
votes

Taarikh module will help you use the Islamic calendar as a field formatter and widget for display with the ability to translate month names, I used it and works very well, it's the best for Islamic calendar.

You could patch the module to add support for other calendars "i could help"

  • github.com/mgomma/taarikh – Mohammed Gomma Jul 13 '16 at 13:13
  • Sure you could patch the module and push your code, I could add you as a contributor on github and work with you on that issue, By the way the module maintainer have another approach to add any calendar system but not working yet and I contacted him to contribute and publish it, let me know your mail address. – Mohammed Gomma Jul 13 '16 at 18:40
1
vote
+150

You can use this Modules:

Calendar Systems

Module page: Calendar Systems
Support: Iranian (jalali) , Arabic/Islamic/Hijri , Thai and Gregorian (Using PHP's built-in)
Version: Drupal 7
BY: Sina Salek
Description:

Advances Drupal's already awesome localization support one more big step ahead by Adding support for different calendar systems like Iranian , Jalali , Hijir , Thai , Hebrew etc. This support is currently limited to display and data entry for date fields and the back-end date is always Gregorian (Timestamp).


Datex

Module page: Datex
Support: only Iranian (Hijri Shamsi/ jalali)
Version: Drupal 7
By: Drupalion
Description:

Datex and related module will help to create a better multilingual website for languages needing a Jalali (Or in near feature of datex, any) calendar instead of default Gregorian calendar.


Hijri

Module page: Hijri
Support: Only Hijri Date (Islamic, Hijri Ghamari)
Version: Drupal 7, Drupal 8 (Development releases)
By: Abdullah Bamelhes
Description:

This is a smart module that basically extends Drupal's display date to provide Hijri Date integration with Drupal core date field and with other Drupal contributions.

This module is integrated very well with Views module


Taarikh

Module page:Taarikh
Support: Only Hijri Date (Islamic, Hijri Ghamari)
Version: Drupal 7, Drupal 8 (Development releases)
By: Husain Inad
Description:

Taarikh is a field formatter and widget module to display and input hijri dates using the fields defined by the date module. The formatter can work with all the fields defined by the date module, i.e., Date, Date (ISO format) and Date (Unix timestamp). The widget, however, does not work with Date (Unix timestamp).

  • The question is about drupal 8 and you suggest drupal 7 modules !!! – Yuseferi Oct 8 '16 at 11:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.