Skip to content

Contribute Locale ​

One of the main features of Short Number is the ability to support multiple languages. If you want to add a new language to the library, you can do it by following the steps below.

Before Contributing ​

Before opening a pull request:

  • Fork the repository and create a branch for your language.
  • Check the existing sets to follow the same structure.
  • Run the test suite and verify the new set covers every supported number range.
  • Update README.md so the language is listed publicly.

Only Hindu-Arabic Numerals (0-9) Supported

Languages with a different numeral system, such as Arabic-Indic numerals, are not currently supported. Support for replacing the numeral system may be added in a future version if there is demand.

Step 1. Abbreviation Set ​

Add the language set to the /sets/ directory using the language code. Here is /sets/en.php, the set for English:

php
use Serhii\ShortNumber\AbbreviationSet;
use Serhii\ShortNumber\FormulaRule;

return new AbbreviationSet(formula: [
    new FormulaRule(inputDigits: 4, integerDigits: 1, suffix: 'k'),
    new FormulaRule(inputDigits: 5, integerDigits: 2, suffix: 'k'),
    new FormulaRule(inputDigits: 6, integerDigits: 3, suffix: 'k'),
    new FormulaRule(inputDigits: 7, integerDigits: 1, suffix: 'm'),
    new FormulaRule(inputDigits: 8, integerDigits: 2, suffix: 'm'),
    new FormulaRule(inputDigits: 9, integerDigits: 3, suffix: 'm'),
    new FormulaRule(inputDigits: 10, integerDigits: 1, suffix: 'b'),
    new FormulaRule(inputDigits: 11, integerDigits: 2, suffix: 'b'),
    new FormulaRule(inputDigits: 12, integerDigits: 3, suffix: 'b'),
    new FormulaRule(inputDigits: 13, integerDigits: 1, suffix: 't'),
    new FormulaRule(inputDigits: 14, integerDigits: 2, suffix: 't'),
    new FormulaRule(inputDigits: 15, integerDigits: 3, suffix: 't'),
    new FormulaRule(inputDigits: 16, integerDigits: 1, suffix: 'q'),
    new FormulaRule(inputDigits: 17, integerDigits: 2, suffix: 'q'),
    new FormulaRule(inputDigits: 18, integerDigits: 3, suffix: 'q'),
    new FormulaRule(inputDigits: 19, integerDigits: 1, suffix: 'qi'),
]);

This file should return an instance of the AbbreviationSet class.

Define the formula for your language. The formula is a list of FormulaRule objects. Each rule describes how to format a number with a specific number of digits:

  • inputDigits: The number of digits in the number.
  • integerDigits: The number of digits to take from the beginning of the number. Must be at least 1.
  • suffix: The suffix for the number. Like k for thousands in English.

Here is how the English set maps input lengths to output:

inputDigitsintegerDigitssuffixInputOutput
41k1,0001k
82m44,000,00044m
123b999,000,000,000999b

For example, inputDigits: 8 matches any number with 8 digits. integerDigits: 2 keeps the first 2 digits (44), and suffix: 'm' is appended, producing 44m.

FormulaRule also accepts optional argument for languages that need more control over the fractional part:

  • significantDigits:
    • The number of digits to retain when Config::decimals is not set.
    • Default: null.

This suffix format is flexible and can be used for any language. Next, let's look at the more advanced Chinese example.

Significant Digits ​

Some languages use their own counting systems and need a different amount of digits. The Chinese set uses significantDigits to match CLDR formatting:

php
use Serhii\ShortNumber\AbbreviationSet;
use Serhii\ShortNumber\FormulaRule;

return new AbbreviationSet(
    formula: [
        new FormulaRule(inputDigits: 5, integerDigits: 1, suffix: '万', significantDigits: 2),
        new FormulaRule(inputDigits: 6, integerDigits: 2, suffix: '万', significantDigits: 2),
        // ...
    ],
);

For 12345 the first matching rule is inputDigits: 5. It keeps 1 integer digit and 2 significant digits, so the output is 1.2万. For 1,000,000,000,000 the matching rule is inputDigits: 13. It keeps 1 integer digit and 2 significant digits, but the remaining digits are all zeros, so they are trimmed and the output is 1万亿.

Decimals Priority

If you explicitly pass decimals to Config, it takes priority over significantDigits. See Decimals vs Significant Digits for more details.

Step 2. Tests ​

In the /tests/Sets/ directory, create a test file for your language. The file should be named based on the language name. For example, for the Chinese language, the file is named ChineseSetTest.php.

You can just copy the content of one of the existing test files and rewrite it for your language. Make sure to test all the possible cases.

The typical data provider for a test look like one below. It's a truncated version of the Chinese test provider:

php
public static function provideLanguageSet(): array
{
    return self::withNegativeNumbers([
        ['0', 0],
        ['999', 999],
        // ....
        ['99京', 99_9054_2399_4223_9191],
        ['99京', 99_9999_9999_9999_9999],
    ]);
}

On the left, we define the expected output. On the right, we provide the number to use as input for Number::short().

PHPUnit Data Providers

If you are unfamiliar with PHPUnit Data Providers, read about them before adding test data. They provide an easy way to supply test data.

You do not need to test negative numbers because self::withNegativeNumbers() adds them to the provider. Alternatively, you can change where to place the minus sign by passing a second argument to self::withNegativeNumbers(). For example, to place the minus sign at the end of the number:

php
self::withNegativeNumbers([], '%s-');

Use this format only if your language requires the minus sign at the end.

Step 3. README File ​

Add new language to README.md file under the section Languages to let everybody know that this library supports your language.

md
## Languages
| Flag | Language           | Code |
| ---- | ------------------ | ---- |
| 🇷🇺   | Russian            | ru   |
| 🇬🇧   | English            | en   |
| 🇨🇳   | Chinese            | zh   |