Created
June 13, 2017 12:58
-
-
Save FabianPastor/a1e05e7796e833f1590eee78e473eb40 to your computer and use it in GitHub Desktop.
Small script to download and format into JSON the IANA Languages subtag registry
This file contains 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
<?php | |
$iana_lang_subtags_file = 'iana_lang_subtags.txt'; | |
$iana_lang_subtags_url = 'https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry'; | |
$iana_lang_subtags_json = 'iana_lang_subtags.json'; | |
$update = false; if(isset($argv[1])) $update = true; | |
$langs = new stdClass; | |
$langs->metadata = new stdClass; | |
$uri = $iana_lang_subtags_file; | |
if(!file_exists($iana_lang_subtags_file) || $update){ | |
$uri = $iana_lang_subtags_url; | |
$update = true; | |
} | |
// fetch list | |
$raw = file_get_contents($uri); | |
// save to file | |
if($update) file_put_contents($iana_lang_subtags_file, $raw); | |
$langs_strings = explode('%%', $raw); | |
$langs->metadata->comment = trim(array_shift($langs_strings)); | |
$structure_langs = new stdClass; | |
$structure_langs->tags = new stdClass; | |
$structure_langs->total = 0; | |
foreach($langs_strings as $lang_string){ | |
$lang = new stdClass; | |
//$lang_aux = new stdClass; | |
foreach(explode("\n", $lang_string) as $line){ | |
$line=trim($line); | |
if($line=="") continue; | |
$item = explode(": ",$line); | |
if( count($item) > 1 ){ | |
list($name,$value)=$item; | |
$name = str_replace(["-"], ["_"], trim(strtolower($name))); | |
if(strpos($name," ")===false){ | |
$lang->$name[] = trim($value); | |
$last=$name; | |
}else{ | |
$lang->$last[]=$line; | |
} | |
}else{ | |
$lang->$last[]=$item[0]; | |
} | |
} | |
foreach($lang as $prop => &$val){ | |
//var_dump($val[0]); | |
$val = implode("\n",$val); | |
if(!isset($structure_langs->tags->$prop)) $structure_langs->tags->$prop = 0; | |
++$structure_langs->tags->$prop; | |
} | |
++$structure_langs->total; | |
//var_dump($lang); | |
$langs->content[] = $lang; | |
} | |
$structure_tags = new stdClass; | |
foreach($structure_langs->tags as $tag => $value){ | |
if($structure_langs->total == $value){ | |
$structure_tags->$tag = "always-present"; | |
}else{ | |
$structure_tags->$tag = "optional"; | |
} | |
} | |
$langs->metadata->structure = $structure_tags; | |
file_put_contents($iana_lang_subtags_json, json_encode($langs, JSON_PRETTY_PRINT)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment