Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/Plugins/TranslatorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace InterNACHI\Modular\Plugins;

use Illuminate\Contracts\Translation\Translator;
use Illuminate\Support\Collection;
use Illuminate\Translation\Translator;
use InterNACHI\Modular\Plugins\Attributes\AfterResolving;
use InterNACHI\Modular\Support\FinderFactory;
use InterNACHI\Modular\Support\ModuleFileInfo;
Expand Down Expand Up @@ -31,8 +31,13 @@ public function discover(FinderFactory $finders): iterable
public function handle(Collection $data): void
{
$data->each(function(array $row) {
$this->translator->addNamespace($row['namespace'], $row['path']);
$this->translator->addJsonPath($row['path']);
if (method_exists($this->translator, 'addNamespace')) {
$this->translator->addNamespace($row['namespace'], $row['path']);
}

if (method_exists($this->translator, 'addJsonPath')) {
$this->translator->addJsonPath($row['path']);
}
});
}
}
36 changes: 36 additions & 0 deletions tests/Plugins/TranslatorPluginTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace InterNACHI\Modular\Tests\Plugins;

use Illuminate\Contracts\Translation\Translator as TranslatorContract;
use Illuminate\Support\Facades\Lang;
use InterNACHI\Modular\Tests\Concerns\PreloadsAppModules;
use InterNACHI\Modular\Tests\TestCase;
Expand Down Expand Up @@ -41,4 +42,39 @@ public function test_json_translations_are_loaded(): void
$translator->get('Hello')
);
}

public function test_custom_translators_that_do_not_extend_the_base_translator_are_supported(): void
{
$this->app->forgetInstance('translator');
$this->app->singleton('translator', fn() => new CustomTranslator());

$translator = $this->app->make('translator');

$this->assertInstanceOf(CustomTranslator::class, $translator);
}
}

class CustomTranslator implements TranslatorContract
{
protected string $locale = 'en';

public function get($key, array $replace = [], $locale = null)
{
return $key;
}

public function choice($key, $number, array $replace = [], $locale = null)
{
return $key;
}

public function getLocale()
{
return $this->locale;
}

public function setLocale($locale)
{
$this->locale = $locale;
}
}