Language Configurations
Learn how to configure Short Number by changing language and overwriting translations.
Change Language
For changing the language you want to call set()
method once before calling other methods from this package.
use Serhii\ShortNumber\Lang;
Lang::set('ru');
To get the current active language you can use the current()
method, which returns a string representing the current language code.
use Serhii\ShortNumber\Lang;
Lang::set('ru');
$lang = Lang::current();
echo $lang; // output: "ru"
Overwrite Translations
To customize existing translations for a supported language or to add a new language, you can provide custom translations as the second argument to the Lang::set()
method.
There are two approaches to overriding translations suffixes:
- Override the current language: Applies changes to the active language in use.
- Override a specific language: Targets a specific language, regardless of the currently active one.
use Serhii\ShortNumber\Lang;
Lang::set('en', overwrites: [
'k' => 'kilo',
'm' => 'mil',
'b' => 'bil',
't' => 'tril',
'q' => 'quad',
]);
use Serhii\ShortNumber\Lang;
Lang::setOverwrites([
'en' => [
'k' => 'kilo',
'm' => 'mil',
'b' => 'bil',
't' => 'tril',
'q' => 'quad',
],
]);
All the language sets live in the /sets
directory in the root of the project. There, you can find suffixes that you can override.
Here is another example of simple overwrite:
use Serhii\ShortNumber\Lang;
Lang::set('zh', ['千' => ' 千']);
echo Lang::short(2000); // output: "2 千"
Reset to Defaults
Use resetToDefaults()
method to reset the language and overwrites to their default values. For language it will be en
(English) and for overwrites it will be an empty array.
use Serhii\ShortNumber\Lang;
Lang::resetToDefaults();