88use Illuminate \Filesystem \Filesystem ;
99use Illuminate \Translation \FileLoader ;
1010use Illuminate \Translation \Translator ;
11+ use Psr \Container \ContainerExceptionInterface ;
12+ use Psr \Container \NotFoundExceptionInterface ;
1113
1214class 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 );
0 commit comments