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

Commit

Permalink
Merge pull request #574 from dimsav/ft-locales-helper
Browse files Browse the repository at this point in the history
 add locales helper class
  • Loading branch information
Gummibeer committed Jun 3, 2019
2 parents de9f536 + b9c330c commit 485b33a
Show file tree
Hide file tree
Showing 6 changed files with 423 additions and 71 deletions.
136 changes: 136 additions & 0 deletions src/Translatable/Locales.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace Dimsav\Translatable;

use ArrayAccess;
use Illuminate\Contracts\Support\Arrayable;
use Dimsav\Translatable\Exception\LocalesNotDefinedException;
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Contracts\Translation\Translator as TranslatorContract;

class Locales implements Arrayable, ArrayAccess
{
/**
* @var ConfigContract
*/
protected $config;

/**
* @var TranslatorContract
*/
protected $translator;

/**
* @var array
*/
protected $locales = [];

public function __construct(ConfigContract $config, TranslatorContract $translator)
{
$this->config = $config;
$this->translator = $translator;

$this->load();
}

public function load(): void
{
$localesConfig = (array) $this->config->get('translatable.locales', []);

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

$this->locales = [];
foreach ($localesConfig as $key => $locale) {
if (is_string($key) && is_array($locale)) {
$this->locales[$key] = $key;
foreach ($locale as $country) {
$countryLocale = $this->getCountryLocale($key, $country);
$this->locales[$countryLocale] = $countryLocale;
}
} else {
$this->locales[$locale] = $locale;
}
}
}

public function all(): array
{
return array_values($this->locales);
}

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

public function has(string $locale): bool
{
return isset($this->locales[$locale]);
}

public function get(string $locale): ?string
{
return $this->locales[$locale] ?? null;
}

public function add(string $locale): void
{
$this->locales[$locale] = $locale;
}

public function forget(string $locale): void
{
unset($this->locales[$locale]);
}

public function getLocaleSeparator(): string
{
return $this->config->get('translatable.locale_separator') ?: '-';
}

public function getCountryLocale(string $locale, string $country): string
{
return $locale.$this->getLocaleSeparator().$country;
}

public function isLocaleCountryBased(string $locale): bool
{
return strpos($locale, $this->getLocaleSeparator()) !== false;
}

public function getLanguageFromCountryBasedLocale(string $locale): string
{
return explode($this->getLocaleSeparator(), $locale)[0];
}

public function toArray(): array
{
return $this->all();
}

public function offsetExists($key): bool
{
return $this->has($key);
}

public function offsetGet($key): ?string
{
return $this->get($key);
}

public function offsetSet($key, $value)
{
if (is_string($key) && is_string($value)) {
$this->add($this->getCountryLocale($key, $value));
} elseif (is_string($value)) {
$this->add($value);
}
}

public function offsetUnset($key)
{
$this->forget($key);
}
}
80 changes: 17 additions & 63 deletions src/Translatable/Translatable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Illuminate\Database\Query\JoinClause;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Dimsav\Translatable\Exception\LocalesNotDefinedException;

trait Translatable
{
Expand Down Expand Up @@ -346,26 +345,14 @@ private function getFallbackLocale($locale = null)
return config('translatable.fallback_locale');
}

/**
* @param $locale
*
* @return bool
*/
private function isLocaleCountryBased($locale)
private function isLocaleCountryBased(string $locale): bool
{
return strpos($locale, $this->getLocaleSeparator()) !== false;
return $this->getLocalesHelper()->isLocaleCountryBased($locale);
}

/**
* @param $locale
*
* @return string
*/
private function getLanguageFromCountryBasedLocale($locale)
private function getLanguageFromCountryBasedLocale(string $locale): string
{
$parts = explode($this->getLocaleSeparator(), $locale);

return array_get($parts, 0);
return $this->getLocalesHelper()->getLanguageFromCountryBasedLocale($locale);
}

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

/**
* @param string $key
*
* @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException
* @return bool
*/
protected function isKeyALocale($key)
protected function isKeyALocale(string $key): bool
{
$locales = $this->getLocales();

return in_array($key, $locales);
return $this->getLocalesHelper()->has($key);
}

/**
* @throws \Dimsav\Translatable\Exception\LocalesNotDefinedException
* @return array
*/
protected function getLocales()
protected function getLocales(): array
{
$localesConfig = (array) config('translatable.locales');

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

$locales = [];
foreach ($localesConfig as $key => $locale) {
if (is_array($locale)) {
$locales[] = $key;
foreach ($locale as $countryLocale) {
$locales[] = $key.$this->getLocaleSeparator().$countryLocale;
}
} else {
$locales[] = $locale;
}
}

return $locales;
return $this->getLocalesHelper()->all();
}

/**
* @return string
*/
protected function getLocaleSeparator()
protected function getLocaleSeparator(): string
{
return config('translatable.locale_separator', '-');
return $this->getLocalesHelper()->getLocaleSeparator();
}

/**
Expand Down Expand Up @@ -778,17 +731,13 @@ private function getTranslationsTable()
return app()->make($this->getTranslationModelName())->getTable();
}

/**
* @return string
*/
protected function locale()
protected function locale(): string
{
if ($this->defaultLocale) {
return $this->defaultLocale;
}

return config('translatable.locale')
?: app()->make('translator')->getLocale();
return $this->getLocalesHelper()->current();
}

/**
Expand Down Expand Up @@ -874,4 +823,9 @@ public static function disableAutoloadTranslations()
{
self::$autoloadTranslations = false;
}

protected function getLocalesHelper(): Locales
{
return app(Locales::class);
}
}
19 changes: 16 additions & 3 deletions src/Translatable/TranslatableServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,26 @@ public function boot()
], 'translatable');
}

/**
* Register the service provider.
*/
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/../config/translatable.php', 'translatable'
);

$this->registerTranslatableHelper();
}

public function registerTranslatableHelper()
{
$this->app->singleton('translatable.locales', Locales::class);
$this->app->singleton(Locales::class);
}

public function provides()
{
return [
'translatable.helper',
Locales::class,
];
}
}

0 comments on commit 485b33a

Please sign in to comment.