1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Signifly\Translator; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
6
|
|
|
use Illuminate\Contracts\Config\Repository; |
7
|
|
|
use Signifly\Translator\Models\Translation; |
8
|
|
|
use Signifly\Translator\Exceptions\InvalidConfiguration; |
9
|
|
|
use Signifly\Translator\Contracts\Translator as Contract; |
10
|
|
|
use Signifly\Translator\Contracts\Translation as TranslationContract; |
11
|
|
|
|
12
|
|
|
class Translator implements Contract |
13
|
|
|
{ |
14
|
|
|
/** @var \Illuminate\Contracts\Config\Repository */ |
15
|
|
|
protected $config; |
16
|
|
|
|
17
|
|
|
public function __construct(Repository $config) |
18
|
|
|
{ |
19
|
|
|
$this->config = $config; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
public function activateLanguage(string $languageCode): void |
23
|
|
|
{ |
24
|
|
|
$this->config->set('translator.active_language_code', $languageCode); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function activeLanguageCode(): string |
28
|
|
|
{ |
29
|
|
|
return $this->config->get('translator.active_language_code') ?? $this->defaultLanguageCode(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public function autoTranslates(): bool |
33
|
|
|
{ |
34
|
|
|
return (bool) $this->config->get('translator.auto_translate_attributes', false); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function defaultLanguageCode(): string |
38
|
|
|
{ |
39
|
|
|
return $this->config->get('translator.default_language_code'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function determineModel(): string |
43
|
|
|
{ |
44
|
|
|
$model = $this->config->get('translator.translation_model') ?? Translation::class; |
45
|
|
|
|
46
|
|
|
if (! is_a($model, TranslationContract::class, true) |
47
|
|
|
|| ! is_a($model, Model::class, true)) { |
48
|
|
|
throw InvalidConfiguration::modelIsNotValid($model); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $model; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function disableAutoTranslation(): void |
55
|
|
|
{ |
56
|
|
|
$this->config->set('translator.auto_translate_attributes', false); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function enableAutoTranslation(): void |
60
|
|
|
{ |
61
|
|
|
$this->config->set('translator.auto_translate_attributes', true); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function isDefaultLanguage(string $languageCode): bool |
65
|
|
|
{ |
66
|
|
|
return $this->defaultLanguageCode() === $languageCode; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function softDeletes(): bool |
70
|
|
|
{ |
71
|
|
|
return (bool) $this->config->get('translator.soft_deletes', false); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|