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

add locales helper class #574

Merged
merged 12 commits into from
Jun 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}
86 changes: 20 additions & 66 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 @@ -204,7 +203,7 @@ private function getAttributeOrFallback($locale, $attribute)
*/
public function getAttribute($key)
{
list($attribute, $locale) = $this->getAttributeAndLocale($key);
[$attribute, $locale] = $this->getAttributeAndLocale($key);

if ($this->isTranslationAttribute($attribute)) {
if ($this->getTranslation($locale) === null) {
Expand Down Expand Up @@ -234,7 +233,7 @@ public function getAttribute($key)
*/
public function setAttribute($key, $value)
{
list($attribute, $locale) = $this->getAttributeAndLocale($key);
[$attribute, $locale] = $this->getAttributeAndLocale($key);

if ($this->isTranslationAttribute($attribute)) {
$this->getTranslationOrNew($locale)->$attribute = $value;
Expand Down Expand Up @@ -304,7 +303,7 @@ public function fill(array $attributes)
$this->getTranslationOrNew($key)->fill($values);
unset($attributes[$key]);
} else {
list($attribute, $locale) = $this->getAttributeAndLocale($key);
[$attribute, $locale] = $this->getAttributeAndLocale($key);
if ($this->isTranslationAttribute($attribute) and $this->isKeyALocale($locale)) {
$this->getTranslationOrNew($locale)->fill([$attribute => $values]);
unset($attributes[$key]);
Expand Down Expand Up @@ -345,26 +344,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 @@ -389,53 +376,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 @@ -777,17 +730,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 @@ -873,4 +822,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,
];
}
}
Loading