Skip to content

Commit 42b0603

Browse files
committed
include to eval
1 parent 66be241 commit 42b0603

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

src/Facades/Translator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* @method static array getDeadTranslations(string $locale, string $namespace, ?SearchCodeServiceInterface $service = null, null|(Closure(string $file, string[] $translations):void) $progress = null, ?array $ignore = null )
2222
* @method static void sortAllTranslations()
2323
* @method static \Elegantly\Translator\Translator clearCache()
24+
* @method static Translations setTranslations(string $locale, string $namespace, array $values)
25+
* @method static Translations deleteTranslations(string $locale, string $namespace, array $keys)
2426
*
2527
* @see \Elegantly\Translator\Translator
2628
*/

src/Translator.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,33 @@ public function getNamespaces(string $locale): array
5151
->toArray();
5252
}
5353

54+
/**
55+
* This function uses eval and not include
56+
* Because using 'include' would cache/compile the code in opcache
57+
* Therefore it would not reflect the changes after the file is edited
58+
*/
5459
public function getTranslations(string $locale, string $namespace): Translations
5560
{
56-
$path = "{$locale}/{$namespace}.php";
57-
58-
if ($this->storage->exists($path)) {
61+
if ($content = $this->getTranslationsFileContent($locale, $namespace)) {
5962
return new Translations(
60-
items: include $this->storage->path($path),
63+
items: eval('?>'.$content),
6164
);
6265
}
6366

6467
return new Translations;
6568
}
6669

70+
public function getTranslationsFileContent(string $locale, string $namespace): ?string
71+
{
72+
$path = "{$locale}/{$namespace}.php";
73+
74+
if ($this->storage->exists($path)) {
75+
return $this->storage->get($path);
76+
}
77+
78+
return null;
79+
}
80+
6781
/**
6882
* Return all the translations keys present in the reference locale but not in the other ones
6983
*

0 commit comments

Comments
 (0)