K
K
konservat0r2020-10-05 10:16:44
Joomla
konservat0r, 2020-10-05 10:16:44

Transliteration of Cyrillic to Latin?

When uploading files to the gallery, Russian names are not converted to Latin, how can I change the code so that transliteration occurs?

static function replaceSpecial($fileName)
{
$fileExt = JFile::getExt($fileName);
$fileNameNoExt = JFile::stripExt($fileName);
$fileNameNoExt = preg_replace('/[^A-Za-z0-9.]/', '_', $fileNameNoExt);

$fileExtLowerCase = strtolower($fileExt);

$fileName = $fileNameNoExt.'.'.$fileExtLowerCase;

return $fileName;
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
exmmth, 2020-10-05
@exmmth

Before the preg_replace line, use the standard joomla transliteration functionality
here is an example

A
Alexey Gnevyshev, 2020-10-05
@iResource

If the standard transliteration (which has already been mentioned) isn't enough or doesn't quite fit,
you can take control of this by writing and then using a PHP function. I use it in such cases:

// translit
function rus2lat4alias($text) {
    $text = mb_strtolower(trim($text));
    $text = preg_replace("/([0-9])х([0-9])/", "$1-$2", $text); // Ха русская
    $text = preg_replace("/([0-9])x([0-9])/", "$1-$2", $text); // Икс латинская
    $ru = explode(" ", "а б в г д е ё ж з и й к л м н о п р с т у ф х ц ч ш щ ъ ы ь э ю я");
    $lat = explode(" ", "a b v g d e e zh z i j k l m n o p r s t u f h c ch sh sch - y - e ju ja");
    $text = str_replace($ru, $lat, $text);
    unset($ru, $lat);
    $text = preg_replace("/[^a-zA-Z0-9\-]/", "-", $text);
    $text = preg_replace("/[-]{2,}/", "-", $text);
    $text = trim($text, '-');
    return $text;
}

Usage example:
$text = 'Некий текст - пуcть будет с размером 300х400 :)';
$alias  = rus2lat4alias($text);

From the blog .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question