views:

241

answers:

2

I need to format numbers in my web application depending on user's chosen language, e.g. 1234.56 = "1.234,56" in German. Stuff like sprintf is currently out of question, since they depend on LC_NUMERIC (which is sensible for desktop applications IMHO) and I'd have to generate every locale on the server, which is a no-go. I would prefer using CLDR's formatting strings, but haven't found an appropriate module. What I'd like to have is a nutshell:

set_locale("de_DE");
print format_number(1234.56);

How does one do that properly?

+1  A: 

perldoc perllocale states:

The setlocale function You can switch locales as often as you wish at run time with the POSIX::setlocale() function:

It also notes the module I18N::Langinfo, which provides localization data piece by piece.

Anonymous
Still still requires locales to be generated on the server :(
rassie
Ah, I missed that for that module. Well, Locale::Object comes with a database "collated from several sources and provided in an accompanying DBD::SQLite database".
Anonymous
+2  A: 
use POSIX qw( locale_h );
use Math::Currency;
set_locale(LC_ALL, "de_DE");
Math::Currency->localize();
my $eur = Math::Currency->new("1234.56");

print "$eur";

That does, however, depend on the locales existing. Look at Math::Currency's docs for how to generate Math::Currency::XX submodules for all the data you need first, then install those on the server.. no locales needed then.

I also have a patched one somewhere that copes with various sorts of EUR. (Now if only the author would apply it ;)

Jess.

castaway