-
Notifications
You must be signed in to change notification settings - Fork 7
/
Package.php
164 lines (149 loc) · 3.95 KB
/
Package.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @copyright Copyright (c) 2017 Aura for PHP
* @link https://cakephp.org CakePHP(tm) Project
* @since 4.2.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\I18n;
/**
* Message Catalog
*/
class Package
{
/**
* Message keys and translations in this package.
*
* @var array<string|array>
*/
protected $messages = [];
/**
* The name of a fallback package to use when a message key does not
* exist.
*
* @var string|null
*/
protected $fallback;
/**
* The name of the formatter to use when formatting translated messages.
*
* @var string
*/
protected $formatter;
/**
* Constructor.
*
* @param string $formatter The name of the formatter to use.
* @param string|null $fallback The name of the fallback package to use.
* @param array<string|array> $messages The messages in this package.
*/
public function __construct(
string $formatter = 'default',
?string $fallback = null,
array $messages = []
) {
$this->formatter = $formatter;
$this->fallback = $fallback;
$this->messages = $messages;
}
/**
* Sets the messages for this package.
*
* @param array<string|array> $messages The messages for this package.
* @return void
*/
public function setMessages(array $messages): void
{
$this->messages = $messages;
}
/**
* Adds one message for this package.
*
* @param string $key the key of the message
* @param string|array $message the actual message
* @return void
*/
public function addMessage(string $key, $message): void
{
$this->messages[$key] = $message;
}
/**
* Adds new messages for this package.
*
* @param array<string|array> $messages The messages to add in this package.
* @return void
*/
public function addMessages(array $messages): void
{
$this->messages = array_merge($this->messages, $messages);
}
/**
* Gets the messages for this package.
*
* @return array<string|array>
*/
public function getMessages(): array
{
return $this->messages;
}
/**
* Gets the message of the given key for this package.
*
* @param string $key the key of the message to return
* @return string|array|false The message translation, or false if not found.
*/
public function getMessage(string $key)
{
if (isset($this->messages[$key])) {
return $this->messages[$key];
}
return false;
}
/**
* Sets the formatter name for this package.
*
* @param string $formatter The formatter name for this package.
* @return void
*/
public function setFormatter(string $formatter): void
{
$this->formatter = $formatter;
}
/**
* Gets the formatter name for this package.
*
* @return string
*/
public function getFormatter(): string
{
return $this->formatter;
}
/**
* Sets the fallback package name.
*
* @param string|null $fallback The fallback package name.
* @return void
*/
public function setFallback(?string $fallback): void
{
$this->fallback = $fallback;
}
/**
* Gets the fallback package name.
*
* @return string|null
*/
public function getFallback(): ?string
{
return $this->fallback;
}
}