burxon/letter-exchange
Laravel/PHP package to transliterate between Cyrillic and Latin alphabets. Convert Cyrillic letters to Latin or Latin back to Cyrillic for consistent slugs, search, and text normalization. Install via Composer: burxon/letter-exchange.
Installation:
composer require burxon/letter-exchange
Add the service provider to config/app.php under providers:
Burxon\LetterExchange\LetterExchangeServiceProvider::class,
First Use Case: Convert Cyrillic to Latin (e.g., for a username or slug):
use Burxon\LetterExchange\Facades\LetterExchange;
$cyrillicText = "Привет";
$latinText = LetterExchange::cyrillicToLatin($cyrillicText);
// Output: "Privet"
Reverse Conversion:
$latinText = "Privet";
$cyrillicText = LetterExchange::latinToCyrillic($latinText);
// Output: "Привет"
Facade Alias: Publish the config (optional) to customize mappings:
php artisan vendor:publish --provider="Burxon\LetterExchange\LetterExchangeServiceProvider"
User Input Sanitization: Convert Cyrillic usernames to Latin for consistency:
$username = request()->input('username');
$sanitized = LetterExchange::cyrillicToLatin($username);
Slug Generation: Create SEO-friendly URLs from Cyrillic content:
$title = "Привет, мир!";
$slug = Str::slug(LetterExchange::cyrillicToLatin($title));
// Output: "privet-mir"
Two-Way Conversion in Forms: Dynamically switch between languages based on user locale:
if (app()->getLocale() === 'en') {
$displayText = LetterExchange::cyrillicToLatin($originalText);
} else {
$displayText = LetterExchange::latinToCyrillic($originalText);
}
Batch Processing: Convert arrays or collections:
$cyrillicArray = ["Привет", "Мир"];
$latinArray = array_map([LetterExchange::class, 'cyrillicToLatin'], $cyrillicArray);
public function handle($request, Closure $next) {
$request->merge([
'sanitized_name' => LetterExchange::cyrillicToLatin($request->name)
]);
return $next($request);
}
public function saving(Model $model) {
$model->slug = Str::slug(LetterExchange::cyrillicToLatin($model->title));
}
return response()->json([
'name' => LetterExchange::cyrillicToLatin($user->name)
]);
Case Sensitivity:
The package preserves case but relies on predefined mappings. Test edge cases like "ПриВет" (mixed case).
Fix: Normalize case before conversion if needed:
LetterExchange::cyrillicToLatin(strtolower($text));
Unmapped Characters:
Special characters (e.g., ё, Ё) or non-Cyrillic/Latin letters may not convert.
Fix: Extend the config (config/letter-exchange.php) or pre-filter text:
preg_replace('/[^а-яА-Я]/u', '', $text);
Performance: Avoid converting the same text repeatedly in loops. Cache results if possible:
$cacheKey = 'converted_'.$text;
$converted = cache()->remember($cacheKey, now()->addHours(1), fn() =>
LetterExchange::cyrillicToLatin($text)
);
Locale Awareness:
The package doesn’t auto-detect locale. Explicitly handle conversions based on app()->getLocale().
dd(config('letter-exchange.mappings'));
$testCases = [
"Привет", "мир", "Ёлки", "A", "a", "123", "!@#"
];
foreach ($testCases as $text) {
echo LetterExchange::cyrillicToLatin($text)."\n";
}
Custom Mappings:
Override defaults in config/letter-exchange.php:
'mappings' => [
'custom' => [
'а' => 'a_custom',
'б' => 'b_custom',
],
],
Then use:
LetterExchange::convert($text, 'custom');
Add New Conversion Modes:
Extend the Converter class or create a new facade method by publishing and modifying the package source.
Reverse Lookup:
For bidirectional checks, combine with array_flip:
$latinToCyrillic = array_flip(config('letter-exchange.mappings.cyrillic_to_latin'));
Combine with Laravel Helpers:
Chain with Str::of() for fluent syntax:
Str::of($text)->cyrillicToLatin()->slug();
(Requires extending the Str macro.)
Database Storage: Store data in one script (e.g., Latin) and convert on display:
// Store
$user->save(['name' => LetterExchange::cyrillicToLatin($request->name)]);
// Display
$user->name_cyrillic = LetterExchange::latinToCyrillic($user->name);
Validation: Use for input validation rules:
$validator->addRules([
'username' => [
'required',
Rule::function(fn($attribute, $value) =>
preg_match('/^[a-z0-9_-]+$/i', LetterExchange::cyrillicToLatin($value))
),
],
]);
How can I help you explore Laravel packages today?