Skip to content

Commit fbd97d9

Browse files
committed
#282 Added partial support for the previous translation comment
1 parent 017e249 commit fbd97d9

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

src/Generator/PoGenerator.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,18 @@ public function generateString(Translations $translations): string
6565

6666
$prefix = $translation->isDisabled() ? '#~ ' : '';
6767

68+
if ($context = $translation->getPreviousContext()) {
69+
$lines[] = sprintf('%s#| msgctxt %s', $prefix, self::encode($context));
70+
}
71+
72+
if ($original = $translation->getPreviousOriginal()) {
73+
$lines[] = sprintf('%s#| msgid %s', $prefix, self::encode($original));
74+
}
75+
76+
if ($plural = $translation->getPreviousPlural()) {
77+
$lines[] = sprintf('%s#| msgid_plural %s', $prefix, self::encode($plural));
78+
}
79+
6880
if ($context = $translation->getContext()) {
6981
$lines[] = sprintf('%smsgctxt %s', $prefix, self::encode($context));
7082
}

src/Translation.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class Translation
1919
protected $flags;
2020
protected $comments;
2121
protected $extractedComments;
22+
protected $previousContext;
23+
protected $previousOriginal;
24+
protected $previousPlural;
2225

2326
public static function create(?string $context, string $original): Translation
2427
{
@@ -64,6 +67,9 @@ public function toArray(): array
6467
'plural' => $this->plural,
6568
'pluralTranslations' => $this->pluralTranslations,
6669
'disabled' => $this->disabled,
70+
'previousContext' => $this->previousContext,
71+
'previousOriginal' => $this->previousOriginal,
72+
'previousPlural' => $this->previousPlural,
6773
'references' => $this->getReferences()->toArray(),
6874
'flags' => $this->getFlags()->toArray(),
6975
'comments' => $this->getComments()->toArray(),
@@ -116,6 +122,42 @@ public function getPlural(): ?string
116122
return $this->plural;
117123
}
118124

125+
public function setPreviousOriginal(?string $previousOriginal): self
126+
{
127+
$this->previousOriginal = $previousOriginal;
128+
129+
return $this;
130+
}
131+
132+
public function getPreviousOriginal(): ?string
133+
{
134+
return $this->previousOriginal;
135+
}
136+
137+
public function setPreviousContext(?string $previousContext): self
138+
{
139+
$this->previousContext = $previousContext;
140+
141+
return $this;
142+
}
143+
144+
public function getPreviousContext(): ?string
145+
{
146+
return $this->previousContext;
147+
}
148+
149+
public function setPreviousPlural(?string $previousPlural): self
150+
{
151+
$this->previousPlural = $previousPlural;
152+
153+
return $this;
154+
}
155+
156+
public function getPreviousPlural(): ?string
157+
{
158+
return $this->previousPlural;
159+
}
160+
119161
public function disable(bool $disabled = true): self
120162
{
121163
$this->disabled = $disabled;
@@ -225,6 +267,18 @@ public function mergeWith(Translation $translation, int $strategy = 0): Translat
225267
$merged->plural = $translation->plural;
226268
}
227269

270+
if (!$merged->previousContext || ($translation->previousContext && $override)) {
271+
$merged->previousContext = $translation->previousContext;
272+
}
273+
274+
if (!$merged->previousOriginal || ($translation->previousOriginal && $override)) {
275+
$merged->previousOriginal = $translation->previousOriginal;
276+
}
277+
278+
if (!$merged->previousPlural || ($translation->previousPlural && $override)) {
279+
$merged->previousPlural = $translation->previousPlural;
280+
}
281+
228282
if (empty($merged->pluralTranslations) || (!empty($translation->pluralTranslations) && $override)) {
229283
$merged->pluralTranslations = $translation->pluralTranslations;
230284
}

0 commit comments

Comments
 (0)