Documentation
Lightweight package shortens given number to a short representation of it. For example 1234 will be formatted to 1k and 20244023 to 20m. Package supports multiple languages, the default it's set to English.
π Supported PHP versions
- β 7.2
- β 7.3
- β 7.4
- β 8.0
- β 8.1
- β 8.2
- β 8.3
βοΈ Language configurations
Change language
For changing the language you want to call set()
method once before calling other methods from this package.
Serhii\ShortNumber\Lang::set('ru');
Overwrite translations
If you want to replace existing translations for supported language or add your own language, you can pass custom translations as the second argument to set()
method.
// Overwriting all fields
Serhii\ShortNumber\Lang::set('en', [
'thousand' => 'thou',
'million' => 'mil',
'billion' => 'bil',
'trillion' => 'tril',
]);
You can overwrite any fields that you need, overwriting all fields is not necessary.
// Overwriting 1 field
Serhii\ShortNumber\Lang::set('en', ['million' => 'mil']);
π© Supported languages
Flag | Language | Code (ISO 639-1) | Thousand | Million | Billion | Trillion |
---|---|---|---|---|---|---|
π¬π§ | English | en | 1k | 1m | 1b | 1t |
π·πΊ | Russian | ru | 1ΡΡΡ | 1ΠΌΠ»Π½ | 1ΠΌΠ»Π΄ | 1ΡΡΠ½ |
πΊπ¦ | Ukrainian | uk | 1ΡΠΈΡ | 1ΠΌΠ»Π½ | 1ΠΌΠ»Π΄ | 1ΡΡΠ½ |
π Usage
use Serhii\ShortNumber\Number;
Number::conv(1893234); // returns: 1m
Number::conv(20234); // returns: 20m
π Contribute language support
Here is the commit that added support for Ukrainian language. Contribute another language is very simple. You need to make 3 steps:
Step 1. Translations
Add translations to resources/translations.php
file. Here is the file:
return [
// English
'en' => [
'thousand' => 'k',
'million' => 'm',
'billion' => 'b',
'trillion' => 't',
],
// Russian
'ru' => [
'thousand' => 'ΡΡΡ',
'million' => 'ΠΌΠ»Π½',
'billion' => 'ΠΌΠ»Π΄',
'trillion' => 'ΡΡΠ½',
],
// add same for your language
];
Step 2. Tests
After adding another language, you need to add line to tests/TranslationsTest.php
. The method runTestsForSuffixes(string $lang, string[] $suffixes)
will generate tests for you. You just need to run ./vendor/bin/phpunit
to make sure your translation works.
class TranslationsTest extends TestCase
{
public function testing_correct_conversion(): void
{
$this->runTestsForSuffixes('en', ['k', 'm', 'b', 't']);
$this->runTestsForSuffixes('ru', ['ΡΡΡ', 'ΠΌΠ»Π½', 'ΠΌΠ»Π΄', 'ΡΡΠ½']);
// add same line for your language here
}
}
Step 3. README.md
Last step you need to add new language to README.md file under the section Supported languages.
π Quick start
composer require serhii/short-number