@@ -16,14 +16,14 @@ class Locales implements Arrayable, ArrayAccess
1616 protected $ config ;
1717
1818 /**
19- * @var TranslatorContract
19+ * @var array
2020 */
21- protected $ translator ;
21+ protected $ locales = [] ;
2222
2323 /**
24- * @var array
24+ * @var TranslatorContract
2525 */
26- protected $ locales = [] ;
26+ protected $ translator ;
2727
2828 public function __construct (ConfigContract $ config , TranslatorContract $ translator )
2929 {
@@ -33,26 +33,9 @@ public function __construct(ConfigContract $config, TranslatorContract $translat
3333 $ this ->load ();
3434 }
3535
36- public function load ( ): void
36+ public function add ( string $ locale ): void
3737 {
38- $ localesConfig = (array ) $ this ->config ->get ('translatable.locales ' , []);
39-
40- if (empty ($ localesConfig )) {
41- throw new LocalesNotDefinedException ('Please make sure you have run "php artisan config:publish astrotomic/laravel-translatable" and that the locales configuration is defined. ' );
42- }
43-
44- $ this ->locales = [];
45- foreach ($ localesConfig as $ key => $ locale ) {
46- if (is_string ($ key ) && is_array ($ locale )) {
47- $ this ->locales [$ key ] = $ key ;
48- foreach ($ locale as $ country ) {
49- $ countryLocale = $ this ->getCountryLocale ($ key , $ country );
50- $ this ->locales [$ countryLocale ] = $ countryLocale ;
51- }
52- } elseif (is_string ($ locale )) {
53- $ this ->locales [$ locale ] = $ locale ;
54- }
55- }
38+ $ this ->locales [$ locale ] = $ locale ;
5639 }
5740
5841 public function all (): array
@@ -65,49 +48,61 @@ public function current()
6548 return $ this ->config ->get ('translatable.locale ' ) ?: $ this ->translator ->getLocale ();
6649 }
6750
68- public function has (string $ locale ): bool
51+ public function forget (string $ locale ): void
6952 {
70- return isset ($ this ->locales [$ locale ]);
53+ unset ($ this ->locales [$ locale ]);
7154 }
7255
7356 public function get (string $ locale ): ?string
7457 {
7558 return $ this ->locales [$ locale ] ?? null ;
7659 }
7760
78- public function add (string $ locale ): void
61+ public function getCountryLocale (string $ locale, string $ country ): string
7962 {
80- $ this ->locales [ $ locale ] = $ locale ;
63+ return $ locale . $ this ->getLocaleSeparator (). $ country ;
8164 }
8265
83- public function forget (string $ locale ): void
66+ public function getLanguageFromCountryBasedLocale (string $ locale ): string
8467 {
85- unset ($ this ->locales [ $ locale]) ;
68+ return explode ($ this ->getLocaleSeparator (), $ locale)[ 0 ] ;
8669 }
8770
8871 public function getLocaleSeparator (): string
8972 {
9073 return $ this ->config ->get ('translatable.locale_separator ' ) ?: '- ' ;
9174 }
9275
93- public function getCountryLocale (string $ locale, string $ country ): string
76+ public function has (string $ locale ): bool
9477 {
95- return $ locale . $ this ->getLocaleSeparator (). $ country ;
78+ return isset ( $ this ->locales [ $ locale ]) ;
9679 }
9780
9881 public function isLocaleCountryBased (string $ locale ): bool
9982 {
10083 return strpos ($ locale , $ this ->getLocaleSeparator ()) !== false ;
10184 }
10285
103- public function getLanguageFromCountryBasedLocale ( string $ locale ): string
86+ public function load ( ): void
10487 {
105- return explode ($ this ->getLocaleSeparator (), $ locale )[0 ];
106- }
88+ $ localesConfig = (array ) $ this ->config ->get ('translatable.locales ' , []);
10789
108- public function toArray (): array
109- {
110- return $ this ->all ();
90+ if (empty ($ localesConfig )) {
91+ throw new LocalesNotDefinedException ('Please make sure you have run "php artisan config:publish astrotomic/laravel-translatable" and that the locales configuration is defined. ' );
92+ }
93+
94+ $ this ->locales = [];
95+ foreach ($ localesConfig as $ key => $ locale ) {
96+ if (is_string ($ key ) && is_array ($ locale )) {
97+ $ this ->locales [$ key ] = $ key ;
98+ foreach ($ locale as $ country ) {
99+ $ countryLocale = $ this ->getCountryLocale ($ key , $ country );
100+ $ this ->locales [$ countryLocale ] = $ countryLocale ;
101+ }
102+ } elseif (is_string ($ locale )) {
103+ $ this ->locales [$ locale ] = $ locale ;
104+ }
105+ }
111106 }
112107
113108 public function offsetExists ($ key ): bool
@@ -133,4 +128,9 @@ public function offsetUnset($key)
133128 {
134129 $ this ->forget ($ key );
135130 }
131+
132+ public function toArray (): array
133+ {
134+ return $ this ->all ();
135+ }
136136}
0 commit comments