nurbekjummayev / laravel-number-words
Multi-language number and date formatter with easy translation support for Laravel
Package info
github.com/nurbekjummayev/laravel-number-words
pkg:composer/nurbekjummayev/laravel-number-words
Requires
- php: ^8.3|^8.4|^8.5
- illuminate/support: ^11.0|^12.0|^13.0
- nesbot/carbon: ^2.72|^3.0
- spatie/laravel-package-tools: ^1.93
Requires (Dev)
- orchestra/testbench: ^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.5|^11.0|^12.0
README
A Laravel package for converting numbers to words and formatting dates in multiple languages. The core logic stays in one place, while translations are stored in separate language files. Developers can easily add new languages by simply adding translation files without touching the core code.
Features
- Convert numbers to words in multiple languages
- Format currency with localized text
- Convert numbers to ordinal format (1st, 2nd, 3rd, etc.)
- Format dates with localized month and day names
- Relative time formatting (e.g., "2 hours ago")
- Easy to add new languages - just create translation files
- No code duplication - universal algorithm for all languages
- Laravel-friendly with Facade support and helper functions
Supported Languages
Out of the box:
- Uzbek (Latin) (uz) - O'zbek tili
- Uzbek (Cyrillic) (uz_cyrl) - Ўзбек тили
- English (en)
- Russian (ru) - Русский
Learn how to add more languages →
Installation
Install via Composer:
composer require nurbekjummayev/laravel-number-words
The package will automatically register its service provider.
Publish Configuration (Optional)
php artisan vendor:publish --tag=multilang-config
Publish Language Files (Optional)
php artisan vendor:publish --tag=multilang-translations
Configuration
After publishing the config file, you can set your default locale in config/multilang.php:
'default_locale' => env('MULTILANG_LOCALE', 'uz'),
Or set it in your .env file:
MULTILANG_LOCALE=uz
Usage
Number to Words
Using Facade
use MultiLangFormatter\Facades\ML; // Uzbek (default) echo ML::number()->toWords(1234567); // Output: "Bir million ikki yuz o'ttiz to'rt ming besh yuz oltmish yetti" // English echo ML::number()->locale('en')->toWords(1234567); // Output: "One million two hundred thirty-four thousand five hundred sixty-seven" // Russian echo ML::number()->locale('ru')->toWords(1234567); // Output: "Один миллион двести тридцать четыре тысячи пятьсот шестьдесят семь"
Using Helper Function
// Default locale echo ml_number(12345); // Output: "O'n ikki ming uch yuz qirq besh" // Specific locale echo ml_number(12345, 'en'); // Output: "Twelve thousand three hundred forty-five" // With options echo ml_number(1234.56, 'uz', ['decimals' => true, 'capitalize' => true]); // Output: "Bir ming ikki yuz o'ttiz to'rt, 56"
Currency Formatting
use MultiLangFormatter\Facades\ML; // Uzbek echo ML::number()->toCurrency(1500.50); // Output: "Bir ming besh yuz so'm 50 tiyin" // English echo ML::number()->locale('en')->toCurrency(1500.50); // Output: "One thousand five hundred dollar 50 cent" // Custom currency echo ML::number()->toCurrency(2000, 'EUR'); // Output: "Ikki ming EUR" // Using helper echo ml_currency(1500.50); // Output: "Bir ming besh yuz so'm 50 tiyin"
Ordinal Numbers
use MultiLangFormatter\Facades\ML; // Uzbek echo ML::number()->toOrdinal(1); // "birinchi" echo ML::number()->toOrdinal(2); // "ikkinchi" echo ML::number()->toOrdinal(3); // "uchinchi" // English echo ML::number()->locale('en')->toOrdinal(1); // "first" echo ML::number()->locale('en')->toOrdinal(2); // "second" echo ML::number()->locale('en')->toOrdinal(3); // "third" // Using helper echo ml_ordinal(1); // "birinchi" echo ml_ordinal(21, 'en'); // "twenty-first"
Date Formatting
use MultiLangFormatter\Facades\ML; // Uzbek echo ML::date()->format('2024-03-15', 'full'); // Output: "2024-yil 15-Mart, Juma" // English echo ML::date()->locale('en')->format('2024-03-15', 'full'); // Output: "Friday, March 15, 2024" // Russian echo ML::date()->locale('ru')->format('2024-03-15', 'full'); // Output: "Пятница, 15 Март 2024" // Using helper echo ml_date('2024-03-15', 'full'); // Output: "2024-yil 15-Mart, Juma" echo ml_date('2024-03-15', 'short', 'en'); // Output: "March 15"
Available Date Formats
default: Standard format for the localefull: Full format with weekdayshort: Short format (month and day only)long: Long format with year
Relative Time
use MultiLangFormatter\Facades\ML; // Uzbek echo ML::date()->relative('2024-03-14'); // Output: "1 kun oldin" // English echo ML::date()->locale('en')->relative('2024-03-14'); // Output: "1 day ago" // Russian echo ML::date()->locale('ru')->relative('2024-03-14'); // Output: "1 день назад" // Using helper echo ml_date_relative('2024-03-14'); // Output: "1 kun oldin"
Adding New Languages
Adding a new language is super easy! You just need to create two translation files:
lang/{locale}/numbers.php- Number translationslang/{locale}/dates.php- Date translations
For detailed instructions, see ADDING_LANGUAGES.md
Quick Example: Adding Turkish
Create lang/tr/numbers.php:
<?php return [ 'words' => [ 1000 => 'bin', 100 => 'yüz', // ... more numbers ], 'scales' => [ 4 => 'milyar', 3 => 'milyon', 2 => 'bin', ], 'ordinals' => [ 1 => 'birinci', 2 => 'ikinci', // ... more ], 'default_currency' => 'lira', 'plural_rule' => 'none', ];
Create lang/tr/dates.php:
<?php return [ 'months' => [ 1 => 'Ocak', 2 => 'Şubat', // ... more months ], 'weekdays' => [ 0 => 'Pazar', 1 => 'Pazartesi', // ... more days ], // ... other date settings ];
That's it! Now you can use it:
echo ml_number(1234, 'tr'); // Output: "bin iki yüz otuz dört"
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Security
If you discover any security-related issues, please email jummayevnurbek279@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
Support
If you find this package helpful, please consider giving it a star on GitHub!