Skip to content

Commit 570da8b

Browse files
committed
support for description and flags
1 parent 3fcec0f commit 570da8b

8 files changed

Lines changed: 125 additions & 54 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Previous releases are documented in [github releases](https://github.com/oscarot
1010
## [5.4.0] - Unreleased
1111
### Added
1212
- Added `_` function to the list of functions scanned by default
13+
- Added `Translations::setDescription()` and `Translations::getDescription()` methods [#251]
14+
- Added `Translations::getFlags()` that returns a `Flags` object to assign flags to the entire po file [#251]
1315

1416
## [5.3.0] - 2020-02-18
1517
### Added
@@ -60,7 +62,9 @@ Previous releases are documented in [github releases](https://github.com/oscarot
6062
[#244]: https://github.com/php-gettext/Gettext/issues/244
6163
[#246]: https://github.com/php-gettext/Gettext/issues/246
6264
[#247]: https://github.com/php-gettext/Gettext/issues/247
65+
[#251]: https://github.com/php-gettext/Gettext/issues/251
6366

67+
[5.4.0]: https://github.com/php-gettext/Gettext/compare/v5.3.0...HEAD
6468
[5.3.0]: https://github.com/php-gettext/Gettext/compare/v5.2.2...v5.3.0
6569
[5.2.2]: https://github.com/php-gettext/Gettext/compare/v5.2.1...v5.2.2
6670
[5.2.1]: https://github.com/php-gettext/Gettext/compare/v5.2.0...v5.2.1

src/Loader/PoLoader.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,19 @@ public function loadString(string $string, Translations $translations = null): T
122122
}
123123

124124
$translations->remove($translation);
125+
126+
$description = $translation->getComments()->toArray();
127+
128+
if ($description) {
129+
$translations->setDescription(implode("\n", $description));
130+
}
131+
132+
$flags = $translation->getFlags()->toArray();
133+
134+
if ($flags) {
135+
$translations->getFlags()->add(...$flags);
136+
}
137+
125138
$headers = $translations->getHeaders();
126139

127140
foreach (self::parseHeaders($translation->getTranslation()) as $name => $value) {

src/Translations.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
*/
1515
class Translations implements Countable, IteratorAggregate
1616
{
17+
protected $description;
1718
protected $translations = [];
1819
protected $headers;
20+
protected $flags;
1921

2022
public static function create(string $domain = null, string $language = null): Translations
2123
{
@@ -35,6 +37,7 @@ public static function create(string $domain = null, string $language = null): T
3537
protected function __construct()
3638
{
3739
$this->headers = new Headers();
40+
$this->flags = new Flags();
3841
}
3942

4043
public function __clone()
@@ -46,10 +49,29 @@ public function __clone()
4649
$this->headers = clone $this->headers;
4750
}
4851

52+
public function setDescription(?string $description): self
53+
{
54+
$this->description = $description;
55+
56+
return $this;
57+
}
58+
59+
public function getDescription(): ?string
60+
{
61+
return $this->description;
62+
}
63+
64+
public function getFlags(): Flags
65+
{
66+
return $this->flags;
67+
}
68+
4969
public function toArray(): array
5070
{
5171
return [
72+
'description' => $this->description,
5273
'headers' => $this->headers->toArray(),
74+
'flags' => $this->flags->toArray(),
5375
'translations' => array_map(
5476
function ($translation) {
5577
return $translation->toArray();
@@ -163,6 +185,16 @@ public function mergeWith(Translations $translations, int $strategy = 0): Transl
163185
$merged->headers = $merged->headers->mergeWith($translations->headers);
164186
}
165187

188+
if ($strategy & Merge::FLAGS_THEIRS) {
189+
$merged->flags = clone $translations->flags;
190+
} elseif (!($strategy & Merge::FLAGS_OURS)) {
191+
$merged->flags = $merged->flags->mergeWith($translations->flags);
192+
}
193+
194+
if (!$merged->description) {
195+
$merged->description = $translations->description;
196+
}
197+
166198
foreach ($translations as $id => $translation) {
167199
if (isset($merged->translations[$id])) {
168200
$translation = $merged->translations[$id]->mergeWith($translation, $strategy);

tests/MergeTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ private static function createPOT(): Translations
1919
->set('Last-Translator', '')
2020
->set('X-Foo', 'foo')
2121
->set('X-Generator', 'PHP Gettext scanner');
22+
$translations->getFlags()->add('fuzzy');
2223

2324
$translation = Translation::create(null, 'title');
2425
$translation->getReferences()->add('template.php', 3);
@@ -55,6 +56,7 @@ private static function createPO(): Translations
5556
->set('Language-Team', 'My Team')
5657
->set('X-Foo', 'bar')
5758
->set('Language', 'gl_ES');
59+
$translations->setDescription('This is a description');
5860

5961
$translation = Translation::create(null, 'title');
6062
$translation->getReferences()

tests/PoLoaderTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ public function testPoLoader()
1414
$loader = new PoLoader();
1515
$translations = $loader->loadFile(__DIR__.'/assets/translations.po');
1616

17+
$description = $translations->getDescription();
18+
$this->assertSame(<<<'EOT'
19+
SOME DESCRIPTIVE TITLE
20+
Copyright (C) YEAR Free Software Foundation, Inc.
21+
This file is distributed under the same license as the PACKAGE package.
22+
FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
23+
EOT, $description);
24+
25+
$this->assertSame(['fuzzy'], $translations->getFlags()->toArray());
26+
1727
$this->assertCount(14, $translations);
1828

1929
$array = $translations->getTranslations();

tests/assets/translations.po

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
# SOME DESCRIPTIVE TITLE
2+
# Copyright (C) YEAR Free Software Foundation, Inc.
3+
# This file is distributed under the same license as the PACKAGE package.
4+
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
5+
#
6+
#, fuzzy
17
msgid ""
28
msgstr ""
39
"Content-Type: text/plain; charset=UTF-8\n"

tests/snapshots/testNoStrategy.php

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
<?php
2-
return [
1+
<?php return [
2+
'description' => 'This is a description',
33
'headers' => [
44
'Language' => 'gl_ES',
55
'Language-Team' => 'My Team',
66
'Last-Translator' => 'Oscar',
77
'POT-Creation-Date' => '2019-10-10 10:10:10',
88
'X-Domain' => 'my-domain',
99
'X-Foo' => 'bar',
10-
'X-Generator' => 'PHP Gettext scanner',
10+
'X-Generator' => 'PHP Gettext scanner'
11+
],
12+
'flags' => [
13+
'fuzzy'
1114
],
1215
'translations' => [
1316
[
@@ -21,15 +24,15 @@
2124
'references' => [
2225
'template.php' => [
2326
3,
24-
2,
27+
2
2528
],
2629
'other-template.php' => [
27-
2,
28-
],
30+
2
31+
]
2932
],
3033
'flags' => [],
3134
'comments' => [],
32-
'extractedComments' => [],
35+
'extractedComments' => []
3336
],
3437
[
3538
'id' => 'intro',
@@ -41,14 +44,14 @@
4144
'disabled' => true,
4245
'references' => [
4346
'template.php' => [
44-
4,
45-
],
47+
4
48+
]
4649
],
4750
'flags' => [],
4851
'comments' => [
49-
'Disabled comment',
52+
'Disabled comment'
5053
],
51-
'extractedComments' => [],
54+
'extractedComments' => []
5255
],
5356
[
5457
'id' => 'one comment',
@@ -57,20 +60,20 @@
5760
'translation' => 'Un comentario',
5861
'plural' => '%s comments',
5962
'pluralTranslations' => [
60-
'%s comentarios',
63+
'%s comentarios'
6164
],
6265
'disabled' => false,
6366
'references' => [
6467
'template.php' => [
6568
5,
66-
6,
67-
],
69+
6
70+
]
6871
],
6972
'flags' => [],
7073
'comments' => [],
7174
'extractedComments' => [
72-
'Number of comments of the article',
73-
],
75+
'Number of comments of the article'
76+
]
7477
],
7578
[
7679
'id' => 'This is a flagged element',
@@ -82,17 +85,17 @@
8285
'disabled' => false,
8386
'references' => [
8487
'template.php' => [
85-
10,
86-
],
88+
10
89+
]
8790
],
8891
'flags' => [
8992
'a-code',
90-
'c-code',
93+
'c-code'
9194
],
9295
'comments' => [
93-
'This is a comment',
96+
'This is a comment'
9497
],
95-
'extractedComments' => [],
98+
'extractedComments' => []
9699
],
97100
[
98101
'id' => 'This is a new translation',
@@ -104,12 +107,12 @@
104107
'disabled' => false,
105108
'references' => [
106109
'template.php' => [
107-
11,
108-
],
110+
11
111+
]
109112
],
110113
'flags' => [],
111114
'comments' => [],
112-
'extractedComments' => [],
115+
'extractedComments' => []
113116
],
114117
[
115118
'id' => 'subtitle',
@@ -121,12 +124,12 @@
121124
'disabled' => false,
122125
'references' => [
123126
'template.php' => [
124-
2,
125-
],
127+
2
128+
]
126129
],
127130
'flags' => [],
128131
'comments' => [],
129-
'extractedComments' => [],
130-
],
131-
],
132+
'extractedComments' => []
133+
]
134+
]
132135
];

0 commit comments

Comments
 (0)