42 public function flatten( array $unflat ): array {
45 foreach ( $unflat as $key => $value ) {
46 if ( !is_array( $value ) ) {
52 if ( $this->parseCLDRPlurals ) {
56 if ( $this->parseCLDRPlurals && $plurals ) {
57 $flat[$key] = $plurals;
60 foreach ( $value as $subKey => $subValue ) {
61 $newKey =
"$key{$this->sep}$subKey";
62 $temp[$newKey] = $subValue;
64 $flat += $this->
flatten( $temp );
68 unset( $unflat[$key] );
80 $hasNonPluralKeys =
false;
82 foreach ( $messages as $key => $value ) {
83 if ( is_array( $value ) ) {
89 if ( isset( self::PLURAL_WORDS[$key] ) ) {
92 $hasNonPluralKeys =
true;
102 if ( $hasNonPluralKeys ) {
104 if ( $pluralKeys === [
'other' ] ) {
108 $keys = implode(
', ', array_keys( $messages ) );
109 throw new InvalidArgumentException(
"Reserved plural keywords mixed with other keys: $keys." );
113 foreach ( $messages as $key => $value ) {
114 if ( $key ===
'other' ) {
118 $pls .=
"|$key=$value";
122 $other = isset( $messages[
'other'] ) ?
'|' . $messages[
'other'] :
'';
136 if ( $this->parseCLDRPlurals ) {
137 $unflattenedPlurals = [];
138 foreach ( $flat as $key => $value ) {
140 if ( !is_array( $value ) ) {
141 $plurals = $this->unflattenCLDRPlurals( $key, $value );
143 if ( is_array( $plurals ) ) {
144 $unflattenedPlurals += $plurals;
146 $unflattenedPlurals[$key] = $value;
149 $flat = $unflattenedPlurals;
152 foreach ( $flat as $key => $value ) {
153 $path = explode( $this->sep, $key );
154 if ( count( $path ) === 1 ) {
155 $unflat[$key] = $value;
162 $level = array_shift( $path );
163 if ( !isset( $pointer[$level] ) ) {
164 $pointer[$level] = [];
168 $tmpPointer = &$pointer[$level];
170 $pointer = &$tmpPointer;
171 unset( $tmpPointer );
174 if ( count( $path ) === 1 ) {
175 $lastKey = array_shift( $path );
176 $pointer[$lastKey] = $value;
178 }
while ( count( $path ) );
190 if ( !str_contains( $message,
'{{PLURAL' ) ) {
198 $regex =
'/\{[a-z_-]+}/i';
202 while ( preg_match( $regex, $message, $match ) ) {
203 $uniqkey = Utilities::getPlaceholder();
204 $placeholders[$uniqkey] = $match[0];
205 $search = preg_quote( $match[0],
'~' );
206 $message = preg_replace(
"~$search~", $uniqkey, $message );
210 $regex =
'~\{\{PLURAL\|(.*?)}}~s';
214 while ( preg_match( $regex, $message, $match ) ) {
215 $uniqkey = Utilities::getPlaceholder();
216 $matches[$uniqkey] = $match;
217 $message = preg_replace( $regex, $uniqkey, $message, 1 );
221 if ( !count( $matches ) ) {
233 $pluralChoice = implode(
'|', array_keys( self::PLURAL_WORDS ) );
234 $regex =
"~($pluralChoice)\s*=\s*(.*)~s";
235 foreach ( $matches as $ph => $plu ) {
236 $forms = explode(
'|', $plu[1] );
238 foreach ( $forms as $form ) {
240 if ( preg_match( $regex, $form, $match ) ) {
241 $formWord =
"$key{$this->sep}{$match[1]}";
244 $formWord =
"$key{$this->sep}other";
248 if ( !isset( $alts[$formWord] ) ) {
249 $alts[$formWord] = $message;
252 $string = $alts[$formWord];
253 $alts[$formWord] = str_replace( $ph, $value, $string );
258 foreach ( $alts as &$value ) {
259 $value = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $value );
262 if ( !isset( $alts[
"$key{$this->sep}other"] ) ) {
264 $alts[
"$key{$this->sep}other"] = end( $alts );