• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bckp / translator-core / 14307448765

07 Apr 2025 10:58AM UTC coverage: 98.585%. Remained the same
14307448765

push

github

bckp
Small fixes and changes in code

16 of 16 new or added lines in 3 files covered. (100.0%)

3 existing lines in 2 files now uncovered.

209 of 212 relevant lines covered (98.58%)

0.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.12
/src/Translator.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * BCKP Translator
7
 * (c) Radovan Kepák
8
 *
9
 * For the full copyright and license information, please view
10
 * the file license.md that was distributed with this source code.
11
 *
12
 * @author Radovan Kepak <radovan@kepak.dev>
13
 */
14

15
namespace Bckp\Translator;
16

17
use Bckp\Translator\Interfaces\Diagnostics;
18
use Stringable;
19

20
use function array_key_exists;
21
use function array_key_last;
22
use function is_array;
23
use function vsprintf;
24

25
final class Translator implements Interfaces\Translator
26
{
27
        /** @var callable function(string $string): string */
28
        protected $normalizeCallback;
29

30
        public function __construct(
1✔
31
                private readonly Catalogue $catalogue,
32
                private readonly ?Diagnostics $diagnostics = null
33
        ) {
34
                $this->normalizeCallback = [$this, 'normalize'];
1✔
35
                $this->diagnostics?->setLocale($catalogue->locale());
1✔
36
        }
1✔
37

38
        /**
39
         * @api
40
         */
41
        public function normalize(string $string): string
1✔
42
        {
43
                return str_replace(
1✔
UNCOV
44
                        ['%label', '%value', '%name'],
×
UNCOV
45
                        ['%%label', '%%value', '%%name'],
×
46
                        $string
1✔
47
                );
48
        }
49

50
        /**
51
         * @api
52
         */
53
        #[\Override]
54
        public function setNormalizeCallback(callable $callback): void
1✔
55
        {
56
                $this->normalizeCallback = $callback;
1✔
57
        }
1✔
58

59
        /**
60
         * @api
61
         */
62
        #[\Override]
63
        public function translate(string|Stringable $message, float|int|string ...$parameters): string
1✔
64
        {
65
                $message = (string) $message;
1✔
66

67
                if (empty($message)) {
1✔
68
                        return '';
1✔
69
                }
70

71
                $translation = $this->catalogue->get($message);
1✔
72
                if (!$translation) {
1✔
73
                        $this->untranslated($message);
1✔
74
                        return $message;
1✔
75
                }
76

77
                // Plural option is returned, we need to choose the right one
78
                if (is_array($translation)) {
1✔
79
                        $plural = is_numeric($parameters[0] ?? null) ? $this->catalogue->plural((int) $parameters[0]) : Plural::Other;
1✔
80
                        $translation = $this->getVariant($message, $translation, $plural);
1✔
81
                }
82

83
                if (!empty($parameters)) {
1✔
84
                        $translation = ($this->normalizeCallback)($translation);
1✔
85
                        $translation = @vsprintf($translation, $parameters);
1✔
86
                }
87

88
                return $translation;
1✔
89
        }
90

91
        /**
92
         * @api
93
         * @param array<string, string> $translations
94
         */
95
        public function getVariant(string $message, array $translations, Plural $plural): string
1✔
96
        {
97
                if (!array_key_exists($plural->value, $translations)) {
1✔
98
                        $this->warn(
1✔
99
                                'Plural form not defined. (message: %s, form: %s)',
1✔
100
                                $message,
101
                                $plural->value,
1✔
102
                        );
103
                }
104

105
                return $translations[$plural->value] ?? $translations[array_key_last($translations)] ?? $message;
1✔
106
        }
107

108
        protected function untranslated(string $message): void
1✔
109
        {
110
                $this->diagnostics?->untranslated($message);
1✔
111
        }
1✔
112

113
        protected function warn(string $message, float|int|string ...$parameters): void
1✔
114
        {
115
                if (!empty($parameters)) {
1✔
116
                        $message = @vsprintf($message, $parameters);
1✔
117
                } // Intentionally @ as parameter count can mismatch
118

119
                $this->diagnostics?->warning($message);
1✔
120
        }
1✔
121
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc