I would like to hava a localized date string e.g. in buglist.cgi.
I can modify format string in the template, but I still get the English
names for the months and day of the week strings.
My idea was to hook the following function in global.pl
(after http://bugzilla.mozilla.org/show_bug.cgi?id=bz-l10n patch
had been applied)
---------------------------------
use strict;
use Date::Format;
use Date::Language;
# See man Date::Format for details.
# Unfortunally RFC language tags are not accepted :-(
sub getDate {
my $lang = $_[0] || "en";
my $language = "English";
if ( $lang =~ m/de-at.*/i) { $language = "Austrian"; }
elsif ( $lang =~ m/cz$|cz-.*/i) { $language = "Czech"; }
elsif ( $lang =~ m/nl$|nl-.*/i) { $language = "Dutch"; }
elsif ( $lang =~ m/fr$|fr-.*/i) { $language = "French"; }
elsif ( $lang =~ m/de|de-.*/i) { $language = "German"; }
elsif ( $lang =~ m/it|it-.*/i) { $language = "Italian"; }
elsif ( $lang =~ m/no|no-.*/i) { $language = "Norwegian"; }
return Date::Language->new($language);
}
print getDate("cz")->time2str("%a %b %e %T %Y\n",time);
---------------------------------
but this is really (TM) ugly :-(
Has someone a better idea?
Tobias
Yes. Wrap it in a function.
sub timeToStr {
return getDate("cz")->time2str(@_);
}
We should probably cache the date object, as well.
Except we need a global mapping table of languages to date types, so we know that the .cx localisation actually uses Austrian-formatted dates. Or something.
Can we sit on this problem till I get back? I might get inspired :-)
Gerv
Gervase Markham wrote:
> Yes. Wrap it in a function.
> sub timeToStr {
> return getDate("cz")->time2str(@_);
> }
Well rather:
sub timeToStr {
my $locale = shift;
return getDate($locale)->time2str(@_);
}
A completely different approach is:
-----------------------
setlocale(LC_ALL,$language);
POSIX::strftime("%A %B %e %T %Z %Y",gmtime());
-----------------------
The problem is how to convert e.g. "de-li-mylang" correctly.
- de_LI doesn't exist (on my Linux computer)
- de doesn't exists either.
My idea was something like
-------------------------
use POSIX;
$language = "de-li-my-own";
# First try: "de_LI"
$language =~ s/-(\w*).*/_\U$1\E/;
if (! setlocale(LC_ALL,$language)) {
# Second try: "de"
$language =~ s/_.*//;
if (! setlocale(LC_ALL,$language)) {
#last try: "de_DE"
$language = $language."_\U$language\E";
setlocale(LC_ALL,$language);
}
}
print POSIX::strftime("%A %B %e %T %Z %Y",gmtime())."\n";
----------------------------
but the $language."_\U$language\E"; is really a hack.
This hack works ok for those languages:
bg, de, es, fi, fo, fr, hr, hu, id, is, it, lt, kv, mk, mt, nl, no, pl,
pt, ro, ru, sk, th, tr, uz.
But it fails with:
af, ar, be, br, bs, ca, cs, cy, da, el, en, et, eu, fa, ga, gl, gv, he,
hi, iw, ja, ka, kl, ko, kw, mi, mr, ms, nb, nn, oc, sh, sl, sq, sr, sv,
ta, te, tg, tl, tr, uk, ur, vi, yi, zh.
A real advantage is that the number of supported languages is much
higher than with time2str. If one adds an additional translation table
of the form:
ja->ja_JP, af -> af_ZA etc. this works even better.
Tobias
I don't really understand this :-) But never mind. If you produce a patch and explain it, I'll look at it :-)
Gerv