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

Commit 485b33a

Browse files
authored
Merge pull request #574 from dimsav/ft-locales-helper
add locales helper class
2 parents de9f536 + b9c330c commit 485b33a

6 files changed

+423
-71
lines changed

src/Translatable/Locales.php

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<?php
2+
3+
namespace Dimsav\Translatable;
4+
5+
use ArrayAccess;
6+
use Illuminate\Contracts\Support\Arrayable;
7+
use Dimsav\Translatable\Exception\LocalesNotDefinedException;
8+
use Illuminate\Contracts\Config\Repository as ConfigContract;
9+
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
10+
11+
class Locales implements Arrayable, ArrayAccess
12+
{
13+
/**
14+
* @var ConfigContract
15+
*/
16+
protected $config;
17+
18+
/**
19+
* @var TranslatorContract
20+
*/
21+
protected $translator;
22+
23+
/**
24+
* @var array
25+
*/
26+
protected $locales = [];
27+
28+
public function __construct(ConfigContract $config, TranslatorContract $translator)
29+
{
30+
$this->config = $config;
31+
$this->translator = $translator;
32+
33+
$this->load();
34+
}
35+
36+
public function load(): void
37+
{
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 dimsav/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+
} else {
53+
$this->locales[$locale] = $locale;
54+
}
55+
}
56+
}
57+
58+
public function all(): array
59+
{
60+
return array_values($this->locales);
61+
}
62+
63+
public function current()
64+
{
65+
return $this->config->get('translatable.locale') ?: $this->translator->getLocale();
66+
}
67+
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+
98+
public function isLocaleCountryBased(string $locale): bool
99+
{
100+
return strpos($locale, $this->getLocaleSeparator()) !== false;
101+
}
102+
103+
public function getLanguageFromCountryBasedLocale(string $locale): string
104+
{
105+
return explode($this->getLocaleSeparator(), $locale)[0];
106+
}
107+
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)
133+
{
134+
$this->forget($key);
135+
}
136+
}

src/Translatable/Translatable.php

+17-63
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Illuminate\Database\Query\JoinClause;
88
use Illuminate\Database\Eloquent\Relations\Relation;
99
use Illuminate\Database\Query\Builder as QueryBuilder;
10-
use Dimsav\Translatable\Exception\LocalesNotDefinedException;
1110

1211
trait Translatable
1312
{
@@ -346,26 +345,14 @@ private function getFallbackLocale($locale = null)
346345
return config('translatable.fallback_locale');
347346
}
348347

349-
/**
350-
* @param $locale
351-
*
352-
* @return bool
353-
*/
354-
private function isLocaleCountryBased($locale)
348+
private function isLocaleCountryBased(string $locale): bool
355349
{
356-
return strpos($locale, $this->getLocaleSeparator()) !== false;
350+
return $this->getLocalesHelper()->isLocaleCountryBased($locale);
357351
}
358352

359-
/**
360-
* @param $locale
361-
*
362-
* @return string
363-
*/
364-
private function getLanguageFromCountryBasedLocale($locale)
353+
private function getLanguageFromCountryBasedLocale(string $locale): string
365354
{
366-
$parts = explode($this->getLocaleSeparator(), $locale);
367-
368-
return array_get($parts, 0);
355+
return $this->getLocalesHelper()->getLanguageFromCountryBasedLocale($locale);
369356
}
370357

371358
/**
@@ -390,53 +377,19 @@ public function isTranslationAttribute($key)
390377
return in_array($key, $this->translatedAttributes);
391378
}
392379

393-
/**
394-
* @param string $key
395-
*
396-
* @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException
397-
* @return bool
398-
*/
399-
protected function isKeyALocale($key)
380+
protected function isKeyALocale(string $key): bool
400381
{
401-
$locales = $this->getLocales();
402-
403-
return in_array($key, $locales);
382+
return $this->getLocalesHelper()->has($key);
404383
}
405384

406-
/**
407-
* @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException
408-
* @return array
409-
*/
410-
protected function getLocales()
385+
protected function getLocales(): array
411386
{
412-
$localesConfig = (array) config('translatable.locales');
413-
414-
if (empty($localesConfig)) {
415-
throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish dimsav/laravel-translatable" '.
416-
' and that the locales configuration is defined.');
417-
}
418-
419-
$locales = [];
420-
foreach ($localesConfig as $key => $locale) {
421-
if (is_array($locale)) {
422-
$locales[] = $key;
423-
foreach ($locale as $countryLocale) {
424-
$locales[] = $key.$this->getLocaleSeparator().$countryLocale;
425-
}
426-
} else {
427-
$locales[] = $locale;
428-
}
429-
}
430-
431-
return $locales;
387+
return $this->getLocalesHelper()->all();
432388
}
433389

434-
/**
435-
* @return string
436-
*/
437-
protected function getLocaleSeparator()
390+
protected function getLocaleSeparator(): string
438391
{
439-
return config('translatable.locale_separator', '-');
392+
return $this->getLocalesHelper()->getLocaleSeparator();
440393
}
441394

442395
/**
@@ -778,17 +731,13 @@ private function getTranslationsTable()
778731
return app()->make($this->getTranslationModelName())->getTable();
779732
}
780733

781-
/**
782-
* @return string
783-
*/
784-
protected function locale()
734+
protected function locale(): string
785735
{
786736
if ($this->defaultLocale) {
787737
return $this->defaultLocale;
788738
}
789739

790-
return config('translatable.locale')
791-
?: app()->make('translator')->getLocale();
740+
return $this->getLocalesHelper()->current();
792741
}
793742

794743
/**
@@ -874,4 +823,9 @@ public static function disableAutoloadTranslations()
874823
{
875824
self::$autoloadTranslations = false;
876825
}
826+
827+
protected function getLocalesHelper(): Locales
828+
{
829+
return app(Locales::class);
830+
}
877831
}

src/Translatable/TranslatableServiceProvider.php

+16-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,26 @@ public function boot()
1313
], 'translatable');
1414
}
1515

16-
/**
17-
* Register the service provider.
18-
*/
1916
public function register()
2017
{
2118
$this->mergeConfigFrom(
2219
__DIR__.'/../config/translatable.php', 'translatable'
2320
);
21+
22+
$this->registerTranslatableHelper();
23+
}
24+
25+
public function registerTranslatableHelper()
26+
{
27+
$this->app->singleton('translatable.locales', Locales::class);
28+
$this->app->singleton(Locales::class);
29+
}
30+
31+
public function provides()
32+
{
33+
return [
34+
'translatable.helper',
35+
Locales::class,
36+
];
2437
}
2538
}

0 commit comments

Comments
 (0)