44 public function flatten( array $unflat ): array {
47 foreach ( $unflat as $key => $value ) {
48 if ( !is_array( $value ) ) {
54 if ( $this->parseCLDRPlurals ) {
58 if ( $this->parseCLDRPlurals && $plurals ) {
59 $flat[$key] = $plurals;
62 foreach ( $value as $subKey => $subValue ) {
63 $newKey =
"$key{$this->sep}$subKey";
64 $temp[$newKey] = $subValue;
66 $flat += $this->
flatten( $temp );
70 unset( $unflat[$key] );
82 $hasNonPluralKeys =
false;
84 foreach ( $messages as $key => $value ) {
85 if ( is_array( $value ) ) {
91 if ( isset( self::$pluralWords[$key] ) ) {
94 $hasNonPluralKeys =
true;
104 if ( $hasNonPluralKeys ) {
106 if ( $pluralKeys === [
'other' ] ) {
110 $keys = implode(
', ', array_keys( $messages ) );
111 throw new InvalidArgumentException(
"Reserved plural keywords mixed with other keys: $keys." );
115 foreach ( $messages as $key => $value ) {
116 if ( $key ===
'other' ) {
120 $pls .=
"|$key=$value";
124 $other = isset( $messages[
'other'] ) ?
'|' . $messages[
'other'] :
'';
138 if ( $this->parseCLDRPlurals ) {
139 $unflattenedPlurals = [];
140 foreach ( $flat as $key => $value ) {
142 if ( !is_array( $value ) ) {
143 $plurals = $this->unflattenCLDRPlurals( $key, $value );
145 if ( is_array( $plurals ) ) {
146 $unflattenedPlurals += $plurals;
148 $unflattenedPlurals[$key] = $value;
151 $flat = $unflattenedPlurals;
154 foreach ( $flat as $key => $value ) {
155 $path = explode( $this->sep, $key );
156 if ( count( $path ) === 1 ) {
157 $unflat[$key] = $value;
164 $level = array_shift( $path );
165 if ( !isset( $pointer[$level] ) ) {
166 $pointer[$level] = [];
170 $tmpPointer = &$pointer[$level];
172 $pointer = &$tmpPointer;
173 unset( $tmpPointer );
176 if ( count( $path ) === 1 ) {
177 $lastKey = array_shift( $path );
178 $pointer[$lastKey] = $value;
180 }
while ( count( $path ) );
192 if ( !str_contains( $message,
'{{PLURAL' ) ) {
200 $regex =
'/\{[a-z_-]+}/i';
204 while ( preg_match( $regex, $message, $match ) ) {
205 $uniqkey = Utilities::getPlaceholder();
206 $placeholders[$uniqkey] = $match[0];
207 $search = preg_quote( $match[0],
'~' );
208 $message = preg_replace(
"~$search~", $uniqkey, $message );
212 $regex =
'~\{\{PLURAL\|(.*?)}}~s';
216 while ( preg_match( $regex, $message, $match ) ) {
217 $uniqkey = Utilities::getPlaceholder();
218 $matches[$uniqkey] = $match;
219 $message = preg_replace( $regex, $uniqkey, $message, 1 );
223 if ( !count( $matches ) ) {
235 $pluralChoice = implode(
'|', array_keys( self::$pluralWords ) );
236 $regex =
"~($pluralChoice)\s*=\s*(.*)~s";
237 foreach ( $matches as $ph => $plu ) {
238 $forms = explode(
'|', $plu[1] );
240 foreach ( $forms as $form ) {
242 if ( preg_match( $regex, $form, $match ) ) {
243 $formWord =
"$key{$this->sep}{$match[1]}";
246 $formWord =
"$key{$this->sep}other";
250 if ( !isset( $alts[$formWord] ) ) {
251 $alts[$formWord] = $message;
254 $string = $alts[$formWord];
255 $alts[$formWord] = str_replace( $ph, $value, $string );
260 foreach ( $alts as &$value ) {
261 $value = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $value );
264 if ( !isset( $alts[
"$key{$this->sep}other"] ) ) {
266 $alts[
"$key{$this->sep}other"] = end( $alts );