Skip to content

Commit 4d50ae9

Browse files
authored
Medged php-translation/symfony (#19)
1 parent 00951dd commit 4d50ae9

File tree

10 files changed

+821
-7
lines changed

10 files changed

+821
-7
lines changed

Catalogue/CatalogueManager.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Bundle\Catalogue;
13+
14+
use Symfony\Component\Translation\MessageCatalogue;
15+
use Translation\Bundle\Model\Message;
16+
17+
/**
18+
* A manager that handle loaded catalogues.
19+
*
20+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
21+
*/
22+
class CatalogueManager
23+
{
24+
/**
25+
* @var MessageCatalogue[]
26+
*/
27+
private $catalogues;
28+
29+
/**
30+
* @var string
31+
*/
32+
private $projectRoot;
33+
34+
/**
35+
* @param string $projectRoot
36+
*/
37+
public function __construct($projectRoot)
38+
{
39+
$this->projectRoot = $projectRoot;
40+
}
41+
42+
/**
43+
* @param MessageCatalogue[] $catalogues
44+
*/
45+
public function load(array $catalogues)
46+
{
47+
$this->catalogues = [];
48+
foreach ($catalogues as $c) {
49+
$this->catalogues[$c->getLocale()] = $c;
50+
}
51+
}
52+
53+
/**
54+
* @return array
55+
*/
56+
public function getDomains()
57+
{
58+
/** @var MessageCatalogue $c */
59+
$c = reset($this->catalogues);
60+
61+
return $c->getDomains();
62+
}
63+
64+
/**
65+
* @param string $locale
66+
* @param string $domain
67+
*
68+
* @return Message[]
69+
*/
70+
public function getMessages($locale, $domain)
71+
{
72+
$messages = [];
73+
if (!isset($this->catalogues[$locale])) {
74+
return $messages;
75+
}
76+
77+
foreach ($this->catalogues[$locale]->all($domain) as $key => $text) {
78+
$messages[] = new Message($this, $locale, $domain, $key, $text);
79+
}
80+
81+
return $messages;
82+
}
83+
84+
/**
85+
* @param string $domain
86+
* @param string $key
87+
*
88+
* @return array
89+
*/
90+
public function getTranslations($domain, $key)
91+
{
92+
$translations = [];
93+
foreach ($this->catalogues as $locale => $catalogue) {
94+
if ($catalogue->has($key, $domain)) {
95+
$translations[$locale] = $catalogue->get($key, $domain);
96+
}
97+
}
98+
99+
return $translations;
100+
}
101+
102+
/**
103+
* @param string $domain
104+
* @param string $key
105+
*
106+
* @return array
107+
*/
108+
public function getSourceLocations($domain, $key)
109+
{
110+
$notes = $this->getNotes($domain, $key);
111+
$sources = [];
112+
foreach ($notes as $note) {
113+
if ($note['content'] === 'file-source') {
114+
list($path, $line) = explode(':', $note['from'], 2);
115+
$sources[] = ['full_path' => $this->projectRoot.$path, 'path' => $path, 'line' => $line];
116+
}
117+
}
118+
119+
return $sources;
120+
}
121+
122+
/**
123+
* @param string $domain
124+
* @param string $key
125+
*
126+
* @return bool
127+
*/
128+
public function isNew($domain, $key)
129+
{
130+
$notes = $this->getNotes($domain, $key);
131+
foreach ($notes as $note) {
132+
if ($note['content'] === 'status:new') {
133+
return true;
134+
}
135+
}
136+
137+
return false;
138+
}
139+
140+
/**
141+
* @param string $domain
142+
* @param string $key
143+
*
144+
* @return bool
145+
*/
146+
public function isObsolete($domain, $key)
147+
{
148+
$notes = $this->getNotes($domain, $key);
149+
foreach ($notes as $note) {
150+
if ($note['content'] === 'status:obsolete') {
151+
return true;
152+
}
153+
}
154+
155+
return false;
156+
}
157+
158+
/**
159+
* @param $domain
160+
* @param $key
161+
*
162+
* @return array
163+
*/
164+
private function getNotes($domain, $key)
165+
{
166+
/** @var MessageCatalogue $c */
167+
$c = reset($this->catalogues);
168+
$meta = $c->getMetadata($key, $domain);
169+
170+
if (!isset($meta['notes'])) {
171+
return [];
172+
}
173+
174+
return $meta['notes'];
175+
}
176+
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the PHP Translation package.
5+
*
6+
* (c) PHP Translation team <tobias.nyholm@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Translation\Bundle\Catalogue\Operation;
13+
14+
use Symfony\Component\Translation\Catalogue\AbstractOperation;
15+
16+
/**
17+
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
18+
* @author Jean-François Simon <contact@jfsimon.fr>
19+
*/
20+
class MetadataAwareMerge extends AbstractOperation
21+
{
22+
protected function processDomain($domain)
23+
{
24+
$this->messages[$domain] = [
25+
'all' => [],
26+
'new' => [],
27+
'obsolete' => [],
28+
];
29+
$targetMessages = $this->target->all($domain);
30+
31+
foreach ($this->source->all($domain) as $id => $message) {
32+
$this->messages[$domain]['all'][$id] = $message;
33+
$this->result->add([$id => $message], $domain);
34+
if (empty($message)) {
35+
$this->messages[$domain]['new'][$id] = $message;
36+
}
37+
38+
// If $id is NOT defined in target.
39+
if (!array_key_exists($id, $targetMessages)) {
40+
$this->messages[$domain]['obsolete'][$id] = $message;
41+
}
42+
43+
if (null !== $keySourceMetadata = $this->source->getMetadata($id, $domain)) {
44+
$this->result->setMetadata($id, $keySourceMetadata, $domain);
45+
}
46+
47+
// Get metadata from target
48+
if (null !== $keyTargetMetadata = $this->target->getMetadata($id, $domain)) {
49+
if (null === $keySourceMetadata) {
50+
// If there were no metadata in source. Just use target's metadata
51+
$this->result->setMetadata($id, $keyTargetMetadata, $domain);
52+
continue;
53+
}
54+
55+
// Merge metadata
56+
$resultMetadata = $this->mergeMetaData($keySourceMetadata, $keyTargetMetadata);
57+
$this->result->setMetadata($id, $resultMetadata, $domain);
58+
}
59+
}
60+
61+
foreach ($targetMessages as $id => $message) {
62+
if (!$this->source->has($id, $domain)) {
63+
$this->messages[$domain]['all'][$id] = $message;
64+
$this->messages[$domain]['new'][$id] = $message;
65+
66+
$this->result->add([$id => $message], $domain);
67+
if (null !== $keyMetadata = $this->target->getMetadata($id, $domain)) {
68+
$this->result->setMetadata($id, $keyMetadata, $domain);
69+
}
70+
}
71+
}
72+
}
73+
74+
/**
75+
* @param array $source
76+
* @param array $target
77+
*
78+
* @return array
79+
*/
80+
private function mergeMetadata(array $source, array $target)
81+
{
82+
$toRemove = ['status:new', 'status:obsolete', 'file-source'];
83+
$resultNotes = [];
84+
85+
// Remove some old notes
86+
if (isset($source['notes'])) {
87+
foreach ($source['notes'] as $note) {
88+
if (isset($note['content']) && in_array($note['content'], $toRemove)) {
89+
continue;
90+
}
91+
$resultNotes[] = $note;
92+
}
93+
}
94+
95+
if (isset($target['notes'])) {
96+
foreach ($target['notes'] as $note) {
97+
$resultNotes[] = $note;
98+
}
99+
}
100+
101+
$result = $source;
102+
$result['notes'] = $resultNotes;
103+
104+
return $result;
105+
}
106+
}

Controller/WebUIController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
use Translation\Bundle\Exception\MessageValidationException;
2222
use Translation\Bundle\Model\GuiMessageRepresentation;
2323
use Translation\Common\Exception\StorageException;
24-
use Translation\Symfony\Model\Message;
24+
use Translation\Bundle\Model\Message;
2525

2626
/**
2727
* @author Tobias Nyholm <tobias.nyholm@gmail.com>

0 commit comments

Comments
 (0)