Skip to content
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
4 changes: 4 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@ plugins:
enabled: false
CyclomaticComplexity:
enabled: false
CleanCode/ElseExpression:
enabled: false
CleanCode/BooleanArgumentFlag:
enabled: false
phan:
enabled: false
35 changes: 35 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

$finder = PhpCsFixer\Finder::create()
->in('src/Translatable');

return PhpCsFixer\Config::create()
->setRules([
'ordered_class_elements' => [
'order' => [
'use_trait',
'constant_public',
'constant_protected',
'constant_private',
'property_public',
'property_protected',
'property_private',
'construct',
'method_public_static',
'method_public',
'method_protected_static',
'method_protected',
'method_private_static',
'method_private',
'destruct',
'magic',
],
'sortAlgorithm' => 'alpha',
],
'yoda_style' => [
'equal' => false,
'identical' => false,
'less_and_greater' => null,
],
])
->setFinder($finder);
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@
},
"config": {
"sort-packages": true
},
"scripts": {
"csfix": "php-cs-fixer fix --using-cache=no"
}
}
43 changes: 43 additions & 0 deletions src/Translatable/Contracts/Translatable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Astrotomic\Translatable\Contracts;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

interface Translatable
{
public static function defaultAutoloadTranslations(): void;

public static function disableAutoloadTranslations(): void;

public static function enableAutoloadTranslations(): void;

public function translations(): HasMany;

public function deleteTranslations($locales = null): void;

public function getDefaultLocale(): ?string;

public function getNewTranslation(string $locale): Model;

public function getTranslation(?string $locale = null, bool $withFallback = null): ?Model;

public function getTranslationOrNew(?string $locale = null): Model;

public function getTranslationsArray(): array;

public function hasTranslation(?string $locale = null): bool;

public function isTranslationAttribute(string $key): bool;

public function replicateWithTranslations(array $except = null): Model;

public function setDefaultLocale(?string $locale);

public function translate(?string $locale = null, bool $withFallback = false): ?Model;

public function translateOrDefault(?string $locale = null): ?Model;

public function translateOrNew(?string $locale = null): Model;
}
74 changes: 37 additions & 37 deletions src/Translatable/Locales.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ class Locales implements Arrayable, ArrayAccess
protected $config;

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

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

public function __construct(ConfigContract $config, TranslatorContract $translator)
{
Expand All @@ -33,26 +33,9 @@ public function __construct(ConfigContract $config, TranslatorContract $translat
$this->load();
}

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

if (empty($localesConfig)) {
throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish astrotomic/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;
}
} elseif (is_string($locale)) {
$this->locales[$locale] = $locale;
}
}
$this->locales[$locale] = $locale;
}

public function all(): array
Expand All @@ -65,49 +48,61 @@ public function current()
return $this->config->get('translatable.locale') ?: $this->translator->getLocale();
}

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

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

public function add(string $locale): void
public function getCountryLocale(string $locale, string $country): string
{
$this->locales[$locale] = $locale;
return $locale.$this->getLocaleSeparator().$country;
}

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

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

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

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

public function getLanguageFromCountryBasedLocale(string $locale): string
public function load(): void
{
return explode($this->getLocaleSeparator(), $locale)[0];
}
$localesConfig = (array) $this->config->get('translatable.locales', []);

public function toArray(): array
{
return $this->all();
if (empty($localesConfig)) {
throw new LocalesNotDefinedException('Please make sure you have run "php artisan config:publish astrotomic/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;
}
} elseif (is_string($locale)) {
$this->locales[$locale] = $locale;
}
}
}

public function offsetExists($key): bool
Expand All @@ -133,4 +128,9 @@ public function offsetUnset($key)
{
$this->forget($key);
}

public function toArray(): array
{
return $this->all();
}
}
67 changes: 67 additions & 0 deletions src/Translatable/Traits/Relationship.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Astrotomic\Translatable\Traits;

use Illuminate\Database\Eloquent\Relations\HasMany;

/**
* @property-read string $translationModel
* @property-read string $translationForeignKey
*/
trait Relationship
{
/**
* @deprecated
*/
public function getRelationKey(): string
{
return $this->getTranslationRelationKey();
}

/**
* @internal will change to protected
*/
public function getTranslationModelName(): string
{
return $this->translationModel ?: $this->getTranslationModelNameDefault();
}

/**
* @internal will change to private
*/
public function getTranslationModelNameDefault(): string
{
$modelName = get_class($this);

if ($namespace = $this->getTranslationModelNamespace()) {
$modelName = $namespace.'\\'.class_basename(get_class($this));
}

return $modelName.config('translatable.translation_suffix', 'Translation');
}

/**
* @internal will change to private
*/
public function getTranslationModelNamespace(): ?string
{
return config('translatable.translation_model_namespace');
}

/**
* @internal will change to protected
*/
public function getTranslationRelationKey(): string
{
if ($this->translationForeignKey) {
return $this->translationForeignKey;
}

return $this->getForeignKey();
}

public function translations(): HasMany
{
return $this->hasMany($this->getTranslationModelName(), $this->getTranslationRelationKey());
}
}
Loading