Last active

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

Script to convert a Serbian Cyrillic repo to Latin script

View gist:6069330
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
#!/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
Something went wrong with that request. Please try again.