Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
SmartFormatPlural.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\Extension\Translate\Utilities;
8
41 public static function getPluralInstances( string $text ): array {
42 // ldns = Large Deeply-Nested Structure
43 $ldns = [];
44
45 // Named variables seem to be supported by the spec, but we limit ourselves
46 // only to numbers. Example syntax {0:message|messages}
47 $regex = '/\{(\d+):([^}]+)\}/Us';
48 $matches = [];
49 preg_match_all( $regex, $text, $matches, PREG_SET_ORDER );
50
51 foreach ( $matches as $instance ) {
52 $original = $instance[ 0 ];
53 $variable = $instance[ 1 ];
54 $forms = explode( '|', $instance[ 2 ] );
55 $ldns[ $variable ] = $ldns[ $variable ] ?? [];
56 $ldns[ $variable ][] = [
57 'forms' => $forms,
58 'original' => $original,
59 ];
60 }
61
62 return $ldns;
63 }
64}
Implements partial support for SmartFormat plural syntax parsing.
static getPluralInstances(string $text)
Example input: {0} {0:message|messages} older than {1} {1:week|weeks} {0:has|have} been deleted.