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

drago-ex / translator / 20815936187

08 Jan 2026 11:54AM UTC coverage: 64.384% (+0.5%) from 63.889%
20815936187

push

github

scrs_zdenek
Update

47 of 73 relevant lines covered (64.38%)

1.29 hits per line

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

70.37
/src/Drago/Localization/Translator.php
1
<?php
2

3
/**
4
 * Drago Extension
5
 * Package built on Nette Framework
6
 */
7

8
declare(strict_types=1);
9

10
namespace Drago\Localization;
11

12
use Nette\Localization\Translator as ITranslator;
13
use Nette\Neon\Exception;
14
use Nette\Neon\Neon;
15
use Throwable;
16

17

18
/**
19
 * NEON-based translator implementation.
20
 *
21
 * Supports multiple translation sources:
22
 * - Single translation directory (manual)
23
 * - Automatic finder scanning the whole app directory
24
 *
25
 * Later directories override earlier ones.
26
 */
27
class Translator implements ITranslator
28
{
29
        /** @var array<string, string> */
30
        private array $messages = [];
31

32
        /** @var string[] */
33
        private array $translateDirs = [];
34

35

36
        /**
37
         * @param Options $options Translator configuration
38
         * @param TranslatorFinder $translatorFinder Finder service (used if autoFinder is enabled)
39
         */
40
        public function __construct(
2✔
41
                private readonly Options $options,
42
                private readonly TranslatorFinder $translatorFinder,
43
        ) {
44
                if (!$options->autoFinder) {
2✔
45
                        foreach ($options->translateDir as $dir) {
2✔
46
                                $this->addTranslateDir($dir);
2✔
47
                        }
48
                }
49
        }
2✔
50

51

52
        /**
53
         * Add a custom translation directory.
54
         *
55
         * @param string $dir Absolute path to translation directory
56
         */
57
        public function addTranslateDir(string $dir): void
2✔
58
        {
59
                if (!is_dir($dir)) {
2✔
60
                        return;
2✔
61
                }
62

63
                if (!in_array($dir, $this->translateDirs, true)) {
×
64
                        $this->translateDirs[] = $dir;
×
65
                }
66
        }
67

68

69
        /**
70
         * Loads translations for the given language.
71
         *
72
         * @param string $lang Language code (e.g. 'cs', 'en')
73
         * @return array<string, string>
74
         * @throws Exception
75
         * @throws Throwable
76
         */
77
        public function setTranslate(string $lang): array
2✔
78
        {
79
                $this->messages = [];
2✔
80
                $translateFiles = $this->options->autoFinder
2✔
81
                        ? $this->translatorFinder->findFiles($lang)
×
82
                        : array_map(fn($dir) => $dir . '/' . $lang . '.neon', $this->translateDirs);
2✔
83

84
                $this->loadTranslateFiles($translateFiles);
2✔
85
                return $this->messages;
2✔
86
        }
87

88

89
        /**
90
         * Helper method to load NEON files and merge messages.
91
         *
92
         * @param string[] $files
93
         * @throws Exception
94
         */
95
        private function loadTranslateFiles(array $files): void
2✔
96
        {
97
                foreach ($files as $file) {
2✔
98
                        if (!is_file($file)) {
×
99
                                continue;
×
100
                        }
101

102
                        $data = Neon::decodeFile($file);
×
103
                        if (is_array($data)) {
×
104
                                $this->messages = array_merge($this->messages, $data);
×
105
                        }
106
                }
107
        }
2✔
108

109

110
        /**
111
         * Translate a message.
112
         */
113
        public function translate(mixed $message, mixed ...$parameters): string
2✔
114
        {
115
                return $this->messages[$message] ?? (string) $message;
2✔
116
        }
117
}
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