Converter to change gregorian date to jalali on view layer of discourse

Thank you :slight_smile:

I have a question about making plugins for discourse.
Iran calendar is Solar hijri or jalali, I want to make a converter to change gregorian date to jalali on view layer of discourse (without changing database).
wordpress has a plugin that convert all dates on website to jalali.
I want to do exact same thing and globally override the date method on view layer and display jalali date if forum language is persian.
How can I do this without changing discourse core codes and database ?

2 „Gefällt mir“

Any help ? :worried:

1 „Gefällt mir“

I’ve looked at a few sites here that I thought might be using different calendars.

A Chinese site is using English and Gregorian dates.
Hebrew sites look to be using Hebrew month names with Gregorian years.

I’m guessing this hasn’t come up before because much of the world uses Gregorian / countries with alternate calendar systems also understand Gregorian?

I know the database has a great many timestamp fields, I wouldn’t mess with those.
Sorry, but I am completely ignorant about converting one calendar to another.
Do you know of any Gems that do this?

1 „Gefällt mir“

Thank you for your response.

You are right, a lot of countries use gregorian dates. But most people (I guess +99%) of people in Iran use jalali calendar.
I found this gem called parsi date but I’m not familiar with ruby language and I don’t know how to use it.
as I said before, I don’t want to change discourse’s core or database. just changing the dates on view layer would be great.

ps : jalali date is not just about month names. years and month length are different too.
for example :
29
Apr
2017

to jalali :
09
اردیبهشت ( ordibehesht )
1396

Maybe you can do that with a simple js script JalaliJSCalendar

6 „Gefällt mir“

are you looking for a date-convertor or do you want to know how to change the dates in discourse in a plugin without knowing ruby?

if you want the second one, a simple-stupid way is to change it in the front-end side using js, with the data in json pages. e.g. check this page:

https://meta.discourse.org/t/converter-to-change-gregorian-date-to-jalali-on-view-layer-of-discourse/61671.json

all the post information exists there! if you search for “2017” you’ll find some dates there, and you can then use some third party calendars and change between the two.

so far we’ve used this simple way a lot. it’ll work for short-time until you learn ruby+discourse!

4 „Gefällt mir“

@Trash Thank you, I know how to convert dates to jalali. I did this in PHP before. I just don’t know how to do it in Ruby on Rails Project.

@Pad_Pors Thanks, I want to do the second one.
Which .rb file generates this json file ?
Converter to change gregorian date to jalali on view layer of discourse

1 „Gefällt mir“

you don’t need to generate the json files, it already exists and you only need to use it.

@Alavi1412 can help you better about this.

1 „Gefällt mir“

@Pad_Pors
I’m not trying to generate json file. I want to change date before sending to client.

2 „Gefällt mir“

So not try to change the source code.
You just need a javascript to select the date element at your page and change it and add it to your page.
I think Topic controller is making this for you but for changing this you need to know some Ruby.
I suggest you to create a very simple plugin and change the date by selecting elements completely in javascript

Hi
Is it possible for anyone to write a date conversion plugin written by Jalali?
We need such a plugin.
If anyone can guide, thank you.

Hallo,

Das ist tatsächlich ein unkomplizierter Vorgang.


Schritt-für-Schritt: Shamsi-Daten im Discourse-Frontend aktivieren

1) Eine Theme-Komponente erstellen

  1. Gehen Sie zu Admin → Anpassen → Themes
  2. Klicken Sie auf Komponenten
  3. Klicken Sie auf Hinzufügen → Neu erstellen
  4. Benennen Sie sie: Shamsi-Datums-Konverter (Oder was auch immer Sie möchten)

2) Das Skript hinzufügen (Kopfbereich)

Innerhalb der Komponente:

  1. Öffnen Sie Allgemein → Kopf
  2. Fügen Sie alles unten ein
  3. Speichern

Code:

<script>
(function () {
  // Nur anwenden, wenn die Seitensprache Persisch ist
  if (!document.documentElement.lang.startsWith("fa")) return;

  const formatter = new Intl.DateTimeFormat(
    "fa-IR-u-ca-persian",
    { dateStyle: "medium" }
  );

  function toDate(value) {
    if (!value) return null;

    // Epoche (Sekunden oder ms)
    if (/^\d{10,13}$/.test(value)) {
      const n = Number(value);
      return new Date(value.length === 10 ? n * 1000 : n);
    }

    const d = new Date(value);
    return isNaN(d) ? null : d;
  }

  function process(el) {
    if (el.dataset.shamsi === "1") return;

    const date =
      toDate(el.getAttribute("datetime")) ||
      toDate(el.dataset.time) ||
      toDate(el.getAttribute("title"));

    if (!date) return;

    el.textContent = formatter.format(date);
    el.dataset.shamsi = "1";
  }

  function run(root = document) {
    root.querySelectorAll("time, .relative-date").forEach(process);
  }

  // Erstes Laden
  run();

  // Unendliches Scrollen / Live-Updates behandeln
  new MutationObserver(muts => {
    muts.forEach(m => m.addedNodes.forEach(n => {
      if (n.nodeType === 1) run(n);
    }));
  }).observe(document.documentElement, { childList: true, subtree: true });
})();
</script>
___

Alternativ können Sie das, was ich exportiert habe, hochladen und es Ihren Themes zuweisen.

discourse-shamsi-date.zip (1.1 KB)


Funktionsweise

Discourse sendet bereits normale gregorianische Daten an Ihren Browser; dieses Skript ändert die Daten nicht, sondern ersetzt nur, wie der Datumstext auf der Seite angezeigt wird, indem es ihn mithilfe des Browsers selbst in Shamsi (Jalali) umwandelt.

Was das Skript tut (auf hoher Ebene)

  1. Findet diese <td>-Elemente
  2. Liest das tatsächliche gregorianische Datum aus datetime
  3. Wandelt es in Shamsi um
  4. Ersetzt nur den sichtbaren Text
  5. Wiederholt dies, wann immer neue Beiträge geladen werden

Das ist alles. Nichts weiter.


Dies führt zu Daten wie diesen:

Sie können es online auf unserer Instanz ansehen: https://forums.7ho.st

Viel Erfolg

1 „Gefällt mir“