diff options
Diffstat (limited to 'Babel/txt2php.php')
-rw-r--r-- | Babel/txt2php.php | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/Babel/txt2php.php b/Babel/txt2php.php new file mode 100644 index 00000000..11c32f18 --- /dev/null +++ b/Babel/txt2php.php @@ -0,0 +1,57 @@ +<?php +/** + * txt2php: Converts the text file of ISO codes to a PHP static array definition. + * + * Usage: php txt2php.php + */ + +use Wikimedia\StaticArrayWriter; + +if ( getenv( 'MW_INSTALL_PATH' ) ) { + $IP = getenv( 'MW_INSTALL_PATH' ); +} else { + $IP = __DIR__ . '/../..'; +} + +require_once "$IP/maintenance/CommandLineInc.php"; + +$dir = __DIR__; + +$names = []; +$codes = []; +$fr = fopen( "$dir/codes.txt", 'r' ); + +while ( true ) { + $line = fgets( $fr ); + if ( !$line ) { + break; + } + + // Format is code1 code2 "language name" + $line = explode( ' ', $line, 3 ); + $iso1 = trim( $line[0] ); + $iso3 = trim( $line[1] ); + // Strip quotes + $name = substr( trim( $line[2] ), 1, -1 ); + if ( $iso1 !== '-' ) { + $codes[ $iso1 ] = $iso1; + if ( $iso3 !== '-' ) { + $codes[ $iso3 ] = $iso1; + } + $names[ $iso1 ] = $name; + $names[ $iso3 ] = $name; + } elseif ( $iso3 !== '-' ) { + $codes[ $iso3 ] = $iso3; + $names[ $iso3 ] = $name; + } +} + +fclose( $fr ); + +$writer = new StaticArrayWriter(); +$header = 'This file is generated by txt2php.php. Do not edit it directly.'; +$code = $writer->create( $names, $header ); +file_put_contents( "$dir/names.php", $code ); + +$code = $writer->create( $codes, $header ); +file_put_contents( "$dir/codes.php", $code ); |