+ # normalize country and state if they're not already ISO codes
+ # easier than doing it on the client side--we already have the tables here
+ my $country = $packet->{country};
+ my $state = $packet->{state};
+ if (length($country) > 2) {
+ # it likes title case
+ $country = join(' ', map ucfirst, split(/\s+/, $country));
+ my $lsc = Locale::SubCountry->new($country);
+ if ($lsc) {
+ $country = uc($lsc->country_code);
+
+ if ($lsc->has_sub_countries) {
+ if ( $lsc->full_name($state) eq 'unknown' ) {
+ # then we were probably given a full name, so resolve it
+ $state = $lsc->code($state);
+ if ( $state eq 'unknown' ) {
+ # doesn't resolve as a full name either, return an error
+ $error = "Unknown state: ".$packet->{state};
+ } else {
+ $state = uc($state);
+ }
+ }
+ } # else state doesn't matter
+ } else {
+ # couldn't find the country in LSC
+ $error = "Unknown country: $country";
+ }
+ }
+ $location->set('country', $country);
+ $location->set('state', $state);