Localizing Dates in a Perl Web App With JavaScript

Last week's article received a comment on a private Facebook group that amounted to "just use JavaScript's built-in formatting." So what would that look like?

Perl
 




x
38


 
1
#!/usr/bin/env perl
2

           
3
use Mojolicious::Lite -signatures;
4
use DateTime;
5

           
6
get '/' =>
7
    sub ($c) { $c->render( template => 'index', date => DateTime->today ) };
8

           
9
helper localize_date => sub ( $c, $date = DateTime->today, $style = 'full' ) {
10
    my $date_params = join ',' => $date->year, $date->month_0, $date->day;
11
    return
12
        qq<new Date($date_params).toLocaleString( [], {dateStyle: "$style"})>;
13
};
14

           
15
app->start;
16
__DATA__
17

           
18
@@ index.html.ep
19
% layout 'default';
20
% title 'Today';
21

           
22
<ul>
23
    <li><script>
24
        document.write(<%== localize_date $date %>)
25
    </script></li>
26
    % for my $style ( qw(long medium short) ) {
27
    <li><script>
28
        document.write(<%== localize_date $date, $style %>)
29
    </script></li>
30
    % }
31
</ul>
32

           
33
@@ layouts/default.html.ep
34
<!DOCTYPE html>
35
<html>
36
    <head><title><%= title %></title></head>
37
    <body><%= content %></body>
38
</html>


It's structured much like the Perl-only solution, with a default "/" route and a localize_date Mojolicious helper to do the formatting. I opted to output a piece of JavaScript from the helper on lines 11 through 14 since it could be repeated several times in a document. You could instead declare a function in the default layout's HTML <head> on line 38 that would receive a date and a formatting style, outputting the resulting formatted date.

Localizing Dates in A Perl Web Application

When we're writing software for a global audience, it's nice if we can provide it according to their native languages and conventions. Translating all of the text can be a huge undertaking, but we can start small by making sure that when we show the day and date it appears as the user expects. For example, to me it's Tuesday, April 20, 2021; to my friend Paul in the UK, it's Tuesday, 20 April 2021 (note the difference in order); and to my other friend Gabór in Israel, it's יום שלישי, 20 באפריל 2021 (note the different direction of the text, different language, and character set).

Thankfully, we have a number of tools to assist us:

  • The DateTime::Locale library, which enables our Perl software to represent dates and times globally and contains a catalog of locales. It works with the DateTime library for storing our dates as objects that can be easily manipulated and formatted.
  • The HTTP Accept-Language header, which lets a web browser communicate to the server what natural languages and locale variants the user understands.
  • The HTTP::AcceptLanguage module, which helps us parse the Accept-Language header and select a compatible locale.

Our sample code uses the Mojolicious framework and is very simple; almost half of it is just HTML web page templates. You could easily adapt it to other frameworks or templating systems.

Perl and XML in 2021: A Few Lessons Learned

It's been years since I've had to hack on anything XML-related, but a recent project at work has me once again jumping into the waters of generating, parsing, and modifying this 90s-era document format. Most developers these days likely only know of it as part of the curiously-named XMLHTTPRequest object in web browsers used to retrieve data in JSON format from servers, and as the "X" in AJAX. But here we are in 2021, and there are still plenty of APIs and documents using XML to get their work done.

In my particular case, the task is to update the API calls for a new version of Virtuozzo Automator. Its API is a bit unusual in that it doesn't use HTTP but rather relies on opening a TLS-encrypted socket to the server and exchanging documents delimited with a null character. The previous version of our code is in 1990s-sysadmin-style Perl, with manual ing of objects and parsing the XML using regular expressions. I've decided to update it to use the Moo object system and a proper XML parser. But which parser and module to use?

One Challenge With 10 Solutions

Technologies we use for Data Analytics have evolved a lot, recently. Good old relational database systems become less popular every day. Now, we have to find our way through several new technologies, which can handle big (and streaming) data, preferably on distributed environments.

Python is all the rage now, but of course there are lots of alternatives as well. SQL will always shine, and some other oldies-but-goldies, which we can never under-estimate, are still out there.

Real-Time Email Tracking With Go, Redis, and Lua

Real-time data provides a goldmine of information.

At Pepipost, we handle around 20k – 50k email events/second. That’s huge! Processing all these events every single second in real-time. We use the magical combination of Redis and PERL to deal with these complex data structures. And it seemed to work just fine, because of the high text processing efficiency, powerful set of regular expressions, fast development, and easy-to-learn functionality.