Skip to content

Instantly share code, notes, and snippets.

@ocean90
Created February 6, 2016 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ocean90/ef98c5bb2308354a36be to your computer and use it in GitHub Desktop.
Save ocean90/ef98c5bb2308354a36be to your computer and use it in GitHub Desktop.
<?php
$data = file_get_contents( 'https://raw.githubusercontent.com/unicode-cldr/cldr-core/master/supplemental/territoryInfo.json' );
$data = json_decode( $data );
$country2langs = [];
foreach ( $data->supplemental->territoryInfo as $country => $data ) {
if ( ! isset( $data->languagePopulation ) ) {
continue;
}
$country = strtolower( $country );
$language_population = [];
foreach ( $data->languagePopulation as $language => $data ) {
$language = strtok( $language, '_' );
if ( 'en' == $language && ! in_array( $country, [ 'au', 'ca', 'nz', 'za', 'gb' ] ) ) {
continue;
}
if ( (float) $data->_populationPercent > 18 || ( isset( $data->_officialStatus ) && in_array( $data->_officialStatus, [ 'official', 'official_regional' ] ) ) ) {
$language_population[ $language ] = (float) $data->_populationPercent;
}
}
if ( empty( $language_population ) ) {
continue;
}
if ( ! isset( $country2langs[ $country ] ) ) {
$country2langs[ $country ] = [];
}
arsort( $language_population );
$language_population = array_keys( $language_population );
if ( count( $language_population ) == 1 ) {
$country2langs[ $country ] = reset( $language_population );
} else {
$country2langs[ $country ] = $language_population;
}
}
$export = preg_replace( '/(\d+\s=>)/', '', var_export( $country2langs, true ) );
$export = preg_replace( '/(=>\s+array \()/m', '=> array(', $export );
$export = preg_replace( '/(\(\s+)/m', '( ', $export );
$export = preg_replace( '/(,\s{6}\')/m', ', \'', $export );
$export = preg_replace( '/(,\s{3}\))/m', ' )', $export );
$export = preg_replace( '/( )/', "\t", $export );
$export = preg_replace( '/(^array \( )/', "array(\n\t", $export );
echo $export;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment