alexivanou/russian-text-bundle
Installation Add the bundle via Composer:
composer require alexivanou/russian-text-bundle
Register the bundle in config/bundles.php (Symfony) or config/app.php (Laravel via alexivanou/russian-text-bundle/RussianTextBundle::class).
First Use Case For Laravel, use the facade directly in a controller or service:
use RussianText\RussianText;
// Pluralize a number (e.g., "1 яблоко", "2 яблока", "5 яблок")
$text = RussianText::pluralize(3, 'яблоко', 'яблока', 'яблок');
Where to Look First
RussianText (main entry point).tests/ for real-world usage examples.Pluralization Handle Russian noun cases dynamically:
// For "день" (day)
$dayText = RussianText::pluralize($days, 'день', 'дня', 'дней');
Number Spelling Convert numbers to Russian words:
$spelled = RussianText::spellNumber(123); // "сто двадцать три"
Name Inflection Adjust names for grammatical cases (e.g., "Иван" → "Ивана"):
$inflected = RussianText::inflectName('Иван', 'genitive');
Money Formatting Format currency with proper pluralization:
$moneyText = RussianText::formatMoney(1234.56, 'рубль', 'рубля', 'рублей');
Transliteration Convert Cyrillic to Latin:
$translit = RussianText::transliterate('Привет'); // "Privet"
RussianText::validatePhone() or validateEmail() for Russian-specific rules.return response()->json([
'message' => RussianText::pluralize($count, 'элемент', 'элемента', 'элементов')
]);
Case Sensitivity
Russian inflection functions (e.g., inflectName) are case-sensitive. Ensure input matches expected format (e.g., "Иван" not "иван").
Pluralization Rules Some nouns have irregular plurals (e.g., "ребенок" → "дети"). Test edge cases like:
RussianText::pluralize(2, 'ребенок', 'ребенка', 'детей'); // Returns "дети"
Transliteration Quirks
transliterate() may not handle all special characters (e.g., ё → yo or e).config/russian_text.php:
'transliteration' => [
'custom' => ['ё' => 'yo'],
],
Performance Heavy operations (e.g., large-scale name inflection) may slow down requests. Cache results if reused:
$cacheKey = "inflected_{$name}_{$case}";
$inflected = cache()->remember($cacheKey, now()->addHours(1), fn() =>
RussianText::inflectName($name, $case)
);
spellNumber() throw exceptions for non-numeric inputs. Validate first:
if (!is_numeric($input)) {
throw new \InvalidArgumentException('Input must be numeric');
}
validatePhone() with strict mode:
$isValid = RussianText::validatePhone('+7 (999) 123-45-67', true); // Strict mode
Custom Rules
Extend pluralization rules by overriding the bundle’s Pluralizer class or adding custom entries to the config:
'plural_rules' => [
'custom_noun' => ['singular', 'plural', 'many'],
],
Service Provider
Bind custom implementations in Laravel’s AppServiceProvider:
$this->app->bind(RussianText::class, function ($app) {
return new CustomRussianText();
});
Testing Mock the facade for unit tests:
$this->partialMock(RussianText::class, ['pluralize'])
->shouldReceive('pluralize')
->andReturn('тест');
How can I help you explore Laravel packages today?