Skip to content

Commit 49ead30

Browse files
Merge pull request #43 from hexadog/develop
feat(lang): add Theme lang
2 parents 3443394 + 84b79a9 commit 49ead30

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

resources/stubs/_folder-structure/lang/en/.gitkeep

Whitespace-only changes.

src/Theme.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Hexadog\ThemesManager\Events\ThemeEnabling;
99
use Hexadog\ThemesManager\Traits\ComposerTrait;
1010
use Illuminate\Container\Container;
11+
use Illuminate\Contracts\Translation\Translator;
1112
use Illuminate\Filesystem\Filesystem;
1213
use Illuminate\Mail\Markdown;
1314
use Illuminate\Support\Facades\Config;
@@ -127,6 +128,29 @@ public function getViewPaths($path = ''): array
127128
return array_reverse($paths);
128129
}
129130

131+
/**
132+
* Get theme translations paths.
133+
*
134+
* @param string $path
135+
*/
136+
public function getTransaltionPaths($path = ''): array
137+
{
138+
// Build Paths array.
139+
// All paths are relative to Config::get('themes-manager.directory')
140+
$paths = [];
141+
$theme = $this;
142+
143+
do {
144+
$translationsPath = $theme->getPath('lang' . ($path ? "/{$path}" : ''));
145+
146+
if (File::exists($translationsPath) && !in_array($translationsPath, $paths)) {
147+
$paths[] = $translationsPath;
148+
}
149+
} while ($theme = $theme->getParent());
150+
151+
return array_reverse($paths);
152+
}
153+
130154
/**
131155
* Set path.
132156
*
@@ -244,6 +268,7 @@ public function enable(bool $withEvent = true, $defaultViewPaths = null, $defaul
244268

245269
$this->setStatus(true);
246270
$this->registerViews($defaultViewPaths, $defaultMailViewPaths);
271+
$this->registerTranlastions();
247272

248273
if ($withEvent) {
249274
event(new ThemeEnabled($this->getName()));
@@ -365,6 +390,24 @@ protected function createPublicAssetsStructure()
365390
}
366391
}
367392

393+
/**
394+
* Register theme's translations.
395+
*/
396+
protected function registerTranlastions()
397+
{
398+
// Register Translation paths
399+
$paths = $this->getTransaltionPaths();
400+
$translator = app()->make(Translator::class);
401+
402+
$paths = array_map(function ($path) use ($translator) {
403+
$path = rtrim($path, DIRECTORY_SEPARATOR);
404+
405+
$translator->addNamespace('theme', $path);
406+
407+
return $path;
408+
}, $paths);
409+
}
410+
368411
/**
369412
* Register theme's views in ViewFinder.
370413
*

0 commit comments

Comments
 (0)