Last active
December 20, 2015 04:18
-
-
Save pascalchevrel/6069330 to your computer and use it in GitHub Desktop.
Script to convert a Serbian Cyrillic repo to Latin script
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
#!/usr/bin/env php | |
<?php | |
// Transliteration to Serbian requires ICU library >= 5.0 | |
if (php_sapi_name() != 'cli') { | |
die('Nope'); | |
} | |
$targetFolder = (isset($argv[1])) ? $argv[1] : 'sr-Latn'; // target folder | |
/* | |
* Recursively find all files, ignore common VCS dot files | |
*/ | |
function findFiles($dir) | |
{ | |
$result = []; | |
$root = scandir($dir); | |
$ignore = ['.', '..', '.git', '.svn', '.hg', '.hgtags', '.gitignore']; | |
foreach ($root as $value) { | |
if (in_array($value, $ignore)) { | |
continue; | |
} | |
if (is_file("$dir/$value")) { | |
$split = explode('.', $value); | |
$result[] = "$dir/$value"; | |
continue; | |
} | |
foreach (findFiles("$dir/$value") as $value) { | |
$result[] = $value; | |
} | |
} | |
return $result; | |
} | |
/* | |
* Write to a file and creates the sub-folders if needed | |
*/ | |
function writeFile($dir, $contents) | |
{ | |
$parts = explode('/', $dir); | |
$file = array_pop($parts); | |
$dir = ''; | |
foreach ($parts as $part) { | |
if (!is_dir($dir .= "/$part")) { | |
mkdir($dir); | |
} | |
} | |
file_put_contents("$dir/$file", $contents); | |
} | |
$sourceFolder = 'sr-Cyrl'; | |
$sourcePath = __DIR__ . '/'; | |
$files = findFiles($sourcePath . $sourceFolder); | |
$translit = Transliterator::create('Serbian-Latin/BGN'); | |
foreach ($files as $file) { | |
$sourceFile = file_get_contents($file); | |
$targetPath = str_replace($sourcePath . $sourceFolder, __DIR__ . '/' . $targetFolder, $file); | |
writeFile($targetPath, $translit->transliterate($sourceFile)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment