MooseX::Types::Locale::Codes - Locale::Codes related type constraints and coercions for Moose
This document describes MooseX::Types::Locale::Codes version 0.00
.
{
package MyApp::Locale;
use Moose;
use MooseX::Types::Locale::Codes::Country qw(CountryCode);
use MooseX::Types::Locale::Codes::Currency qw(CurrencyCode);
use MooseX::Types::Locale::Codes::Language qw(LanguageCode);
use MooseX::Types::Locale::Codes::Script qw(ScriptCode);
has 'country' => (isa => CountryCode[], coerce => 1, is => 'ro');
has 'currency' => (isa => CurrencyCode[], coerce => 1 is => 'ro');
has 'language' => (isa => LanguageCode[], coerce => 1, is => 'ro');
has 'script' => (isa => ScriptCode[], coerce => 1, is => 'ro');
# See each modules for further type constraints and coercions.
__PACKAGE__->meta->make_immutable;
}
my $locale = MyApp::Locale->new(
country => 'us',
currency => 'usd',
language => 'EN',
script => 'latn',
);
print $locale->country; # 'US' (letter case was canonized)
print $locale->currency; # 'USD' (letter case was canonized)
print $locale->language; # 'en' (letter case was canonized)
print $locale->script; # 'Latn' (letter case was canonized)
This distribution contains several Moose::Util::TypeConstraints with coercions, designed to work with the codes and names which were contained by Locale::Codes.
Mappings between Locale::Codes and MooseX::Types::Locale::Codes
Currently, the following modules are included.
This module designed to work with the codes and names of Locale::Codes::Country by following parameterized types:
An alias of Country{Code or Name}['alpha-2'] .
An alias of Country{Code or Name}['alpha-2'] .
It corresponds to alpha-2
country code set identified with the symbol LOCALE_CODE_ALPHA_2
.
An alias of Country{Code or Name}['alpha-2'] .
It corresponds to alpha-3
country code set identified with the symbol LOCALE_CODE_ALPHA_3
.
An alias of Country{Code or Name}['alpha-3'] .
It corresponds to numeric
country code set identified with the symbol LOCALE_CODE_NUMERIC
.
It corresponds to fips-10
country code set identified with the symbol LOCALE_CODE_FIPS
.
An alias of Country{Code or Name}['fips-10'] .
It corresponds to dom
country code set identified with the symbol LOCALE_CODE_DOM
.
An alias of Country{Code or Name}['domain'] .
This module designed to work with the codes and names of Locale::Codes::Currency by following parameterized types:
An alias of Currency{Code or Name}['alpha'] .
An alias of Currency{Code or Name}['alpha'] .
It corresponds to alpha
currency code set identified with the symbol LOCALE_CURR_ALPHA
.
It corresponds to alpha
currency code set identified with the symbol LOCALE_CURR_NUMERIC
.
This module designed to work with the codes and names of Locale::Codes::Language by following parameterized types:
An alias of Language{Code or Name}['alpha-2'] .
An alias of Language{Code or Name}['alpha-2'] .
It corresponds to alpha-2
language code set identified with the symbol LOCALE_LANG_ALPHA_2
.
An alias of Language{Code or Name}['alpha-2'] .
It corresponds to alpha-3
language code set identified with the symbol LOCALE_LANG_ALPHA_3
.
An alias of Language{Code or Name}['alpha-3'] .
An alias of Language{Code or Name}['alpha-3'] .
It corresponds to term
language code set identified with the symbol LOCALE_LANG_TERM
.
An alias of Language{Code or Name}['term'] .
This module designed to work with the codes and names of Locale::Codes::LangExt by following parameterized types:
An alias of LanguageExtension{Code or Name}['alpha'] .
An alias of LanguageExtension{Code or Name}['alpha'] .
It corresponds to alpha
language extension code set identified with the symbol LOCALE_LANGEXT_ALPHA
.
This module designed to work with the codes and names of Locale::Codes::LangVar by following parameterized types:
An alias of LanguageVariation{Code or Name}['alpha'] .
An alias of LanguageVariation{Code or Name}['alpha'] .
It corresponds to alpha
language variation code set identified with the symbol LOCALE_LANGVAR_ALPHA
.
This module designed to work with the codes and names of Locale::Codes::Script by following parameterized types:
An alias of Script{Code or Name}['alpha'] .
An alias of Script{Code or Name}['alpha'] .
It corresponds to alpha
script code set identified with the symbol LOCALE_SCRIPT_ALPHA
.
It corresponds to numeric
script code set identified with the symbol LOCALE_SCRIPT_NUMERIC
.
Option Types for MooseX::Getopt
These modules provide you the optional type mappings for MooseX::Getopt when it was installed. Then you can use the option types for Getopt::Long with hand-maid type mappings.
See each modules for further information.
These type coercions are intended solely for canonization of letter case. In other words, they do not convert codes and names by XXX2code()
, code2XXX()
and XXX_code2code()
on Locale::Codes::API.
For example:
{
package MyApp::Locale::Language;
use Moose;
use MooseX::Types::Locale::Codes::Language qw(
LanguageCode
LanguageName
);
has 'code' => (isa => LanguageCode[], coerce => 1, is => 'rw');
has 'name' => (isa => LanguageName[], coerce => 1, is => 'rw');
__PACKAGE__->meta->make_immutable;
}
my $language = MyApp::Locale::Language;
print $language->name('EN'); # It is not 'English' but 'en'.
print $language->code('english'); # It is not 'en' but 'English'.
If you want conversion, could you implement an individual locale class with several attributes?
{
package MyApp::Locale::Language::Complex;
use 5.010_001;
use Moose;
extends qw(MyApp::Locale::Language);
use Data::Validator;
use Locale::Language;
has '+name' => (lazy_build => 1, trigger => sub { $_[0]->clear_code });
has '+code' => (lazy_build => 1, trigger => sub { $_[0]->clear_name });
around BUILDARGS => sub {
state $rule = Data::Validator->new(
code => { xor => [qw(name)] },
name => { xor => [qw(code)] },
)->with('Method');
my ($next, $class, $init_args) = (shift, $rule->validate(@_));
return $class->$next($init_args);
};
sub _build_code {
confess 'Language name was not specified'
unless $_[0]->has_name;
return language2code($_[0]->name);
}
sub _build_name {
confess 'Language code was not specified'
unless $_[0]->has_code;
return code2language($_[0]->code);
}
__PACKAGE__->meta->make_immutable;
}
use Try::Tiny;
my $language = MyApp::Locale::Language::Comprex->new(code => 'EN');
print $language->code; # 'en' (was canonized by coercion)
print $language->name; # 'English' (was converted by builder)
print $language->code('EO'); # 'eo' (was canonized by coercion)
print $language->name; # 'Esperanto' (was canonized by builder)
try {
MyApp::Locale::Language::Complex->new(code => 'en', name => 'English');
}
catch {
print $_; # Exclusive parameters passed togther: 'code' v.s. 'name'...
};
None reported.
Note that MooseX::Types::Locale::Language and MooseX::Types::Locale::Country which become the predecessors of MooseX::Types::Locale::Codes::Language and MooseX::Types::Locale::Codes::Country are deprecated. The original two modules will continue to work, but all new type constraints and coercions will be added in the MooseX::Types::Locale::Codes
namespace.
Also MooseX::Types::Locale::Language::Fast and MooseX::Types::Locale::Country::Fast which do not provide coercions to save memory footprint and to quicken initialization will continue to work too, but the original two modules now provide coercions. Because these variations can easily be ignored.
More tests.
No bugs have been reported.
Please report any found bugs, feature requests, and ideas for improvements to bug-moosex-types-locale-codes at rt.cpan.org
, or through the web interface at http://rt.cpan.org/Public/Bug/Report.html?Queue=MooseX-Types-Locale-Codes. I will be notified, and then you'll automatically be notified of progress on your bugs/requests as I make changes.
When reporting bugs, if possible, please add as small a sample as you can make of the code that produces the bug. And of course, suggestions and patches are welcome.
You can find documentation for this module with the perldoc
command.
perldoc MooseX::Types::Locale::Codes
You can also look for information at:
- RT: CPAN's request tracker
-
http://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Types-Locale-Codes
- AnnoCPAN: Annotated CPAN documentation
- Search CPAN
- CPAN Ratings
This module is maintained using Git. You can get the latest version from git://github.com/gardejo/p5-moosex-types-locale-codes.git.
- MORIYA Masaki, alias Gardejo
-
<moriya at cpan dot org>
, http://gardejo.org/
Copyright (c) 2009-2011 MORIYA Masaki, alias Gardejo
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlgpl and perlartistic.
The full text of the license can be found in the LICENSE file included with this distribution.
It (MooseX::Types::Locale::Codes) gets ready for release as soon as MooseX::Types::Parameterizable is fixed.
https://github.com/gardejo/p5-moosex-types-locale-codes