| 1 | # ============================================================================ | ||||||||||
| 2 | package CatalystX::I18N::Role::NumberFormat; | ||||||||||
| 3 | # ============================================================================ | ||||||||||
| 4 | |||||||||||
| 5 | 4 | ▶ | use namespace::autoclean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 4 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 4 | ▶ | use Number::Format; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 4 | ▶ | use CatalystX::I18N::TypeConstraints; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | |||||||||||
| 13 | 4 | ▶ | use POSIX qw(); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 4 | has 'i18n_numberformat' => ( | |||||||||
| 16 | is => 'rw', | ||||||||||
| 17 | isa => 'Number::Format', | ||||||||||
| 18 | lazy_build => 1, | ||||||||||
| 19 | builder => '_build_i18n_numberformat', | ||||||||||
| 20 | clearer => '_clear_i18n_numberformat', | ||||||||||
| 21 | ); | ||||||||||
| 22 | |||||||||||
| 23 | 3 | ▶ | sub _build_i18n_numberformat { | ||||||||
|
Subroutine
_build_i18n_numberformat: called CC=4 SCAR=13.9
| |||||||||||
| 24 | 3 | my ($c) = @_; | |||||||||
| 25 | |||||||||||
| 26 | 3 | my $locale = $c->locale.'.UTF-8'; | |||||||||
| 27 | 3 | my $config = $c->i18n_config; | |||||||||
| 28 | 3 | my $lconv = {}; | |||||||||
| 29 | |||||||||||
| 30 | # Get lconv from POSIX | ||||||||||
| 31 | # TODO: Get lconv setting only once at Catalyst startup | ||||||||||
| 32 | { | ||||||||||
| 33 | 4 | ▶ | use locale; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | 4 | ▶ | no strict 'refs'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 35 | 3 | my %original; | |||||||||
| 36 | |||||||||||
| 37 | # Set locale | ||||||||||
| 38 | 3 | foreach my $category (qw(LC_NUMERIC LC_MONETARY)) { | |||||||||
| 39 | 6 | my $current_locale = POSIX::setlocale(&{"POSIX::".uc($category)}); | |||||||||
| 40 | 6 | ▶ | if ($current_locale ne $locale) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 41 | 6 | $original{$category} = $current_locale; | |||||||||
| 42 | 6 | POSIX::setlocale(&{"POSIX::".uc($category)},$locale); | |||||||||
| 43 | } | ||||||||||
| 44 | } | ||||||||||
| 45 | |||||||||||
| 46 | 3 | $lconv = POSIX::localeconv(); | |||||||||
| 47 | |||||||||||
| 48 | # Reset locale to original state | ||||||||||
| 49 | 3 | ▶ | while (my ($category,$locale) = each %original) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 50 | 6 | POSIX::setlocale(&{"POSIX::".uc($category)},$locale); | |||||||||
| 51 | } | ||||||||||
| 52 | } | ||||||||||
| 53 | |||||||||||
| 54 | # Build custom defined for 5.8 | ||||||||||
| 55 | 60 | ▶ | my $defined_or = sub { | ||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||
| 56 | 60 | foreach (@_) { | |||||||||
| 57 | 105 | ▶ | return $_ | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | if defined $_; | ||||||||||
| 59 | } | ||||||||||
| 60 | 3 | }; | |||||||||
| 61 | |||||||||||
| 62 | # Set number format | ||||||||||
| 63 | my $numberformat = Number::Format->new( | ||||||||||
| 64 | -int_curr_symbol => $defined_or->($config->{int_curr_symbol},$lconv->{int_curr_symbol},'EUR'), | ||||||||||
| 65 | -currency_symbol => $defined_or->($config->{currency_symbol},$lconv->{currency_symbol},'€'), | ||||||||||
| 66 | -mon_decimal_point => $defined_or->($config->{mon_decimal_point},$lconv->{mon_decimal_point},'.'), | ||||||||||
| 67 | -mon_thousands_sep => $defined_or->($config->{mon_thousands_sep},$lconv->{mon_thousands_sep},','), | ||||||||||
| 68 | -mon_grouping => $defined_or->($config->{mon_grouping},$lconv->{mon_grouping}), | ||||||||||
| 69 | -positive_sign => $defined_or->($config->{positive_sign},$lconv->{positive_sign},''), | ||||||||||
| 70 | -negative_sign => $defined_or->($config->{negative_sign},$lconv->{negative_sign},'-'), | ||||||||||
| 71 | -int_frac_digits => $defined_or->($config->{int_frac_digits},$lconv->{int_frac_digits},2), | ||||||||||
| 72 | -frac_digits => $defined_or->($config->{frac_digits},$lconv->{frac_digits},2), | ||||||||||
| 73 | -p_cs_precedes => $defined_or->($config->{p_cs_precedes},$lconv->{p_cs_precedes},1), | ||||||||||
| 74 | -p_sep_by_space => $defined_or->($config->{p_sep_by_space},$lconv->{p_sep_by_space},1), | ||||||||||
| 75 | -n_cs_precedes => $defined_or->($config->{n_cs_precedes},$lconv->{n_cs_precedes},1), | ||||||||||
| 76 | -n_sep_by_space => $defined_or->($config->{n_sep_by_space},$lconv->{n_sep_by_space},1), | ||||||||||
| 77 | -p_sign_posn => $defined_or->($config->{p_sign_posn},$lconv->{p_sign_posn},1), | ||||||||||
| 78 | -n_sign_posn => $defined_or->($config->{n_sign_posn},$lconv->{n_sign_posn},1), | ||||||||||
| 79 | |||||||||||
| 80 | -thousands_sep => $defined_or->($config->{thousands_sep},$lconv->{thousands_sep},','), | ||||||||||
| 81 | -decimal_point => $defined_or->($config->{decimal_point},$lconv->{decimal_point},'.'), | ||||||||||
| 82 | # -grouping => ($config->{grouping},$lconv->{grouping}), | ||||||||||
| 83 | |||||||||||
| 84 | -decimal_fill => $defined_or->($config->{decimal_fill},0), | ||||||||||
| 85 | -neg_format => $defined_or->($config->{negative_sign},$lconv->{negative_sign},'-').'x', | ||||||||||
| 86 | 3 | -decimal_digits => $defined_or->($config->{frac_digits},$lconv->{frac_digits},2), | |||||||||
| 87 | ); | ||||||||||
| 88 | |||||||||||
| 89 | 3 | return $numberformat; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 34 | ▶ | after 'set_locale' => sub { | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 93 | 34 | my ($c,$locale) = @_; | |||||||||
| 94 | 34 | $c->_clear_i18n_numberformat(); | |||||||||
| 95 | 4 | }; | |||||||||
| 96 | |||||||||||
| 97 | 4 | ▶ | no Moose::Role; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 98 | 4 | 1; | |||||||||
| 99 | |||||||||||
| 100 | =encoding utf8 | ||||||||||
| 101 | |||||||||||
| 102 | =head1 NAME | ||||||||||
| 103 | |||||||||||
| 104 | CatalystX::I18N::Role::NumberFormat - Support for I18N number formating | ||||||||||
| 105 | |||||||||||
| 106 | =head1 SYNOPSIS | ||||||||||
| 107 | |||||||||||
| 108 | package MyApp::Catalyst; | ||||||||||
| 109 | |||||||||||
| 110 | use Catalyst qw/MyPlugins | ||||||||||
| 111 | CatalystX::I18N::Role::Base | ||||||||||
| 112 | CatalystX::I18N::Role::NumberFormat/; | ||||||||||
| 113 | |||||||||||
| 114 | package MyApp::Catalyst::Controller::Main; | ||||||||||
| 115 | |||||||||||
| 116 | use strict; | ||||||||||
| 117 | use warnings; | ||||||||||
| 118 | use parent qw/Catalyst::Controller/; | ||||||||||
| 119 | |||||||||||
| 120 | sub action : Local { | ||||||||||
| 121 | my ($self,$c) = @_; | ||||||||||
| 122 | |||||||||||
| 123 | $c->stash->{total} = $c->i18n_numberformat->format_price(102.34); | ||||||||||
| 124 | } | ||||||||||
| 125 | |||||||||||
| 126 | =head1 DESCRIPTION | ||||||||||
| 127 | |||||||||||
| 128 | This role add support for localised numbers to your Catalyst application. | ||||||||||
| 129 | |||||||||||
| 130 | All methods are lazy. This means that the values will be only calculated | ||||||||||
| 131 | upon the first call of the method. | ||||||||||
| 132 | |||||||||||
| 133 | =head1 METHODS | ||||||||||
| 134 | |||||||||||
| 135 | =head3 i18n_numberformat | ||||||||||
| 136 | |||||||||||
| 137 | my $number_format = $c->i18n_numberformat; | ||||||||||
| 138 | $number_format->format_price(27.03); | ||||||||||
| 139 | |||||||||||
| 140 | Returns a L<Number::Format> object for your current locale. | ||||||||||
| 141 | |||||||||||
| 142 | The L<Number::Format> settings will be taken from L<POSIX::localeconv> but | ||||||||||
| 143 | can be overdriven in your Catalyst I18N configuration: | ||||||||||
| 144 | |||||||||||
| 145 | # Add some I18N configuration | ||||||||||
| 146 | __PACKAGE__->config( | ||||||||||
| 147 | name => 'MyApp', | ||||||||||
| 148 | I18N => { | ||||||||||
| 149 | default_locale => 'de_AT', | ||||||||||
| 150 | locales => { | ||||||||||
| 151 | 'de_AT' => { | ||||||||||
| 152 | int_curr_symbol => 'EURO', | ||||||||||
| 153 | }, | ||||||||||
| 154 | } | ||||||||||
| 155 | }, | ||||||||||
| 156 | ); | ||||||||||
| 157 | |||||||||||
| 158 | Of course fetching the default locale settings via L<POSIX::localeconv> works | ||||||||||
| 159 | only if you have the requested locales installed. | ||||||||||
| 160 | |||||||||||
| 161 | The following configuration options are available (see L<Number::Format> for | ||||||||||
| 162 | detailed documentation): | ||||||||||
| 163 | |||||||||||
| 164 | =over | ||||||||||
| 165 | |||||||||||
| 166 | =item * int_curr_symbol | ||||||||||
| 167 | |||||||||||
| 168 | =item * currency_symbol | ||||||||||
| 169 | |||||||||||
| 170 | =item * mon_decimal_point | ||||||||||
| 171 | |||||||||||
| 172 | =item * mon_thousands_sep | ||||||||||
| 173 | |||||||||||
| 174 | =item * mon_grouping | ||||||||||
| 175 | |||||||||||
| 176 | =item * positive_sign | ||||||||||
| 177 | |||||||||||
| 178 | =item * negative_sign | ||||||||||
| 179 | |||||||||||
| 180 | =item * int_frac_digits | ||||||||||
| 181 | |||||||||||
| 182 | =item * frac_digits | ||||||||||
| 183 | |||||||||||
| 184 | =item * p_cs_precedes | ||||||||||
| 185 | |||||||||||
| 186 | =item * p_sep_by_space | ||||||||||
| 187 | |||||||||||
| 188 | =item * n_cs_precedes | ||||||||||
| 189 | |||||||||||
| 190 | =item * n_sep_by_space | ||||||||||
| 191 | |||||||||||
| 192 | =item * p_sign_posn | ||||||||||
| 193 | |||||||||||
| 194 | =item * n_sign_posn | ||||||||||
| 195 | |||||||||||
| 196 | =item * thousands_sep | ||||||||||
| 197 | |||||||||||
| 198 | =item * decimal_point | ||||||||||
| 199 | |||||||||||
| 200 | =item * decimal_fill | ||||||||||
| 201 | |||||||||||
| 202 | =item * neg_format | ||||||||||
| 203 | |||||||||||
| 204 | =item * decimal_digits | ||||||||||
| 205 | |||||||||||
| 206 | =back | ||||||||||
| 207 | |||||||||||
| 208 | =head1 SEE ALSO | ||||||||||
| 209 | |||||||||||
| 210 | L<Number::Format> | ||||||||||
| 211 | |||||||||||
| 212 | =head1 AUTHOR | ||||||||||
| 213 | |||||||||||
| 214 | Maroš Kollár | ||||||||||
| 215 | CPAN ID: MAROS | ||||||||||
| 216 | maros [at] k-1.com | ||||||||||
| 217 | |||||||||||
| 218 | L<http://www.k-1.com> | ||||||||||