41 public function flatten( array $unflat ): array {
44 foreach ( $unflat as $key => $value ) {
45 if ( !is_array( $value ) ) {
51 if ( $this->parseCLDRPlurals ) {
55 if ( $this->parseCLDRPlurals && $plurals ) {
56 $flat[$key] = $plurals;
59 foreach ( $value as $subKey => $subValue ) {
60 $newKey =
"$key{$this->sep}$subKey";
61 $temp[$newKey] = $subValue;
63 $flat += $this->
flatten( $temp );
67 unset( $unflat[$key] );
79 $hasNonPluralKeys =
false;
81 foreach ( $messages as $key => $value ) {
82 if ( is_array( $value ) ) {
88 if ( isset( self::PLURAL_WORDS[$key] ) ) {
91 $hasNonPluralKeys =
true;
101 if ( $hasNonPluralKeys ) {
103 if ( $pluralKeys === [
'other' ] ) {
107 $keys = implode(
', ', array_keys( $messages ) );
108 throw new InvalidArgumentException(
"Reserved plural keywords mixed with other keys: $keys." );
112 foreach ( $messages as $key => $value ) {
113 if ( $key ===
'other' ) {
117 $pls .=
"|$key=$value";
121 $other = isset( $messages[
'other'] ) ?
'|' . $messages[
'other'] :
'';
135 if ( $this->parseCLDRPlurals ) {
136 $unflattenedPlurals = [];
137 foreach ( $flat as $key => $value ) {
139 if ( !is_array( $value ) ) {
140 $plurals = $this->unflattenCLDRPlurals( $key, $value );
142 if ( is_array( $plurals ) ) {
143 $unflattenedPlurals += $plurals;
145 $unflattenedPlurals[$key] = $value;
148 $flat = $unflattenedPlurals;
151 foreach ( $flat as $key => $value ) {
152 $path = explode( $this->sep, $key );
153 if ( count( $path ) === 1 ) {
154 $unflat[$key] = $value;
161 $level = array_shift( $path );
162 if ( !isset( $pointer[$level] ) ) {
163 $pointer[$level] = [];
167 $tmpPointer = &$pointer[$level];
169 $pointer = &$tmpPointer;
170 unset( $tmpPointer );
173 if ( count( $path ) === 1 ) {
174 $lastKey = array_shift( $path );
175 $pointer[$lastKey] = $value;
177 }
while ( count( $path ) );
189 if ( !str_contains( $message,
'{{PLURAL' ) ) {
197 $regex =
'/\{[a-z_-]+}/i';
201 while ( preg_match( $regex, $message, $match ) ) {
202 $uniqkey = Utilities::getPlaceholder();
203 $placeholders[$uniqkey] = $match[0];
204 $search = preg_quote( $match[0],
'~' );
205 $message = preg_replace(
"~$search~", $uniqkey, $message );
209 $regex =
'~\{\{PLURAL\|(.*?)}}~s';
213 while ( preg_match( $regex, $message, $match ) ) {
214 $uniqkey = Utilities::getPlaceholder();
215 $matches[$uniqkey] = $match;
216 $message = preg_replace( $regex, $uniqkey, $message, 1 );
220 if ( !count( $matches ) ) {
232 $pluralChoice = implode(
'|', array_keys( self::PLURAL_WORDS ) );
233 $regex =
"~($pluralChoice)\s*=\s*(.*)~s";
234 foreach ( $matches as $ph => $plu ) {
235 $forms = explode(
'|', $plu[1] );
237 foreach ( $forms as $form ) {
239 if ( preg_match( $regex, $form, $match ) ) {
240 $formWord =
"$key{$this->sep}{$match[1]}";
243 $formWord =
"$key{$this->sep}other";
247 if ( !isset( $alts[$formWord] ) ) {
248 $alts[$formWord] = $message;
251 $string = $alts[$formWord];
252 $alts[$formWord] = str_replace( $ph, $value, $string );
257 foreach ( $alts as &$value ) {
258 $value = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $value );
261 if ( !isset( $alts[
"$key{$this->sep}other"] ) ) {
263 $alts[
"$key{$this->sep}other"] = end( $alts );