How to generate the date in different languages with TextExpander

Text expansion is a great way to save time, especially with repetitive tasks like typing the date, an email address, etc.

If you don’t know what text expansion is, you can find a quick explanation at the beginning of this article:

How to generate a random number with TextExpander

I work in both (American) English and French. The problem is that sometime, some snippets should give language specific outputs. For instance, date is not written in the same fashion in French and in English. For simple dates, TextExpander can output the day and the month in different order: 16/06/2018 in French, 06/16/2018 in English. But when it comes to output the month in plain text, it’s another story.

Until now, I had a pretty unfriendly snippet: my Mac is set to American English, so when I wanted to output the date with TextExpander, the month was always outputted in English. To overcome this, I created a snippet for French dates, where I had to manually choose the month in a predefined list. Not really user friendly, even if it did the job.

While researching how to generate random numbers in TextExpander, I found this page about dates in JavaScript. And I had an idea.

After a few minutes of testing, I created this JavaScript snippet (abbreviation is dus):

1
2
3
var event = new Date();
var options = { year: 'numeric', month: 'long', day: 'numeric' };
event.toLocaleDateString('en-US', options);

And I created another one (abbreviation is dfr):

1
2
3
var event = new Date();
var options = { year: 'numeric', month: 'long', day: 'numeric' };
event.toLocaleDateString('fr-FR', options);

Can you spot the difference?

I put those two snippets in a folder “Dates”, with d as a prefix 1Folder prefixes apply to all the snippets in a folder. For instance, the “real” abbreviation of “dus” is “us”. But as “dus” is in the folder “Dates” sets with the prefix “d”, “d” is automatically added at the beginning of “us”. Which leads to “dus”..

dus outputs June 16, 2018, while dfr outputs 16 juin 2018. Now, months are properly formatted: the langage, items order and capitals are all good. And I don’t need to manually choose the month anymore.

The really neat thing is that I only had to change the langage tag to completely alter the formatting. The difference between the two snippets is just a few letters in line 3: en-US was replaced by fr-FR.

In fact, en-US can be replaced by any BCP 47 language tag. I used fr-FR for the French date, but it is possible to set virtually any langage you want. You can even output in non-Latin alphabets!

If you want to further tweak the JavaScript snippet, you can find additional details here. For instance, you can output the day name in plain text, the date without 20, and so on. This small JavaScript utility is actually very flexible.

In the past, I used a shell snippet to expand language-specific dates. It worked on macOS, but shell snippets don’t work on Windows and on iOS. JavaScript snippets do, which is a really nice addition. That being said, it should be clearly understood that this article is NOT an endorsement of TextExpander. It is still an (insanely) overpriced utility with not-so-reliable sync.

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of