PHP code example of slexx / lang

1. Go to this page and download the library: Download slexx/lang library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

slexx / lang example snippets


// Устанавливаем файлы с переводами
Lang::setFile('ru', 'messages', __DIR__ . '/locales/ru/messages.json');
Lang::setFile('en', 'messages', __DIR__ . '/locales/en/messages.yaml');

// Устанавливаем локализацию
Lang::setLocale(Lang::searchLocale('ru'));

// Добываем переводы в нужных местах
Lang::translate('messages:key');

echo Lang::props('Hello, :name!', ['name' => 'World']);
// Hello, World!

var_dump(Lang::parseAcceptLanguage());
// [
//     ['code' => 'ru', 'region' => 'RU', 'quality' => 1],
//     ['code' => 'ru', 'region' => null, 'quality' => 0.8],
//     ['code' => 'en', 'region' => 'US', 'quality' => 0.6],
//     ['code' => 'en', 'region' => null, 'quality' => 0.4],
//     ...
// ]


return [
    'hello' => 'Привет, :name!',
];

Lang::setFile('ru', 'messages', __DIR__ . '/messages.php');
Lang::setLocale('ru');

echo Lang::translate('messages:hello', ['name' => 'Алексей']);
// Выведет: 'Привет, Алексей!'

Lang::setPluralFunction('ru', function($n) {
    return $n%10==1&&$n%100!=11?0:($n%10>=2&&$n%10<=4&&($n%100<10||$n%100>=20)?1:2);
});
Lang::setPluralFunction('en', function($n) {
    return $n>1?1:0;
});


return [
    'comments' => [':count комментарий', ':count комментария', ':count коментариев'],
];

Lang::setFile('ru', 'blog', __DIR__ . '/blog.php');
Lang::setLocale('ru');

Lang::plural('blog:comments', 1); // 1 комментарий
Lang::plural('blog:comments', 2); // 2 комментария
Lang::plural('blog:comments', 45); // 45 коментариев