1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Presspack\Framework\Support\Translation; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Facades\App; |
6
|
|
|
use Presspack\Framework\Support\Translation\Models\IclString; |
7
|
|
|
|
8
|
|
|
class Strings |
9
|
|
|
{ |
10
|
|
|
/** @var \Illuminate\Support\Collection */ |
11
|
|
|
protected $iclStrings; |
12
|
|
|
|
13
|
|
|
/** @var \Illuminate\Support\Collection */ |
14
|
|
|
protected $string; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
protected $translated; |
18
|
|
|
|
19
|
|
|
/** @var bool */ |
20
|
|
|
protected $localeIsDefault = true; |
21
|
|
|
|
22
|
|
|
public function __construct() |
23
|
|
|
{ |
24
|
|
|
if (App::getLocale() != config('presspack.default_locale')) { |
|
|
|
|
25
|
|
|
$this->localeIsDefault = false; |
26
|
|
|
$this->iclStrings = IclString::where('context', 'presspack')->get(); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function get(string $string): string |
31
|
|
|
{ |
32
|
|
|
$this->translated = $string; |
33
|
|
|
|
34
|
|
|
if ($this->localeIsDefault) { |
35
|
|
|
return $this->translated; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->getTranslated(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function getTranslated(): string |
42
|
|
|
{ |
43
|
|
|
if ($this->checkIfExists()) { |
44
|
|
|
$translation = $this->string->translations->where('language', App::getLocale())->first(); |
45
|
|
|
|
46
|
|
|
if (isset($translation->value) && 10 == $translation->status) { |
47
|
|
|
$this->translated = $translation->value; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->translated; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function checkIfExists(): bool |
55
|
|
|
{ |
56
|
|
|
$this->string = $this->iclStrings->where('name', md5($this->translated))->first(); |
57
|
|
|
|
58
|
|
|
if (! $this->string) { |
59
|
|
|
$this->addString($this->translated); |
60
|
|
|
|
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function addString($string) |
68
|
|
|
{ |
69
|
|
|
IclString::create([ |
70
|
|
|
'language' => config('presspack.default_locale'), |
|
|
|
|
71
|
|
|
'context' => 'presspack', |
72
|
|
|
'name' => md5($string), |
73
|
|
|
'value' => $string, |
74
|
|
|
'type' => 'LINE', |
75
|
|
|
'status' => 0, |
76
|
|
|
'gettext_context' => '', |
77
|
|
|
'domain_name_context_md5' => md5('presspack'.md5($string)), |
78
|
|
|
'translation_priority' => '', |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|