如何在Perl中格式化特定于语言环境的数字?

时间:2021-04-27 07:27:42

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:

我需要根据用户选择的语言格式化我的Web应用程序中的数字,例如1234.56 =“1.234,56”德文像sprintf这样的东西目前是不可能的,因为它们依赖于LC_NUMERIC(这对于桌面应用程序IMHO来说是明智的)而且我必须在服务器上生成每个语言环境,这是不行的。我更喜欢使用CLDR的格式化字符串,但没有找到合适的模块。我想要的是一个简单的说法:

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

How does one do that properly?

如何正确地做到这一点?

3 个解决方案

#1


The CPAN now has CLDR::Number for Unicode CLDR-based number, percent, and currency formatting.

CPAN现在具有基于Unicode CLDR的CLDR :: Number数字,百分比和货币格式。

use CLDR::Number;
my $cldr = CLDR::Number->new(locale => 'de-DE');  # or 'de_DE'

my $decf = $cldr->decimal_formatter;
say $decf->format(1234.5);  # '1.234,5'

my $curf = $cldr->currency_formatter(currency_code => 'EUR');
say $curf->format(1234.5);  # '1.234,50 €'

$curf->locale('de-AT');     # Austrian German
say $curf->format(1234.5);  # '€ 1.234,50'

CLDR::Number provides all the locale data that it uses, currently from the CDLR v27, so you don't have to rely on inconsistent operating system locale data.

CLDR :: Number提供它使用的所有语言环境数据,目前来自CDLR v27,因此您不必依赖于不一致的操作系统语言环境数据。

#2


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.

但是,这取决于现有的语言环境。查看Math :: Currency的文档,了解如何为您需要的所有数据生成Math :: Currency :: XX子模块,然后在服务器上安装这些子模块。然后不需要语言环境。

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

我也有一个补丁,可以应付各种各样的欧元。 (现在,如果只有作者会应用它;)

Jess.

#3


perldoc perllocale states:

perldoc perllocale说:

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

setlocale函数您可以使用POSIX :: setlocale()函数在运行时随意切换区域设置:

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

它还注意到模块I18N :: Langinfo,它逐个提供本地化数据。

#1


The CPAN now has CLDR::Number for Unicode CLDR-based number, percent, and currency formatting.

CPAN现在具有基于Unicode CLDR的CLDR :: Number数字,百分比和货币格式。

use CLDR::Number;
my $cldr = CLDR::Number->new(locale => 'de-DE');  # or 'de_DE'

my $decf = $cldr->decimal_formatter;
say $decf->format(1234.5);  # '1.234,5'

my $curf = $cldr->currency_formatter(currency_code => 'EUR');
say $curf->format(1234.5);  # '1.234,50 €'

$curf->locale('de-AT');     # Austrian German
say $curf->format(1234.5);  # '€ 1.234,50'

CLDR::Number provides all the locale data that it uses, currently from the CDLR v27, so you don't have to rely on inconsistent operating system locale data.

CLDR :: Number提供它使用的所有语言环境数据,目前来自CDLR v27,因此您不必依赖于不一致的操作系统语言环境数据。

#2


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.

但是,这取决于现有的语言环境。查看Math :: Currency的文档,了解如何为您需要的所有数据生成Math :: Currency :: XX子模块,然后在服务器上安装这些子模块。然后不需要语言环境。

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

我也有一个补丁,可以应付各种各样的欧元。 (现在,如果只有作者会应用它;)

Jess.

#3


perldoc perllocale states:

perldoc perllocale说:

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

setlocale函数您可以使用POSIX :: setlocale()函数在运行时随意切换区域设置:

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

它还注意到模块I18N :: Langinfo,它逐个提供本地化数据。