From 3461264158b64340e430f0b759b76c809e0cc7e3 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Mon, 24 Jul 2023 08:41:50 +0300 Subject: [PATCH] Add translator function --- README.md | 14 ++++++++++++++ composer.json | 3 ++- src/functions.php | 2 ++ src/translator.php | 24 ++++++++++++++++++++++++ 4 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/translator.php diff --git a/README.md b/README.md index a09643d..f4c4f3a 100644 --- a/README.md +++ b/README.md @@ -110,4 +110,18 @@ alias('@runtime'); // => '/path/to/runtime' ```php aliases('@runtime', '@webroot'); // => ['/path/to/runtime', '/path/to/webroot'] +``` + +### `t(string $message, array $params = [], string $category = 'app', string $language = null): string` + +- `$message` is a translation message +- `$params` is a translation params +- `$category` is a translation category +- `$language` is a translation language + +```php +t('main.hello'); // => 'Hello world' +t('error.message', ['message' => 'Something went wrong']); // => 'Error: "Something went wrong".' +t('error.message', ['message' => 'Something went wrong'], 'modules'); // => 'Error from a module: "Something went wrong".' +t('error.message', ['message' => 'Something went wrong'], 'modules', 'ru'); // => 'Ошибка из модуля: "Something went wrong".' ``` \ No newline at end of file diff --git a/composer.json b/composer.json index 65d100b..9893068 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "httpsoft/http-message": "^1.1", "yiisoft/router": "^3.0", "yiisoft/router-fastroute": "^3.0", - "vimeo/psalm": "^5.13" + "vimeo/psalm": "^5.13", + "yiisoft/translator": "^3.0" }, "provide": { "psr/event-dispatcher-implementation": "1.0.0", diff --git a/src/functions.php b/src/functions.php index ab869dc..8d71572 100644 --- a/src/functions.php +++ b/src/functions.php @@ -5,3 +5,5 @@ require_once __DIR__ . '/user.php'; require_once __DIR__ . '/view.php'; require_once __DIR__ . '/router.php'; +require_once __DIR__ . '/alias.php'; +require_once __DIR__ . '/translator.php'; diff --git a/src/translator.php b/src/translator.php new file mode 100644 index 0000000..14dd7f7 --- /dev/null +++ b/src/translator.php @@ -0,0 +1,24 @@ +translate( + $message, + $parameters, + $category, + $locale, + ); +}