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

Commit 6f0fc54

Browse files
committed
use new helper class in trait
1 parent 9906a7c commit 6f0fc54

File tree

2 files changed

+18
-62
lines changed

2 files changed

+18
-62
lines changed

src/Translatable/Translatable.php

Lines changed: 17 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -345,26 +345,14 @@ private function getFallbackLocale($locale = null)
345345
return config('translatable.fallback_locale');
346346
}
347347

348-
/**
349-
* @param $locale
350-
*
351-
* @return bool
352-
*/
353-
private function isLocaleCountryBased($locale)
348+
private function isLocaleCountryBased(string $locale): bool
354349
{
355-
return strpos($locale, $this->getLocaleSeparator()) !== false;
350+
return $this->getLocalesHelper()->isLocaleCountryBased($locale);
356351
}
357352

358-
/**
359-
* @param $locale
360-
*
361-
* @return string
362-
*/
363-
private function getLanguageFromCountryBasedLocale($locale)
353+
private function getLanguageFromCountryBasedLocale(string $locale): string
364354
{
365-
$parts = explode($this->getLocaleSeparator(), $locale);
366-
367-
return array_get($parts, 0);
355+
return $this->getLocalesHelper()->getLanguageFromCountryBasedLocale($locale);
368356
}
369357

370358
/**
@@ -389,53 +377,19 @@ public function isTranslationAttribute($key)
389377
return in_array($key, $this->translatedAttributes);
390378
}
391379

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

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

433-
/**
434-
* @return string
435-
*/
436-
protected function getLocaleSeparator()
390+
protected function getLocaleSeparator(): string
437391
{
438-
return config('translatable.locale_separator', '-');
392+
return $this->getLocalesHelper()->getLocaleSeparator();
439393
}
440394

441395
/**
@@ -777,17 +731,13 @@ private function getTranslationsTable()
777731
return app()->make($this->getTranslationModelName())->getTable();
778732
}
779733

780-
/**
781-
* @return string
782-
*/
783-
protected function locale()
734+
protected function locale(): string
784735
{
785736
if ($this->defaultLocale) {
786737
return $this->defaultLocale;
787738
}
788739

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

793743
/**
@@ -873,4 +823,9 @@ public static function disableAutoloadTranslations()
873823
{
874824
self::$autoloadTranslations = false;
875825
}
826+
827+
protected function getLocalesHelper(): Locales
828+
{
829+
return app(Locales::class);
830+
}
876831
}

src/Translatable/TranslatableServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function provides()
3232
{
3333
return [
3434
'translatable.helper',
35+
Locales::class,
3536
];
3637
}
3738
}

0 commit comments

Comments
 (0)