111 $pre = preg_quote( self::PRE,
'/' );
112 $post = preg_quote( self::POST,
'/' );
114 $ok = preg_match_all(
"/$pre(.*)$post/Us", $text, $m );
115 if ( $ok ===
false ) {
116 throw new RuntimeException(
"Plural regular expression failed for text: $text" );
122 foreach ( $m[0] as $instanceIndex => $instanceText ) {
123 $ph = TranslateUtils::getPlaceholder();
126 $pattern =
'/' . preg_quote( $instanceText,
'/' ) .
'/';
127 $template = preg_replace( $pattern, $ph, $template, 1 );
130 foreach ( explode(
'|', $m[ 1 ][ $instanceIndex ] ) as $form ) {
132 $ok = preg_match(
"~\s*([a-z]+)\s*=(.+)~s", $form, $m2 );
133 $keyword = $ok ? $m2[ 1 ] :
'other';
134 $value = $ok ? trim( $m2[ 2 ] ) : $form;
135 $instanceForms[] = [ $keyword, $value ];
138 $instanceMap[$ph] = $instanceForms;
141 return [ $template, $instanceMap ];
152 public static function expandTemplate( $template, array $instanceMap, $expectedKeywords ) {
156 foreach ( $instanceMap as $ph => $list ) {
157 $instanceMap[ $ph ] = self::convertFormListToFormMap( $list, $expectedKeywords );
160 foreach ( $expectedKeywords as $keyword ) {
166 foreach ( $instanceMap as $ph => $instanceFormMap ) {
168 $replacement = $instanceFormMap[ $keyword ] ??
'';
169 $form = str_replace( $ph, $replacement, $form );
172 $formArray[ $keyword ] = $form;