Skip to content
This repository was archived by the owner on Jun 18, 2019. It is now read-only.

Commit 9906a7c

Browse files
committed
complete locales helper class logic
1 parent 7b2462e commit 9906a7c

File tree

2 files changed

+309
-19
lines changed

2 files changed

+309
-19
lines changed

src/Translatable/Locales.php

Lines changed: 79 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
namespace Dimsav\Translatable;
44

5+
use ArrayAccess;
56
use Dimsav\Translatable\Exception\LocalesNotDefinedException;
67
use Illuminate\Contracts\Config\Repository as ConfigContract;
8+
use Illuminate\Contracts\Support\Arrayable;
79
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
810

9-
class Locales
11+
class Locales implements Arrayable, ArrayAccess
1012
{
1113
/**
1214
* @var ConfigContract
@@ -27,38 +29,72 @@ public function __construct(ConfigContract $config, TranslatorContract $translat
2729
{
2830
$this->config = $config;
2931
$this->translator = $translator;
32+
33+
$this->load();
3034
}
3135

32-
public function getLocales(): array
36+
public function load(): void
3337
{
34-
if (empty($this->locales)) {
35-
$localesConfig = (array)$this->config->get('translatable.locales', []);
38+
$localesConfig = (array) $this->config->get('translatable.locales', []);
3639

37-
if (empty($localesConfig)) {
38-
throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" and that the locales configuration is defined.');
39-
}
40+
if (empty($localesConfig)) {
41+
throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" and that the locales configuration is defined.');
42+
}
4043

41-
foreach ($localesConfig as $key => $locale) {
42-
if (is_array($locale)) {
43-
$this->locales[] = $key;
44-
foreach ($locale as $countryLocale) {
45-
$this->locales[] = $key . $this->getLocaleSeparator() . $countryLocale;
46-
}
47-
} else {
48-
$this->locales[] = $locale;
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;
4951
}
52+
} else {
53+
$this->locales[$locale] = $locale;
5054
}
5155
}
56+
}
5257

53-
54-
return $this->locales;
58+
public function all(): array
59+
{
60+
return array_values($this->locales);
5561
}
5662

5763
public function current()
5864
{
5965
return $this->config->get('translatable.locale') ?: $this->translator->getLocale();
6066
}
6167

68+
public function has(string $locale): bool
69+
{
70+
return isset($this->locales[$locale]);
71+
}
72+
73+
public function get(string $locale): ?string
74+
{
75+
return $this->locales[$locale] ?? null;
76+
}
77+
78+
public function add(string $locale): void
79+
{
80+
$this->locales[$locale] = $locale;
81+
}
82+
83+
public function forget(string $locale): void
84+
{
85+
unset($this->locales[$locale]);
86+
}
87+
88+
public function getLocaleSeparator(): string
89+
{
90+
return $this->config->get('translatable.locale_separator') ?: '-';
91+
}
92+
93+
public function getCountryLocale(string $locale, string $country): string
94+
{
95+
return $locale . $this->getLocaleSeparator() . $country;
96+
}
97+
6298
public function isLocaleCountryBased(string $locale): bool
6399
{
64100
return strpos($locale, $this->getLocaleSeparator()) !== false;
@@ -69,8 +105,32 @@ public function getLanguageFromCountryBasedLocale(string $locale): string
69105
return explode($this->getLocaleSeparator(), $locale)[0];
70106
}
71107

72-
protected function getLocaleSeparator(): string
108+
public function toArray(): array
109+
{
110+
return $this->all();
111+
}
112+
113+
public function offsetExists($key): bool
114+
{
115+
return $this->has($key);
116+
}
117+
118+
public function offsetGet($key): ?string
119+
{
120+
return $this->get($key);
121+
}
122+
123+
public function offsetSet($key, $value)
124+
{
125+
if (is_string($key) && is_string($value)) {
126+
$this->add($this->getCountryLocale($key, $value));
127+
} elseif (is_string($value)) {
128+
$this->add($value);
129+
}
130+
}
131+
132+
public function offsetUnset($key)
73133
{
74-
return $this->config->get('translatable.locale_separator', '-');
134+
$this->forget($key);
75135
}
76136
}

tests/LocalesTest.php

Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
<?php
2+
3+
use Dimsav\Translatable\Locales;
4+
5+
class LocalesTest extends TestsBase
6+
{
7+
public function test_singleton()
8+
{
9+
$this->assertSame(spl_object_id($this->app->make('translatable.locales')), spl_object_id($this->app->make('translatable.locales')));
10+
$this->assertSame(spl_object_id($this->app->make(Locales::class)), spl_object_id($this->app->make(Locales::class)));
11+
$this->assertSame(spl_object_id($this->app->make('translatable.locales')), spl_object_id($this->app->make(Locales::class)));
12+
}
13+
14+
public function test_load()
15+
{
16+
$this->app['config']->set('translatable.locales', [
17+
'de',
18+
]);
19+
$this->assertEquals(['de'], $this->app->make('translatable.locales')->all());
20+
21+
$this->app['config']->set('translatable.locales', [
22+
'de',
23+
'en',
24+
]);
25+
$this->assertEquals(['de'], $this->app->make('translatable.locales')->all());
26+
$this->app->make('translatable.locales')->load();
27+
$this->assertEquals(['de', 'en'], $this->app->make('translatable.locales')->all());
28+
}
29+
30+
public function test_all_language_locales()
31+
{
32+
$this->app['config']->set('translatable.locales', [
33+
'el',
34+
'en',
35+
'fr',
36+
'de',
37+
'id',
38+
]);
39+
40+
$this->assertEquals(['el', 'en', 'fr', 'de', 'id'], $this->app->make('translatable.locales')->all());
41+
}
42+
43+
public function test_all_country_locales()
44+
{
45+
$this->app['config']->set('translatable.locales', [
46+
'en' => [
47+
'GB',
48+
'US',
49+
],
50+
'de' => [
51+
'DE',
52+
'CH',
53+
],
54+
]);
55+
56+
$this->assertEquals(['en', 'en-GB', 'en-US', 'de', 'de-DE', 'de-CH'], $this->app->make('translatable.locales')->all());
57+
}
58+
59+
public function test_to_array()
60+
{
61+
$this->app['config']->set('translatable.locales', [
62+
'el',
63+
'en',
64+
'fr',
65+
'de',
66+
'id',
67+
]);
68+
69+
$this->assertEquals(['el', 'en', 'fr', 'de', 'id'], $this->app->make('translatable.locales')->toArray());
70+
}
71+
72+
public function test_current_config()
73+
{
74+
$this->app['config']->set('translatable.locale', 'de');
75+
76+
$this->assertEquals('de', $this->app->make('translatable.locales')->current());
77+
}
78+
79+
public function test_current_translator()
80+
{
81+
$this->app['config']->set('translatable.locale', null);
82+
$this->app['translator']->setLocale('en');
83+
84+
$this->assertEquals('en', $this->app->make('translatable.locales')->current());
85+
}
86+
87+
public function test_has()
88+
{
89+
$this->app['config']->set('translatable.locales', [
90+
'el',
91+
'en',
92+
'fr',
93+
'de',
94+
'id',
95+
]);
96+
97+
$this->assertTrue($this->app->make('translatable.locales')->has('de'));
98+
$this->assertFalse($this->app->make('translatable.locales')->has('jp'));
99+
}
100+
101+
public function test_offset_exists()
102+
{
103+
$this->app['config']->set('translatable.locales', [
104+
'el',
105+
'en',
106+
'fr',
107+
'de',
108+
'id',
109+
]);
110+
111+
$this->assertTrue(isset($this->app->make('translatable.locales')['de']));
112+
$this->assertFalse(isset($this->app->make('translatable.locales')['jp']));
113+
}
114+
115+
public function test_get()
116+
{
117+
$this->app['config']->set('translatable.locales', [
118+
'el',
119+
'en',
120+
'fr',
121+
'de',
122+
'id',
123+
]);
124+
125+
$this->assertEquals('de', $this->app->make('translatable.locales')->get('de'));
126+
$this->assertNull($this->app->make('translatable.locales')->get('jp'));
127+
}
128+
129+
public function test_offset_get()
130+
{
131+
$this->app['config']->set('translatable.locales', [
132+
'el',
133+
'en',
134+
'fr',
135+
'de',
136+
'id',
137+
]);
138+
139+
$this->assertEquals('de', $this->app->make('translatable.locales')['de']);
140+
$this->assertNull($this->app->make('translatable.locales')['jp']);
141+
}
142+
143+
public function test_add_language_locale()
144+
{
145+
$this->app['config']->set('translatable.locales', [
146+
'de',
147+
]);
148+
$this->assertTrue($this->app->make('translatable.locales')->has('de'));
149+
$this->assertFalse($this->app->make('translatable.locales')->has('en'));
150+
$this->app->make('translatable.locales')->add('en');
151+
$this->assertTrue($this->app->make('translatable.locales')->has('en'));
152+
}
153+
154+
public function test_offset_set_language_locale()
155+
{
156+
$this->app['config']->set('translatable.locales', [
157+
'de',
158+
]);
159+
$this->assertTrue($this->app->make('translatable.locales')->has('de'));
160+
$this->assertFalse($this->app->make('translatable.locales')->has('en'));
161+
$this->app->make('translatable.locales')[] = 'en';
162+
$this->assertTrue($this->app->make('translatable.locales')->has('en'));
163+
}
164+
165+
public function test_offset_set_country_locale()
166+
{
167+
$this->app['config']->set('translatable.locales', [
168+
'de',
169+
]);
170+
$this->assertTrue($this->app->make('translatable.locales')->has('de'));
171+
$this->assertFalse($this->app->make('translatable.locales')->has('de-AT'));
172+
$this->app->make('translatable.locales')['de'] = 'AT';
173+
$this->assertTrue($this->app->make('translatable.locales')->has('de-AT'));
174+
}
175+
176+
public function test_forget()
177+
{
178+
$this->app['config']->set('translatable.locales', [
179+
'de',
180+
'en',
181+
]);
182+
$this->assertTrue($this->app->make('translatable.locales')->has('de'));
183+
$this->assertTrue($this->app->make('translatable.locales')->has('en'));
184+
$this->app->make('translatable.locales')->forget('en');
185+
$this->assertFalse($this->app->make('translatable.locales')->has('en'));
186+
}
187+
188+
public function test_offset_unset()
189+
{
190+
$this->app['config']->set('translatable.locales', [
191+
'de',
192+
'en',
193+
]);
194+
$this->assertTrue($this->app->make('translatable.locales')->has('de'));
195+
$this->assertTrue($this->app->make('translatable.locales')->has('en'));
196+
unset($this->app->make('translatable.locales')['en']);
197+
$this->assertFalse($this->app->make('translatable.locales')->has('en'));
198+
}
199+
200+
public function test_get_locale_separator_config()
201+
{
202+
$this->app['config']->set('translatable.locale_separator', '_');
203+
204+
$this->assertEquals('_', $this->app->make('translatable.locales')->getLocaleSeparator());
205+
}
206+
207+
public function test_get_locale_separator_default()
208+
{
209+
$this->app['config']->set('translatable.locale_separator', null);
210+
211+
$this->assertEquals('-', $this->app->make('translatable.locales')->getLocaleSeparator());
212+
}
213+
214+
public function test_get_country_locale()
215+
{
216+
$this->assertEquals('de-AT', $this->app->make('translatable.locales')->getCountryLocale('de', 'AT'));
217+
}
218+
219+
public function test_is_locale_country_based()
220+
{
221+
$this->assertTrue($this->app->make('translatable.locales')->isLocaleCountryBased('de-AT'));
222+
$this->assertFalse($this->app->make('translatable.locales')->isLocaleCountryBased('de'));
223+
}
224+
225+
public function test_get_language_from_country_based_locale()
226+
{
227+
$this->assertEquals('de', $this->app->make('translatable.locales')->getLanguageFromCountryBasedLocale('de-AT'));
228+
$this->assertEquals('de', $this->app->make('translatable.locales')->getLanguageFromCountryBasedLocale('de'));
229+
}
230+
}

0 commit comments

Comments
 (0)