-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathPotGenerator.php
More file actions
173 lines (146 loc) · 4.63 KB
/
PotGenerator.php
File metadata and controls
173 lines (146 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
namespace WP_CLI\I18n;
use Gettext\Generators\Po as PoGenerator;
use Gettext\Translations;
use Gettext\Utils\ParsedComment;
/**
* POT file generator.
*
* The only difference to the existing PO file generator is that this
* adds some comments at the very beginning of the file.
*/
class PotGenerator extends PoGenerator {
/**
* @var array<string>
*/
protected static $comments_before_headers = [];
/**
* Text to include as a comment before the start of the PO contents
*
* Doesn't need to include # in the beginning of lines, these are added automatically.
*
* @param string $comment File comment.
* @return void
*/
public static function setCommentBeforeHeaders( $comment ) {
$comments = explode( "\n", $comment );
foreach ( $comments as $line ) {
if ( '' !== trim( $line ) ) {
static::$comments_before_headers[] = '# ' . $line;
}
}
}
/**
* {@inheritdoc}
*
* @param \Gettext\Translations $translations
* @param array<mixed> $options
* @return string
*/
public static function toString( Translations $translations, array $options = [] ) {
/** @var array<string> $lines */
$lines = static::$comments_before_headers;
$lines[] = 'msgid ""';
$lines[] = 'msgstr ""';
$plural_form = $translations->getPluralForms();
$plural_size = 1;
if ( is_array( $plural_form ) && isset( $plural_form[0] ) && is_numeric( $plural_form[0] ) ) {
$plural_size = (int) $plural_form[0] - 1;
}
foreach ( $translations->getHeaders() as $name => $value ) {
$lines[] = sprintf( '"%s: %s\\n"', $name, $value );
}
$lines[] = '';
foreach ( $translations as $translation ) {
/** @var \Gettext\Translation $translation */
if ( $translation->hasComments() ) {
foreach ( $translation->getComments() as $comment ) {
if ( is_scalar( $comment ) ) {
$lines[] = '# ' . $comment;
}
}
}
if ( $translation->hasExtractedComments() ) {
$unique_comments = array();
/** @var ParsedComment|string $comment */
foreach ( $translation->getExtractedComments() as $comment ) {
$comment = ( $comment instanceof ParsedComment ? $comment->getComment() : $comment );
if ( is_scalar( $comment ) ) {
$comment_str = (string) $comment;
if ( ! in_array( $comment_str, $unique_comments, true ) ) {
$lines[] = '#. ' . $comment_str;
$unique_comments[] = $comment_str;
}
}
}
}
foreach ( $translation->getReferences() as $reference ) {
if ( is_array( $reference ) && isset( $reference[0] ) ) {
$ref_str = is_scalar( $reference[0] ) ? (string) $reference[0] : '';
$line_str = '';
if ( isset( $reference[1] ) && is_scalar( $reference[1] ) ) {
$line_str = ':' . $reference[1];
}
$lines[] = '#: ' . $ref_str . $line_str;
}
}
if ( $translation->hasFlags() ) {
$lines[] = '#, ' . implode( ',', $translation->getFlags() );
}
$prefix = $translation->isDisabled() ? '#~ ' : '';
if ( $translation->hasContext() ) {
$lines[] = $prefix . 'msgctxt ' . self::convertString( $translation->getContext() );
}
self::addLines( $lines, $prefix . 'msgid', $translation->getOriginal() );
if ( $translation->hasPlural() ) {
self::addLines( $lines, $prefix . 'msgid_plural', $translation->getPlural() );
for ( $i = 0; $i <= $plural_size; $i++ ) {
self::addLines( $lines, $prefix . 'msgstr[' . $i . ']', '' );
}
} else {
self::addLines( $lines, $prefix . 'msgstr', $translation->getTranslation() );
}
$lines[] = '';
}
$string_lines = array_filter( $lines, 'is_string' );
return implode( "\n", $string_lines );
}
/**
* Escapes and adds double quotes to a string.
*
* @param string $text Multiline string.
*
* @return string[]
*/
protected static function wpMultilineQuote( $text ) {
$lines = explode( "\n", $text );
$last = count( $lines ) - 1;
foreach ( $lines as $k => $line ) {
if ( $k === $last ) {
$lines[ $k ] = self::convertString( $line );
} else {
$lines[ $k ] = self::convertString( $line . "\n" );
}
}
return $lines;
}
/**
* Add one or more lines depending whether the string is multiline or not.
*
* @param array<mixed> &$lines Array lines should be added to.
* @param string $name Name of the line, e.g. msgstr or msgid_plural.
* @param string $value The line to add.
* @return void
*/
protected static function addLines( array &$lines, $name, $value ) {
$newlines = self::wpMultilineQuote( $value );
if ( count( $newlines ) === 1 ) {
$lines[] = $name . ' ' . $newlines[0];
} else {
$lines[] = $name . ' ""';
foreach ( $newlines as $line ) {
$lines[] = $line;
}
}
}
}