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

Commit 3229984

Browse files
deargonautGummibeer
authored andcommitted
transfer logic into helper class
1 parent 5972379 commit 3229984

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

src/Translatable/Locales.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Dimsav\Translatable;
44

5+
use Dimsav\Translatable\Exception\LocalesNotDefinedException;
56
use Illuminate\Contracts\Config\Repository as ConfigContract;
67
use Illuminate\Contracts\Translation\Translator as TranslatorContract;
78

@@ -17,9 +18,59 @@ class Locales
1718
*/
1819
protected $translator;
1920

21+
/**
22+
* @var array
23+
*/
24+
protected $locales = [];
25+
2026
public function __construct(ConfigContract $config, TranslatorContract $translator)
2127
{
2228
$this->config = $config;
2329
$this->translator = $translator;
2430
}
31+
32+
public function getLocales(): array
33+
{
34+
if (empty($this->locales)) {
35+
$localesConfig = (array)$this->config->get('translatable.locales', []);
36+
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+
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;
49+
}
50+
}
51+
}
52+
53+
54+
return $this->locales;
55+
}
56+
57+
public function current()
58+
{
59+
return $this->config->get('translatable.locale') ?: $this->translator->getLocale();
60+
}
61+
62+
public function isLocaleCountryBased(string $locale): bool
63+
{
64+
return strpos($locale, $this->getLocaleSeparator()) !== false;
65+
}
66+
67+
public function getLanguageFromCountryBasedLocale(string $locale): string
68+
{
69+
return explode($this->getLocaleSeparator(), $locale)[0];
70+
}
71+
72+
protected function getLocaleSeparator(): string
73+
{
74+
return $this->config->get('translatable.locale_separator', '-');
75+
}
2576
}

0 commit comments

Comments
 (0)