-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslationUnit.php
More file actions
190 lines (158 loc) · 4.99 KB
/
Copy pathTranslationUnit.php
File metadata and controls
190 lines (158 loc) · 4.99 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
/**
* Italix I18n - TranslationUnit
*
* @package Italix\I18n
*/
declare(strict_types=1);
namespace Italix\I18n;
/**
* One message on its way to or from a translator.
*
* The runtime catalogue — `lang/{locale}/messages.php` — holds only the
* translated text, which is all a page needs and not enough to *maintain* a
* translation. Three things are missing from it and present here:
*
* - the **source** the translation was made from, without which nobody can
* tell that the original has since been reworded;
* - a **state**, so a revised original can be handed back as "look at this
* again" instead of being silently dropped or silently kept;
* - **notes and source references**, because a translator given the bare word
* "Open" cannot know whether it is a verb on a button.
*
* Hence two formats rather than one: PHP arrays are what runs, XLIFF or PO is
* what travels. This class is the shape they meet in.
*/
final class TranslationUnit
{
/** Never seen by a translator. */
public const INITIAL = 'initial';
/** Translated, and the source has not moved since. */
public const TRANSLATED = 'translated';
/**
* Translated, but the source changed afterwards — gettext's `fuzzy`.
*
* The single most useful state, and the reason this whole file exists: the
* old translation is kept and shown, so the work is *revised* rather than
* redone.
*/
public const NEEDS_REVIEW = 'needs-review';
/** Checked by a second pair of eyes. */
public const REVIEWED = 'reviewed';
private string $key_c;
private string $source;
private string $target;
private string $state_c;
/** @var string[] free-text notes for whoever translates this */
private array $notes;
/** @var string[] where in the code this message is used */
private array $references;
/**
* A message the source no longer uses.
*
* Kept rather than deleted — gettext's `#~` — so that reverting a change
* does not throw away a translation somebody was paid for.
*/
private bool $is_obsolete;
public function __construct(
string $key_c,
string $source,
string $target = '',
string $state_c = self::INITIAL,
array $notes = [],
array $references = [],
bool $is_obsolete = false
) {
$this->key_c = $key_c;
$this->source = $source;
$this->target = $target;
$this->state_c = $state_c;
$this->notes = array_values($notes);
$this->references = array_values($references);
$this->is_obsolete = $is_obsolete;
}
public function key(): string
{
return $this->key_c;
}
public function source(): string
{
return $this->source;
}
public function target(): string
{
return $this->target;
}
public function state_code(): string
{
return $this->state_c;
}
/** @return string[] */
public function notes(): array
{
return $this->notes;
}
/** @return string[] */
public function references(): array
{
return $this->references;
}
public function is_obsolete(): bool
{
return $this->is_obsolete;
}
/** Translated and current — the only state safe to ship without a look. */
public function is_usable(): bool
{
return !$this->is_obsolete
&& $this->target !== ''
&& ($this->state_c === self::TRANSLATED || $this->state_c === self::REVIEWED);
}
/** Waiting for somebody: never translated, or translated and then outdated. */
public function needs_work(): bool
{
return !$this->is_obsolete && !$this->is_usable();
}
public function with_target(string $target, string $state_c = self::TRANSLATED): self
{
$copy = clone $this;
$copy->target = $target;
$copy->state_c = $state_c;
return $copy;
}
public function with_source(string $source): self
{
$copy = clone $this;
$copy->source = $source;
return $copy;
}
public function with_state(string $state_c): self
{
$copy = clone $this;
$copy->state_c = $state_c;
return $copy;
}
public function with_references(array $references): self
{
$copy = clone $this;
$copy->references = array_values($references);
return $copy;
}
public function with_notes(array $notes): self
{
$copy = clone $this;
$copy->notes = array_values($notes);
return $copy;
}
public function obsolete(bool $flag = true): self
{
$copy = clone $this;
$copy->is_obsolete = $flag;
return $copy;
}
}