Skip to content

Commit b1a77c6

Browse files
authored
Add support for adding translation on SourceLocation (#219)
1 parent da6bb44 commit b1a77c6

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

Model/Metadata.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,23 @@ public function getDesc()
8080
return null;
8181
}
8282

83+
/**
84+
* Get the extracted translation if any.
85+
*
86+
* @return null|string
87+
*/
88+
public function getTranslation()
89+
{
90+
$notes = $this->getAllInCategory('translation');
91+
foreach ($notes as $note) {
92+
if (isset($note['content'])) {
93+
return $note['content'];
94+
}
95+
}
96+
97+
return null;
98+
}
99+
83100
/**
84101
* @return bool
85102
*/

Service/Importer.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,15 @@ public function extractToCatalogues(Finder $finder, array $catalogues, array $co
9797
$meta->setState('new');
9898
$this->setMetadata($result, $key, $domain, $meta);
9999

100-
// Add "desc" as translation
101-
if (null === $translation && null !== $desc = $meta->getDesc()) {
102-
$result->set($key, $desc, $domain);
100+
// Add custom translations that we found in the source
101+
if (null === $translation) {
102+
if (null !== $newTranslation = $meta->getTranslation()) {
103+
$result->set($key, $newTranslation, $domain);
104+
// We do not want "translation" key stored anywhere.
105+
$meta->removeAllInCategory('translation');
106+
} elseif (null !== $newTranslation = $meta->getDesc()) {
107+
$result->set($key, $newTranslation, $domain);
108+
}
103109
}
104110
}
105111
foreach ($merge->getObsoleteMessages($domain) as $key => $translation) {
@@ -138,6 +144,9 @@ private function convertSourceLocationsToMessages(MessageCatalogue $catalogue, S
138144
if (isset($sourceLocation->getContext()['desc'])) {
139145
$meta->addCategory('desc', $sourceLocation->getContext()['desc']);
140146
}
147+
if (isset($sourceLocation->getContext()['translation'])) {
148+
$meta->addCategory('translation', $sourceLocation->getContext()['translation']);
149+
}
141150
$this->setMetadata($catalogue, $key, $domain, $meta);
142151
}
143152
}

0 commit comments

Comments
 (0)