Skip to content

Commit c103c8b

Browse files
authored
Merge pull request #5 from SolumDeSignum/dev
Dev
2 parents 3bacd0c + 97e61ff commit c103c8b

File tree

4 files changed

+125
-93
lines changed

4 files changed

+125
-93
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
},
4040
"autoload-dev": {
4141
"psr-4": {
42-
"SolumDeSignum\\PackageTranslatorLoader\\Tests\\": "tests"
42+
"SolumDeSignum\\PackageTranslatorLoader\\Tests\\": "tests/"
4343
}
4444
},
4545
"extra": {

src/PackageTranslatorLoader.php

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,18 @@
88
use Illuminate\Filesystem\Filesystem;
99
use Illuminate\Translation\FileLoader;
1010
use Illuminate\Translation\Translator;
11+
use Psr\Container\ContainerExceptionInterface;
12+
use Psr\Container\NotFoundExceptionInterface;
1113

1214
class PackageTranslatorLoader
1315
{
14-
public array $config;
15-
1616
public ?string $locale = null;
1717

1818
public string $translator;
1919

20-
public Application $app;
21-
2220
public function __construct(
23-
Application $app,
24-
array $config = [
21+
public Application $app,
22+
public array $config = [
2523
'translator' => 'package-translation-loader.translator',
2624
'nameSpace' => 'solumdesignum/package-translation-loader',
2725
'packageRootPath' => __DIR__ . '/..',
@@ -30,36 +28,26 @@ public function __construct(
3028
],
3129
?string $locale = null
3230
) {
33-
$this->app = $app;
34-
$this->config = $config;
3531
$this->translator = $this->config['translator'];
3632
$this->localeSetter($locale);
3733
$this->loadTranslations();
3834
}
3935

4036
public function localeSetter(?string $locale): void
4137
{
42-
if ($locale !== null) {
43-
$this->locale = $locale;
44-
} else {
45-
$this->setLocale();
46-
}
38+
$this->locale = $locale ?? $this->setLocale()->locale;
4739
}
4840

4941
public function setLocale(?string $locale = null): self
5042
{
51-
$this->locale = $locale;
52-
43+
$this->locale = $locale ?? $this->app->getLocale();
5344
return $this;
5445
}
5546

5647
/**
5748
* Custom Translation Loader
58-
* When registering the translator component, we'll need to set the default
59-
* locale as well as the fallback locale. So, we'll grab the application
60-
* configuration so we can easily get both of these values from there.
61-
*
62-
* @return void
49+
* Registers the translator component with the application.
50+
* Sets the locale and fallback locale.
6351
*/
6452
public function loadTranslations(): void
6553
{
@@ -68,44 +56,58 @@ public function loadTranslations(): void
6856
function ($app) {
6957
$trans = new Translator(
7058
$this->loader(),
71-
(string)($this->locale !== null
72-
? $this->locale
73-
: $this->locale($app))
74-
);
75-
$this->locale !== null
76-
? $this->locale
77-
: $trans->setFallback(
78-
$app['config']['app.fallback_locale']
59+
$this->locale ?? $this->locale($app)
7960
);
61+
if ($this->locale !== null) {
62+
$trans->setFallback($app['config']['app.fallback_locale']);
63+
}
8064
$trans->addNamespace(
8165
$this->config['nameSpace'],
82-
__DIR__ .
83-
$this->config['loadLangPath']
66+
$this->config['packageRootPath'] . $this->config['loadLangPath']
8467
);
8568

8669
return $trans;
8770
}
8871
);
8972
}
9073

74+
/**
75+
* Creates and returns a FileLoader instance.
76+
*
77+
* @return FileLoader
78+
*/
9179
public function loader(): FileLoader
9280
{
9381
$filesystem = new Filesystem();
9482
$resourcesLangPath = $this->config['packageRootPath'] . $this->config['loaderLangPath'];
95-
$filesystem->allFiles($resourcesLangPath);
83+
84+
if (!is_dir($resourcesLangPath)) {
85+
throw new \RuntimeException("Translation directory not found: $resourcesLangPath");
86+
}
9687

9788
return new FileLoader($filesystem, $resourcesLangPath);
9889
}
9990

91+
/**
92+
* Determines the locale from the request or defaults to the application locale.
93+
*
94+
* @param Application $app
95+
* @return string
96+
* @throws ContainerExceptionInterface
97+
* @throws NotFoundExceptionInterface
98+
*/
10099
public function locale(Application $app): string
101100
{
102-
return $app
103-
->get('request')
104-
->segment(
105-
config('package-translator-loader.segment', 1)
106-
) ?: $app->getLocale();
101+
return $app->get('request')
102+
->segment(config('package-translator-loader.segment', 1))
103+
?: $app->getLocale();
107104
}
108105

106+
/**
107+
* Retrieves the translator instance from the service container.
108+
*
109+
* @return mixed
110+
*/
109111
public function trans(): mixed
110112
{
111113
return app($this->translator);
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SolumDeSignum\PackageTranslatorLoader\Tests\Feature;
6+
7+
use Illuminate\Foundation\Application;
8+
use Illuminate\Support\Facades\Config;
9+
use Orchestra\Testbench\TestCase;
10+
use SolumDeSignum\PackageTranslatorLoader\PackageTranslatorLoaderServiceProvider;
11+
12+
class PackageTranslatorLoaderFeatureTest extends TestCase
13+
{
14+
/**
15+
* @test
16+
*/
17+
public function itTranslatesTextInEnglish(): void
18+
{
19+
$translator = $this->app->get('package-translator-loader');
20+
$translator->setLocale('en');
21+
$translation = $translator->trans()->get('package-translator-loader.run');
22+
23+
$this->assertEquals('I am running.', $translation);
24+
}
25+
26+
/**
27+
* @test
28+
*/
29+
public function itTranslatesTextInLatvian(): void
30+
{
31+
$translator = $this->app->get('package-translator-loader');
32+
$translator->setLocale('lv');
33+
$translation = $translator->trans()->get('package-translator-loader.run');
34+
35+
$this->assertEquals('Es strādāju.', $translation);
36+
}
37+
38+
/**
39+
* @test
40+
*/
41+
public function itSetsAndRetrievesLocaleCorrectly(): void
42+
{
43+
$translator = $this->app->get('package-translator-loader');
44+
$translator->setLocale('fr');
45+
46+
$this->assertEquals('fr', $translator->locale);
47+
}
48+
49+
/**
50+
* @test
51+
*/
52+
public function itFallsBackToDefaultLocale(): void
53+
{
54+
Config::set('app.fallback_locale', 'en');
55+
56+
$translator = $this->app->get('package-translator-loader');
57+
$translator->setLocale('invalid-locale');
58+
$translation = $translator->trans()->get('package-translator-loader.run');
59+
60+
$this->assertEquals('I am running.', $translation);
61+
}
62+
63+
/**
64+
* @test
65+
*/
66+
public function itUsesDefaultLocaleWhenNoLocaleIsSet(): void
67+
{
68+
$translator = $this->app->get('package-translator-loader');
69+
$translator->setLocale(null); // Or leave it unset
70+
71+
$translation = $translator->trans()->get('package-translator-loader.run');
72+
$this->assertEquals('I am running.', $translation); // Assuming default locale is 'en'
73+
}
74+
75+
/**
76+
* @param Application $app
77+
*
78+
* @return array
79+
*/
80+
protected function getPackageProviders($app): array
81+
{
82+
return [
83+
PackageTranslatorLoaderServiceProvider::class,
84+
];
85+
}
86+
}

tests/Unit/PackageTranslatorLoaderSingletonTest.php

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)