ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
Translator.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
24
29class Translator
30{
31 private bool $internal = false;
35 private const LANG_PHP = 'lang.php';
36
37 private MultiLanguage $multi_language;
38
39 public function __construct(
40 private \ilLanguage $lng,
41 ?TranslationsRepository $translations_repository = null,
42 string ...$module
43 ) {
44 foreach ($module as $m) {
45 $this->lng->loadLanguageModule($m);
46 }
47 $this->multi_language = new MultiLanguage($translations_repository);
48 }
49
50 public function t(string $identifier, ?string $prefix = null, array $subsitutions = []): string
51 {
52 return $this->translate($identifier, $prefix, $subsitutions);
53 }
54
58 public function txt(string $identifier, array $subsitutions = []): string
59 {
60 return $this->translate($identifier, null, $subsitutions);
61 }
62
63 public function translate(string $identifier, ?string $prefix = null, array $subsitutions = []): string
64 {
65 $key = $prefix !== null ? $prefix . '_' . $identifier : $identifier;
66
67 if ($subsitutions !== []) {
68 return sprintf($this->internal($key, $this->lng->txt($key)), ...array_values($subsitutions));
69 }
70
71 return $this->internal($key, $this->lng->txt($key));
72 }
73
74 public function ml(): MultiLanguage
75 {
76 return $this->multi_language;
77 }
78
79 private function internal(string $key, string $translation): string
80 {
81 if (!$this->internal) {
82 return $translation;
83 }
84
85 $file = __DIR__ . '/' . self::LANG_PHP;
86 touch($file);
87 $current = (array) ((@include $file) ?? []);
88 $untranslated = ($translation === '-' . $key . '-');
89
90 $differs = $untranslated
91 || (
92 ($current[$key] ?? null) !== null
93 && ($current[$key] !== $translation)
94 );
95
96 if ($differs) {
97 $current[$key] = $translation = $key;
98 asort($current);
99 file_put_contents($file, '<?php return ' . var_export($current, true) . ';');
100 }
101
102 return $translation;
103 }
104
105}
__construct()
Constructor setup ILIAS global object @access public.
Definition: class.ilias.php:76
language handling
global $lng
Definition: privfeed.php:31