pinoox / numera
Translate Numera
Installs: 7 601
Dependents: 0
Suggesters: 0
Security: 0
Stars: 16
Watchers: 0
Forks: 3
Open Issues: 1
Requires
- php: ^8.0
Requires (Dev)
- phpunit/phpunit: ^10.5
This package is auto-updated.
Last update: 2025-04-10 09:36:15 UTC
README
Numera is a PHP library that provides a simple and efficient way to convert numbers to words and vice versa. It supports multiple languages and can be easily extended to support more languages.
- Features
- Installation and Setup
- Usage
- Supported Languages
- Set Locale
- Set Locale Fallback
- Get Translates
- Add Translate
- Create a New Language
- Author
- Contributing
- Documentation
- License
Features
- Convert numbers to words (e.g. 1234 to "one thousand two hundred thirty-four")
- Convert words to numbers (e.g. "one thousand two hundred thirty-four" to 1234)
- Support for multiple languages (currently English and Persian, with more to come)
- Camel case support for output words
- Easy to use and extend
Installation and Setup
Install via Composer
You can install Numera using Composer:
composer require pinoox/numera
Initialize Numera
To use Numera, you need to initialize it with a locale. You can do this using the init
method:
use Pino\Numera; $numera = Numera::init('en'); // Initialize with English locale
Convert Numbers to Words
To convert a number to words, use the convertToWords
method:
$result = $numera->convertToWords(4454545156); echo $result; // Output: "four billion, four hundred fifty-four million, five hundred forty-five thousand, one hundred fifty-six"
Alternatively, you can use the n2w
method for a simpler syntax:
$result = $numera->n2w('4,454,545,156'); echo $result; // Output: "four billion, four hundred fifty-four million, five hundred forty-five thousand, one hundred fifty-six"
Convert Numbers to Summary
To convert a number to summary words, use the convertToSummary
method:
$result = $numera->convertToSummary(4454545156); echo $result; // Output: "4 Billion, 454 Million, 545 Thousand, 156"
Alternatively, you can use the n2w
method for a simpler syntax:
$result = $numera->n2s('4,454,545,156'); echo $result; // Output: "4 Billion, 454 Million, 545 Thousand, 156"
Convert Words to Numbers
To convert words to a number, use the convertToNumber
method:
$result = $numera->convertToNumber('four billion, four hundred fifty-four million, five hundred forty-five thousand, one hundred fifty-six'); echo $result; // Output: 4454545156
Alternatively, you can use the w2n
method for a simpler syntax:
$result = $numera->w2n("4 Billion, 454 Million, 545 Thousand, 156"); echo $result; // Output: 4454545156
You can also specify separators for the w2n
method:
$result = $numera->w2n('four billion, four hundred fifty-four million, five hundred forty-five thousand, one hundred fifty-six', [' ', ',']); echo $result; // Output: 4454545156
Use Camel Case
To use camel case for output words, use the setCamelCase
method:
$numera->setCamelCase(true); $result = $numera->convertToWords('4,454,545,156'); echo $result; // Output: "Four Billion, Four Hundred Fifty-Four Million, Five Hundred Forty-Five Thousand, One Hundred Fifty-Six"
Supported Languages
Numera currently supports the following languages:
- English (en)
- Persian (fa)
Set Locale
To set the locale for the Numera object, use the setLocale
method:
$numera->setLocale('fa'); // Set locale to Persian
Set Locale Fallback
To set the fallback locale for the Numera object, use the setLocaleFallback
method:
$numera->setLocaleFallback('en'); // Set fallback locale to English
Get Translates
To get the translates for the current locale, use the getTranslates
method:
$translates = $numera->getTranslates(); print_r($translates); // Output: Array of translates for the current locale
Add Translate
To add translates for a specific locale, use the addTranslate
method:
$numera->addTranslate('fr', ['four' => 'quatre']); // Add French translates
Add Translate File
To add translates by array file for a specific locale, use the addTranslateFile
method:
$numera->addTranslateFile('fr','/path/lang/fr.php'); // Add French translates
Create a New Language
If you want to add support for a new language, please read our Translation Guide for a step-by-step guide on how to create a new language pack.
Author
Numera was created by Pinoox.
Contributing
If you'd like to contribute to Numera, please fork the repository and submit a pull request. We'd love to have your help.
Documentation
- README.md - Documents
- TranslationGuide.md - Guide for creating a new language pack
License
Numera is licensed under the MIT License. See the LICENSE file for more information.