ISO-639-3
view release on metacpan or search on metacpan
lib/ISO/639/3.pm view on Meta::CPAN
-3: convert to three-letter code (ISO 639-3)
-m: convert to three-letter code but return the macro-language if available (ISO 639-3)
-n: don't print a final new line
-k: keep original code if no mapping is found
=cut
our %TwoToThree = ();
our %ThreeToTwo = ();
our %ThreeToThree = ();
our %ThreeToMacro = ();
our %NameToTwo = ();
our %NameToThree = ();
our %TwoToName = ();
our %ThreeToName = ();
&_read_iso639_codes;
## run the script if not called as a module
__PACKAGE__->run() unless caller();
lib/ISO/639/3.pm view on Meta::CPAN
=cut
sub get_iso639_1{
return $_[0] if (exists $TwoToName{$_[0]});
return $ThreeToTwo{$_[0]} if (exists $ThreeToTwo{$_[0]});
return $NameToTwo{lc($_[0])} if (exists $NameToTwo{lc($_[0])});
return $_[0] if (exists $TwoToThree{$_[0]});
## TODO: is it OK to fallback to macro language in this conversion?
## (should we add some regional code?)
if (exists $ThreeToMacro{$_[0]}){
return $ThreeToTwo{$ThreeToMacro{$_[0]}}
if (exists $ThreeToTwo{$ThreeToMacro{$_[0]}});
}
## try without regional extension
my $code = $_[0];
return &get_iso639_1($code) if ($code=~s/[\-\_].*$//);
return &get_iso639_1(lc($code)) if ($code ne lc($code));
return $_[0] if ($_[1]);
return 'xx';
}
=head2 $iso639_3 = get_iso639_3( $id )
lib/ISO/639/3.pm view on Meta::CPAN
=head2 $macro_language = get_macro_language( $id )
Return the ISO 639-3 code of the macro language for a given language or any ISO 639 code. Returns 'xxx' if the code is not recognized.
=cut
sub get_macro_language{
my $code = get_iso639_3($_[0],$_[1]);
return $ThreeToMacro{$code} if (exists $ThreeToMacro{$code});
return $code;
}
=head2 $language = get_language_name( $id )
Return the name of the language that corresponds to the given language code (any ISO639 code)
=cut
sub get_language_name{
lib/ISO/639/3.pm view on Meta::CPAN
sub _read_macrolanguage_table{
## macro-languages
# print STDERR "read macrolanguages";
while (<DATA>){
chomp;
last unless ($_);
my @f = split(/\t/);
next unless ($f[0]);
$ThreeToThree{$f[1]} = $f[1];
$ThreeToMacro{$f[1]} = $f[0];
}
}
sub _read_collective_language_table{
## collective languages from ISO639-2
# print STDERR "read collective language codes";
while (<DATA>){
chomp;
last unless ($_);
my @f = split(/\t/);
lib/ISO/639/3.pm view on Meta::CPAN
while (<DATA>){
chomp;
last unless ($_);
my @f = split(/\t/);
next unless ($f[0]);
unless (exists $ThreeToThree{$f[0]}){
$ThreeToThree{$f[0]} = $f[1] ? $f[1] : $f[0];
if ($f[2]){
$ThreeToTwo{$f[0]} = $f[2];
}
$ThreeToMacro{$f[0]} = $f[3] if ($f[3]);
if ($f[4]){
$ThreeToName{$f[0]} = $f[4];
}
}
if ($f[4]){
$NameToThree{lc($f[4])} = $f[0] unless (exists $NameToThree{$f[4]});
if ($f[2]){
$NameToTwo{lc($f[4])} = $f[2] unless (exists $NameToTwo{$f[4]});
}
}
( run in 0.536 second using v1.01-cache-2.11-cpan-49f99fa48dc )