User:Mutante/langcodes

mysql> describe langcodes; +-+--+--+-+-+---+ +-+--+--+-+-+---+ +-+--+--+-+-+---+ 5 rows in set (0.02 sec)
 * Field  | Type         | Null | Key | Default | Extra |
 * alpha3 | char(3)      | YES  |     | NULL    |       |
 * alpha3t | char(3)     | YES  |     | NULL    |       |
 * alpha2 | char(2)      | YES  |     | NULL    |       |
 * nameen | varchar(255) | YES  |     | NULL    |       |
 * namefr | varchar(255) | YES  |     | NULL    |       |

mysql> select * from langcodes limit 3; ++-++---+-+ ++-++---+-+ ++-++---+-+
 * alpha3 | alpha3t | alpha2 | nameen   | namefr  |
 * aar   |         | aa     | Afar      | afar    |
 * abk   |         | ab     | Abkhazian | abkhaze |
 * ace   |         |        | Achinese  | aceh    |

mysql> show create table langcodes; +---+--+ +---+--+ `alpha3` char(3) default NULL, `alpha3t` char(3) default NULL, `alpha2` char(2) default NULL, `nameen` varchar(255) default NULL, `namefr` varchar(255) default NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1 | +---+--+ 1 row in set (0.00 sec)
 * Table    | Create Table                                                                                                                                                                                                                                     |
 * langcodes | CREATE TABLE `langcodes` (

<?php
 * 1) Lookup language codes from ISO-639
 * 2) import from http://www.loc.gov/standards/iso639-2/langhome.html
 * 3) used by Wiktionary, Wikipedia and many other projects
 * 4) mutante/s23.org


 * 1) Functions

function alpha2lang ($alpha) {
 * 1) Input: 2 or 3-letter code, Output: language name in English and French

switch (strlen($alpha)) { case 2: $query="select * from langcodes where alpha2='$alpha'"; break; case 3: $query="select * from langcodes where alpha3='$alpha' or alpha3t='$alpha'"; break; default: return "codes can only be 2 or 3 letters long! \n"; }

$result = mysql_magic("$query");

if(mysql_num_rows($result)==0){ $langnames="Could not find this code."; } else { while($row = mysql_fetch_array( $result )) { $nameen=$row['nameen']; $namefr=$row['namefr']; $alpha2=$row['alpha2']; $alpha3=$row['alpha3']; $alpha3t=$row['alpha3t'];

$langnames="'$alpha' is $nameen - $alpha2 $alpha3 $alpha3t"; } } return $langnames; }


 * 1) Input: Language name in English, Output: 2 and 3-letter codes

function lang2alpha ($langname) {

$query="select * from langcodes where nameen like '%$langname%' or namefr like '%$langname%'"; $result = mysql_magic("$query");

if(mysql_num_rows($result)==0){

$langcodes="No codes found for this language name.";

} else { while($row = mysql_fetch_array( $result )) { $nameen=$row['nameen']; $alpha2=$row['alpha2']; $alpha3=$row['alpha3']; $alpha3t=$row['alpha3t'];

$langcodes.="The language codes for $nameen are: $alpha2 $alpha3 $alpha3t \n"; }

} return $langcodes; }

function mysql_magic($query) { $dbhost="localhost"; $dbdatabase="codes"; $dbname="fnord"; $dbpass="fnord"; mysql_connect("$dbhost", "$dbname", "$dbpass") or die(mysql_error); mysql_select_db("$dbdatabase") or die(mysql_error); $result = mysql_query("$query") or die(mysql_error); return $result; }
 * 1) Function to connect to MySQL, input query, output $result


 * http://s23.org/~mutante/codes.sql


 * http://www.loc.gov/standards/iso639-2/langhome.html
 * http://www.loc.gov/standards/iso639-2/ISO-639-2_8859-1.txt
 * http://www.loc.gov/standards/iso639-2/ISO-639-2_utf-8.txt