Skip to content

Commit

Permalink
Add translator function
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Jul 24, 2023
1 parent ac2f6a3 commit 3461264
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 1 deletion.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -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".'
```
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/functions.php
Expand Up @@ -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';
24 changes: 24 additions & 0 deletions src/translator.php
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Yiisoft\Translator\TranslatorInterface;

function t(
string $message,
array $parameters = [],
string $category = 'app',
string $locale = null
): string {
/**
* @var TranslatorInterface $translator

Check failure on line 14 in src/translator.php

View workflow job for this annotation

GitHub Actions / PHP 8.1-ubuntu-latest

UnnecessaryVarAnnotation

src/translator.php:14:13: UnnecessaryVarAnnotation: The @var Yiisoft\Translator\TranslatorInterface annotation for $translator is unnecessary (see https://psalm.dev/212)

Check failure on line 14 in src/translator.php

View workflow job for this annotation

GitHub Actions / PHP 8.2-ubuntu-latest

UnnecessaryVarAnnotation

src/translator.php:14:13: UnnecessaryVarAnnotation: The @var Yiisoft\Translator\TranslatorInterface annotation for $translator is unnecessary (see https://psalm.dev/212)
*/
$translator = container(TranslatorInterface::class);

return $translator->translate(
$message,
$parameters,
$category,
$locale,
);
}

0 comments on commit 3461264

Please sign in to comment.