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

bckp / translator-core / 14318714403

07 Apr 2025 08:21PM UTC coverage: 98.122% (-0.5%) from 98.585%
14318714403

push

github

bckp
Added getLocale method to the translator

0 of 1 new or added line in 1 file covered. (0.0%)

209 of 213 relevant lines covered (98.12%)

0.98 hits per line

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

92.86
/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✔
44
                        ['%label', '%value', '%name'],
×
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 getLocale(): string
64
        {
NEW
65
                return $this->catalogue->locale();
×
66
        }
67

68
        /**
69
         * @api
70
         */
71
        #[\Override]
72
        public function translate(string|Stringable $message, float|int|string ...$parameters): string
1✔
73
        {
74
                $message = (string) $message;
1✔
75

76
                if (empty($message)) {
1✔
77
                        return '';
1✔
78
                }
79

80
                $translation = $this->catalogue->get($message);
1✔
81
                if (!$translation) {
1✔
82
                        $this->untranslated($message);
1✔
83
                        return $message;
1✔
84
                }
85

86
                // Plural option is returned, we need to choose the right one
87
                if (is_array($translation)) {
1✔
88
                        $plural = is_numeric($parameters[0] ?? null) ? $this->catalogue->plural((int) $parameters[0]) : Plural::Other;
1✔
89
                        $translation = $this->getVariant($message, $translation, $plural);
1✔
90
                }
91

92
                if (!empty($parameters)) {
1✔
93
                        $translation = ($this->normalizeCallback)($translation);
1✔
94
                        $translation = @vsprintf($translation, $parameters);
1✔
95
                }
96

97
                return $translation;
1✔
98
        }
99

100
        /**
101
         * @api
102
         * @param array<string, string> $translations
103
         */
104
        public function getVariant(string $message, array $translations, Plural $plural): string
1✔
105
        {
106
                if (!array_key_exists($plural->value, $translations)) {
1✔
107
                        $this->warn(
1✔
108
                                'Plural form not defined. (message: %s, form: %s)',
1✔
109
                                $message,
110
                                $plural->value,
1✔
111
                        );
112
                }
113

114
                return $translations[$plural->value] ?? $translations[array_key_last($translations)] ?? $message;
1✔
115
        }
116

117
        protected function untranslated(string $message): void
1✔
118
        {
119
                $this->diagnostics?->untranslated($message);
1✔
120
        }
1✔
121

122
        protected function warn(string $message, float|int|string ...$parameters): void
1✔
123
        {
124
                if (!empty($parameters)) {
1✔
125
                        $message = @vsprintf($message, $parameters);
1✔
126
                } // Intentionally @ as parameter count can mismatch
127

128
                $this->diagnostics?->warning($message);
1✔
129
        }
1✔
130
}
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