-
-
Save sshaw/b51fa0283645b2cc39cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Mojolicious::Plugin::CountryDropDown; | |
# ABSTRACT: Provide a dropdown where users can select a country | |
use strict; | |
use warnings; | |
use Mojo::Base 'Mojolicious::Plugin'; | |
use Mojo::ByteStream; | |
use List::MoreUtils qw|zip pairwise|; | |
use Locale::Country::Multilingual { use_io_layer => 1 }; | |
use Unicode::Collate; | |
our $VERSION = 0.05_01; | |
$VERSION = eval $VERSION; | |
sub register { | |
my $self = shift; | |
my $app = shift; | |
my $conf = shift; | |
$conf->{lang} = uc( $conf->{lang} || 'EN' ); | |
$conf->{selected} = ''; | |
my $collate = Unicode::Collate->new(); | |
my $lcm = Locale::Country::Multilingual->new(); | |
my $loaded = $lcm->assert_lang( $conf->{lang} ); | |
unless ( defined $loaded and uc($loaded) eq uc( $conf->{lang} ) ) { | |
$app->log->error( | |
"Unable to load language " . $conf->{lang} . "; falling back to English" ); | |
$lcm->set_lang('en'); | |
} | |
else { | |
$lcm->set_lang( $conf->{lang} ); | |
} | |
$app->helper( | |
get_country_list => sub { | |
my $self = shift; | |
my %opt = %{ shift || {} }; | |
my $lang = lc( $opt{lang} || $conf->{lang} ); | |
my %list = (); | |
@list{ $lcm->all_country_codes } = $lcm->all_country_names($lang); | |
return %list; | |
} | |
); | |
$app->helper( | |
show_country_list => sub { | |
my $self = shift; | |
$self->stash( country_drop_down => $self->country_drop_down(@_) ); | |
return; | |
} | |
); | |
$app->helper( | |
'code2country' => sub { | |
my $self = shift; | |
my $code = lc shift; | |
my $lang = lc( shift || $conf->{lang} ); | |
return unless defined $code and $code; | |
return $lcm->code2country( $code, $lang ); | |
} | |
); | |
$app->helper( | |
'country2code' => sub { | |
my $self = shift; | |
my $country = shift; | |
my $lang = lc( shift || $conf->{lang} ); | |
return unless defined $country and $country; | |
return $lcm->country2code( $country, 'LOCALE_CODE_ALPHA_2', $lang ); | |
} | |
); | |
$app->helper( | |
'country_drop_down' => sub { | |
my $self = shift; | |
my %opt = %{ shift || {} }; | |
my $code = $opt{selected} ? uc( $opt{selected} ) : $conf->{selected}; | |
my $lang = $opt{lang} || $conf->{lang}; | |
my %attr = %{ $opt{attr} || {} }; | |
$attr{id} = 'country' unless defined( $attr{id} ) and length( $attr{id} ) > 0; | |
$attr{name} = 'country' unless defined( $attr{name} ) and length( $attr{name} ) > 0; | |
my @names = $lcm->all_country_names($lang); | |
my @codes = $lcm->all_country_codes; | |
my %countries = zip @names, @codes; | |
my @sorted_names = $collate->sort(keys %countries); | |
my @sorted_codes = @countries{@sorted_names}; | |
my @options = pairwise { | |
# $a = $names->[$n], $b = $codes->[$n] | |
my $option = [ $a, $b ]; | |
push @$option, selected => "selected" if $code and $code eq $b; | |
$option; | |
} @sorted_names, @sorted_codes; | |
return $self->select_field($attr{name}, \@options, %attr); | |
} | |
); | |
return; | |
} ## |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment