| 1 | # ============================================================================ | ||||||||||||||||||||||||||||
| 2 | package CatalystX::I18N::Role::PosixLocale; | ||||||||||||||||||||||||||||
| 3 | # ============================================================================ | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | requires qw(set_locale); | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | use POSIX qw(); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 1 | our $ORIGINAL_LOCALE; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | after 'set_locale' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 14 | 0 | my ($c,$locale) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | my $category = $c->i18n_posix_category; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # Save original locale | ||||||||||||||||||||||||||||
| 19 | 0 | ▶ | $ORIGINAL_LOCALE ||= POSIX::setlocale($category); | ||||||||||||||||||||||||||
|
Condition0%0/3
$CatalystX::I18N::Role::PosixLocale::ORIGINAL_LOCALE ||= POSIX::setlocale($category)
MC/DC0%0/2
$CatalystX::I18N::Role::PosixLocale::ORIGINAL_LOCALE ||= POSIX::setlocale($category)
$CatalystX::I18N::Role::PosixLocale::ORIGINAL_LOCALE
POSIX::setlocale($category)
Truth table0%0/3
A ||= B
A= $CatalystX::I18N::Role::PosixLocale::ORIGINAL_LOCALEB= POSIX::setlocale($category)
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | # Set locale | ||||||||||||||||||||||||||||
| 22 | 0 | my $set_locale = $locale.'.UTF-8'; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 0 | my $set_locale_result = POSIX::setlocale( $category, $set_locale); | |||||||||||||||||||||||||||
| 25 | 0 | ▶ | unless (defined $set_locale_result | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $set_locale_result and $set_locale eq $set_locale_result
MC/DC0%0/2
defined $set_locale_result and $set_locale eq $set_locale_result
defined $set_locale_result
$set_locale eq $set_locale_result
Truth table0%0/3
A and B
A= defined $set_locale_resultB= $set_locale eq $set_locale_result
| |||||||||||||||||||||||||||||
| 26 | && $set_locale eq $set_locale_result) { | ||||||||||||||||||||||||||||
| 27 | 0 | $set_locale_result = POSIX::setlocale( $category, $locale); | |||||||||||||||||||||||||||
| 28 | 0 | ▶ | unless (defined $set_locale_result) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 0 | ▶ | $c->log->warn(sprintf("Could not setlocale '%s' or '%s' (do you have this locale installed?)",$set_locale,$locale)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | if $c->debug; | ||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | 1 | }; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | sub i18n_posix_category { | ||||||||||||||||||||||||||
|
Subroutine
i18n_posix_category: not called CC=4 SCAR=25.8
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 0 | my ($c,$category_string) = @_; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | $category_string ||= $c->config->{I18N}{posix_category}; | ||||||||||||||||||||||||||
|
Condition0%0/3
$category_string ||= $c->config->{"I18N"}{"posix_category"}
MC/DC0%0/2
$category_string ||= $c->config->{"I18N"}{"posix_category"}
$category_string
$c->config->{"I18N"}{"posix_category"}
Truth table0%0/3
A ||= B
A= $category_stringB= $c->config->{"I18N"}{"posix_category"}
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $category_string ||= 'LC_ALL'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$category_string ||= "LC_ALL"
MC/DC0%0/1
$category_string ||= "LC_ALL"
$category_string
Truth table0%0/2
A
A= $category_string
| |||||||||||||||||||||||||||||
| 40 | 0 | $category_string = uc($category_string); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | return 0 | ||||||||||||||||||||||||||||
| 43 | 0 | ▶ | unless grep { $category_string eq $_ } qw(LC_ALL LC_COLLATE LC_MONETARY LC_NUMERIC); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 0 | return &{"POSIX::".uc($category_string)}; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | after finalize => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 50 | 0 | my ($c) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | my $category = $c->i18n_posix_category; | |||||||||||||||||||||||||||
| 53 | # Restore original locale | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | POSIX::setlocale( $category, $ORIGINAL_LOCALE ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | if defined $ORIGINAL_LOCALE; | ||||||||||||||||||||||||||||
| 56 | 1 | }; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 1 | 1; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =encoding utf8 | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head1 NAME | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | CatalystX::I18N::Role::PosixLocale - Sets the POSIX locale | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | # In your catalyst base class | ||||||||||||||||||||||||||||
| 69 | package MyApp::Catalyst; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | use Catalyst qw/MyPlugins | ||||||||||||||||||||||||||||
| 72 | CatalystX::I18N::Role::Base | ||||||||||||||||||||||||||||
| 73 | CatalystX::I18N::Role::PosixLocale/; | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | sub action : Local { | ||||||||||||||||||||||||||||
| 76 | my ($self,$c) = @_; | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | $c->locale('sk_SK') | ||||||||||||||||||||||||||||
| 79 | # POSIX LC_COLLATE locale is 'sk_SK.UTF-8' now | ||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | This role sets the POSIX locales for each request. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head1 METHODS | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head3 i18n_posix_category | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | Helper method that returns the value of the requested POSIX locale category | ||||||||||||||||||||||||||||
| 91 | (LC_ALL, LC_COLLATE, LC_NUMERIC, LC_MONETARY). The POSIX category that should | ||||||||||||||||||||||||||||
| 92 | be used can be set in the I18N config. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | __PACKAGE__->config( | ||||||||||||||||||||||||||||
| 95 | I18N => { | ||||||||||||||||||||||||||||
| 96 | posix_category => 'LC_COLLATE', | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Default is LC_ALL | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | =head1 CAVEATS | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | POSIX locale is set for the whole process, and might affect other modules | ||||||||||||||||||||||||||||
| 105 | outside of Catalyst' scope. This role is also known to cause problems if | ||||||||||||||||||||||||||||
| 106 | used in conjunction with the L<CatalystX::I18N::TraitFor::ViewTT> role | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | L<POSIX>, L<perllocale> | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | Maroš Kollár | ||||||||||||||||||||||||||||
| 115 | CPAN ID: MAROS | ||||||||||||||||||||||||||||
| 116 | maros [at] k-1.com | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | L<http://www.k-1.com> | ||||||||||||||||||||||||||||