43 public function flatten( array $unflat ): array {
46 foreach ( $unflat as $key => $value ) {
47 if ( !is_array( $value ) ) {
53 if ( $this->parseCLDRPlurals ) {
57 if ( $this->parseCLDRPlurals && $plurals ) {
58 $flat[$key] = $plurals;
61 foreach ( $value as $subKey => $subValue ) {
62 $newKey =
"$key{$this->sep}$subKey";
63 $temp[$newKey] = $subValue;
65 $flat += $this->
flatten( $temp );
69 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 MWException(
"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'] :
'';
137 if ( $this->parseCLDRPlurals ) {
138 $unflattenedPlurals = [];
139 foreach ( $flat as $key => $value ) {
141 if ( !is_array( $value ) ) {
142 $plurals = $this->unflattenCLDRPlurals( $key, $value );
144 if ( is_array( $plurals ) ) {
145 $unflattenedPlurals += $plurals;
147 $unflattenedPlurals[$key] = $value;
150 $flat = $unflattenedPlurals;
153 foreach ( $flat as $key => $value ) {
154 $path = explode( $this->sep, $key );
155 if ( count( $path ) === 1 ) {
156 $unflat[$key] = $value;
163 $level = array_shift( $path );
164 if ( !isset( $pointer[$level] ) ) {
165 $pointer[$level] = [];
169 $tmpPointer = &$pointer[$level];
171 $pointer = &$tmpPointer;
172 unset( $tmpPointer );
175 if ( count( $path ) === 1 ) {
176 $lastKey = array_shift( $path );
177 $pointer[$lastKey] = $value;
179 }
while ( count( $path ) );
191 if ( strpos( $message,
'{{PLURAL' ) ===
false ) {
199 $regex =
'/\{[a-z_-]+}/i';
203 while ( preg_match( $regex, $message, $match ) ) {
204 $uniqkey = Utilities::getPlaceholder();
205 $placeholders[$uniqkey] = $match[0];
206 $search = preg_quote( $match[0],
'~' );
207 $message = preg_replace(
"~$search~", $uniqkey, $message );
211 $regex =
'~\{\{PLURAL\|(.*?)}}~s';
215 while ( preg_match( $regex, $message, $match ) ) {
216 $uniqkey = Utilities::getPlaceholder();
217 $matches[$uniqkey] = $match;
218 $message = preg_replace( $regex, $uniqkey, $message, 1 );
222 if ( !count( $matches ) ) {
234 $pluralChoice = implode(
'|', array_keys( self::$pluralWords ) );
235 $regex =
"~($pluralChoice)\s*=\s*(.*)~s";
236 foreach ( $matches as $ph => $plu ) {
237 $forms = explode(
'|', $plu[1] );
239 foreach ( $forms as $form ) {
241 if ( preg_match( $regex, $form, $match ) ) {
242 $formWord =
"$key{$this->sep}{$match[1]}";
245 $formWord =
"$key{$this->sep}other";
249 if ( !isset( $alts[$formWord] ) ) {
250 $alts[$formWord] = $message;
253 $string = $alts[$formWord];
254 $alts[$formWord] = str_replace( $ph, $value, $string );
259 foreach ( $alts as &$value ) {
260 $value = str_replace( array_keys( $placeholders ), array_values( $placeholders ), $value );
263 if ( !isset( $alts[
"$key{$this->sep}other"] ) ) {
265 $alts[
"$key{$this->sep}other"] = end( $alts );