+# Work out the correct locale to use if there is one otherwise we assume
+# UTF-8 which shouldn't kill ascii people and anyone else should have
+# correctly set locale.
+my $fromcharset = 'UTF-8';
+my $tocharset = 'UTF-8';
+
+my @all_encodings = Encode->encodings(":all");
+my $locale;
+if (defined $ENV{'LC_ALL'}) {
+ $locale = $ENV{'LC_ALL'};
+} elsif (defined $ENV{'LC_CTYPE'}) {
+ $locale = $ENV{'LC_CTYPE'};
+} elsif (defined $ENV{'LANG'}) {
+ $locale = $ENV{'LANG'};
+}
+if ($locale) {
+ $locale =~ s/.*\.//;
+ $locale = lc $locale;
+ foreach (@all_encodings) {
+ if ($locale eq lc $_) {
+ $tocharset = $fromcharset = $_;
+ last;
+ }
+ }
+}
+