Localized Month Name?

How to modify this action Localized Day Name | Drafts Action Directory to return localized month names instead of days?

perhaps expand this action to return both localized day and month name?

I would take a different approach. That action requires editing to specify an array of potential values each time, but these should be known to the system in any case. Referencing those minimises the maintenance and risk of mis-keying entries.

Here’s a few lines of JavaScript to illustrate the approach I would take.

const LOCALE_ID = "de-DE";
let dt = new Date();
alert(dt.toLocaleDateString(LOCALE_ID, { weekday: 'long' }));
alert(dt.toLocaleDateString(LOCALE_ID, { month: 'long' }));

The first line specifies what language/location we want to use. You can specify this using a standard identifier. Above I’ve chosen Germany as the example.

  • Local Codes List - this list I think has the most commonly available locales listed.

The second line creates a new date object for the current date.

The third and fourth lines display a word via an alert box. They convert the current date to a day of the week, or a month, respectively; specified in the language associated with the locale.

In the example action it is creating a template tag, therefore the following example would generate such tags, but with the day of the week (tag = [[day]]) and the month (tag = [[month]]) being specified in Greek.

const LOCALE_ID = "el-GR";
let dt = new Date();
draft.setTemplateTag("day", dt.toLocaleDateString(LOCALE_ID, { weekday: 'long' }));
draft.setTemplateTag("month", dt.toLocaleDateString(LOCALE_ID, { month: 'long' }));

You can temporarily add a quick check line at the end too to ensure that it is working as expected. The following line will process the day and month tags and display the result in an alert.

alert(draft.processTemplate("[[day]] [[month]]"));

While this doesn’t modify the original action as per your request, I think it should be a better overall solution.

Hope that helps.

2 Likes

Fantastic! If @sylumer think he has a better approach, I believe him. Thank you again for your quick answer and helpful descriptions step-by-step guidance.

One little thing @sylumer, how to capitalise both day and month name? :blush:

I guess there are two ways to do it?

  • Define it on top in script itself
  • In the end just capitalise result

They should come through as capitalised (first character only in upper case) as they are proper nouns. If you mean all caps (upper case), then you can upper case any string with toUpperCase().

Amending the line to setup the month tag for example could be done like this.

draft.setTemplateTag("month", dt.toLocaleDateString(LOCALE_ID, { month: 'long' }).toUpperCase());