Skip to content

Configure Language ​

Learn how to configure Short Number by changing the language and overwriting translations.

Change Language ​

Call the set() method once before using the package's other methods to change the language.

php
use Serhii\ShortNumber\Lang;

Lang::set('ru');

To get the active language, use the current() method, which returns the current language code as a string.

php
use Serhii\ShortNumber\Lang;

Lang::set('ru');

$lang = Lang::current();

echo $lang; // output: "ru"

Locale Variants ​

Lang::set() accepts common locale formats and normalizes them to the two-letter language code. Everything after - or _ is ignored, so region and script are dropped.

php
use Serhii\ShortNumber\Lang;

Lang::set('ru-RU');      // same as Lang::set('ru')
Lang::set('en_US');      // same as Lang::set('en')
Lang::set('RU');         // same as Lang::set('ru')
Lang::set('zh-Hans-CN'); // same as Lang::set('zh')

echo Lang::current(); // output: "zh"

This is handy when a framework or the Accept-Language header gives you a full locale string:

php
use Serhii\ShortNumber\Lang;

Lang::set($request->getPreferredLanguage()); // "en-US" becomes "en"

Overwrites are stored under the normalized code as well, so Lang::set('ru-RU', [...]) applies the same overwrites as Lang::set('ru', [...]).

WARNING

Malformed language codes fall back to English and trigger a deprecation notice. In v5.0.0, they will throw InvalidArgumentException instead. A valid code starts with two letters, followed by nothing, -, or _.

Overwrite Translations ​

To customize existing or missing translations for a supported language, provide custom translations as the second argument to Lang::set().

There are three approaches to overriding translations:

  1. Override the current language: Applies changes to the active language in use. Omitting the second argument keeps any overwrites that were set earlier.
  2. Override a specific language: Sets the overwrites for one or more languages at once. Keep in mind that setOverwrites() replaces all existing overwrites, not just the ones for the given languages.
  3. Add a custom language: Calls Lang::set('xx', [...]) with a language code that has no built-in set file. In that case, English abbreviation rules are used as the base and your translations are applied.

WARNING

Calling Lang::set('xx') without overwrites does not create a custom language. A two-letter code with no built-in set throws an Unsupported language 'xx' exception when formatting starts. Provide overwrites to use a custom language.

php
use Serhii\ShortNumber\Lang;

Lang::set('en', overwrites: [
    'k' => 'kilo',
    'm' => 'mil',
    'b' => 'bil',
    't' => 'tril',
    'q' => 'quad',
]);
php
use Serhii\ShortNumber\Lang;

Lang::setOverwrites([
    'en' => [
        'k' => 'kilo',
        'm' => 'mil',
        'b' => 'bil',
        't' => 'tril',
        'q' => 'quad',
    ],
]);
php
use Serhii\ShortNumber\Lang;
use Serhii\ShortNumber\Number;

Lang::set('xx', [
    'k' => 'K',
    'm' => 'M',
    'b' => 'B',
    't' => 'T',
    'q' => 'Q',
]);

echo Number::short(1500); // output: "1.5K"

Passing an empty array clears the overwrites for that language, restoring its default suffixes.

php
use Serhii\ShortNumber\Lang;
use Serhii\ShortNumber\Number;

Lang::set('en', overwrites: ['k' => 'thousand']);
echo Number::short(1000); // output: "1thousand"

Lang::set('en', overwrites: []);
echo Number::short(1000); // output: "1k"

All language sets live in the /sets directory at the root of the project. There, you can find suffixes that you can override.

Here is another example of a simple overwrite:

php
use Serhii\ShortNumber\Lang;
use Serhii\ShortNumber\Number;

Lang::set('zh', ['万' => ' 万']);

echo Number::short(12_000); // output: "1.2 万"

Reset to Defaults ​

Use the resetToDefaults() method to reset the language and overwrites to their default values. The language will be en (English), and overwrites will be an empty array.

php
use Serhii\ShortNumber\Lang;

Lang::resetToDefaults();