| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
namespace Symfony\Component\Translation\Dumper; |
| 13 |
|
| 14 |
use Symfony\Component\Translation\MessageCatalogue; |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
class PoFileDumper extends FileDumper |
| 22 |
{ |
| 23 |
public function formatCatalogue(MessageCatalogue $messages, string $domain, array $options = []): string |
| 24 |
{ |
| 25 |
$output = 'msgid ""'."\n"; |
| 26 |
$output .= 'msgstr ""'."\n"; |
| 27 |
$output .= '"Content-Type: text/plain; charset=UTF-8\n"'."\n"; |
| 28 |
$output .= '"Content-Transfer-Encoding: 8bit\n"'."\n"; |
| 29 |
$output .= '"Language: '.$messages->getLocale().'\n"'."\n"; |
| 30 |
$output .= "\n"; |
| 31 |
|
| 32 |
$newLine = false; |
| 33 |
foreach ($messages->all($domain) as $source => $target) { |
| 34 |
if ($newLine) { |
| 35 |
$output .= "\n"; |
| 36 |
} else { |
| 37 |
$newLine = true; |
| 38 |
} |
| 39 |
$metadata = $messages->getMetadata($source, $domain); |
| 40 |
|
| 41 |
if (isset($metadata['comments'])) { |
| 42 |
$output .= $this->formatComments($metadata['comments']); |
| 43 |
} |
| 44 |
if (isset($metadata['flags'])) { |
| 45 |
$output .= $this->formatComments(implode(',', (array) $metadata['flags']), ','); |
| 46 |
} |
| 47 |
if (isset($metadata['sources'])) { |
| 48 |
$output .= $this->formatComments(implode(' ', (array) $metadata['sources']), ':'); |
| 49 |
} |
| 50 |
|
| 51 |
$sourceRules = $this->getStandardRules($source); |
| 52 |
$targetRules = $this->getStandardRules($target); |
| 53 |
if (2 == \count($sourceRules) && [] !== $targetRules) { |
| 54 |
$output .= \sprintf('msgid "%s"'."\n", $this->escape($sourceRules[0])); |
| 55 |
$output .= \sprintf('msgid_plural "%s"'."\n", $this->escape($sourceRules[1])); |
| 56 |
foreach ($targetRules as $i => $targetRule) { |
| 57 |
$output .= \sprintf('msgstr[%d] "%s"'."\n", $i, $this->escape($targetRule)); |
| 58 |
} |
| 59 |
} else { |
| 60 |
$output .= \sprintf('msgid "%s"'."\n", $this->escape($source)); |
| 61 |
$output .= \sprintf('msgstr "%s"'."\n", $this->escape($target)); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
return $output; |
| 66 |
} |
| 67 |
|
| 68 |
private function getStandardRules(string $id): array |
| 69 |
{ |
| 70 |
|
| 71 |
$parts = []; |
| 72 |
if (preg_match('/^\|++$/', $id)) { |
| 73 |
$parts = explode('|', $id); |
| 74 |
} elseif (preg_match_all('/(?:\|\||[^\|])++/', $id, $matches)) { |
| 75 |
$parts = $matches[0]; |
| 76 |
} |
| 77 |
|
| 78 |
$intervalRegexp = <<<'EOF' |
| 79 |
/^(?P<interval> |
| 80 |
({\s* |
| 81 |
(\-?\d+(\.\d+)?[\s*,\s*\-?\d+(\.\d+)?]*) |
| 82 |
\s*}) |
| 83 |
|
| 84 |
| |
| 85 |
|
| 86 |
(?P<left_delimiter>[\[\]]) |
| 87 |
\s* |
| 88 |
(?P<left>-Inf|\-?\d+(\.\d+)?) |
| 89 |
\s*,\s* |
| 90 |
(?P<right>\+?Inf|\-?\d+(\.\d+)?) |
| 91 |
\s* |
| 92 |
(?P<right_delimiter>[\[\]]) |
| 93 |
)\s*(?P<message>.*?)$/xs |
| 94 |
EOF; |
| 95 |
|
| 96 |
$standardRules = []; |
| 97 |
foreach ($parts as $part) { |
| 98 |
$part = trim(str_replace('||', '|', $part)); |
| 99 |
|
| 100 |
if (preg_match($intervalRegexp, $part)) { |
| 101 |
|
| 102 |
return []; |
| 103 |
} |
| 104 |
|
| 105 |
$standardRules[] = $part; |
| 106 |
} |
| 107 |
|
| 108 |
return $standardRules; |
| 109 |
} |
| 110 |
|
| 111 |
protected function getExtension(): string |
| 112 |
{ |
| 113 |
return 'po'; |
| 114 |
} |
| 115 |
|
| 116 |
private function escape(string $str): string |
| 117 |
{ |
| 118 |
return addcslashes($str, "\0..\37\42\134"); |
| 119 |
} |
| 120 |
|
| 121 |
private function formatComments(string|array $comments, string $prefix = ''): ?string |
| 122 |
{ |
| 123 |
$output = null; |
| 124 |
|
| 125 |
foreach ((array) $comments as $comment) { |
| 126 |
$output .= \sprintf('#%s %s'."\n", $prefix, $comment); |
| 127 |
} |
| 128 |
|
| 129 |
return $output; |
| 130 |
} |
| 131 |
} |