Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4086664
SmartFormatPlural.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
SmartFormatPlural.php
View Options
<?php
/**
* @file
* @license GPL-2.0-or-later
*/
namespace
MediaWiki\Extension\Translate\Utilities
;
/**
* Implements partial support for SmartFormat plural syntax parsing.
* @see https://github.com/axuno/SmartFormat/wiki/Pluralization
* @since 2019.11
*/
class
SmartFormatPlural
{
/**
* Example input:
* {0} {0:message|messages} older than {1} {1:week|weeks} {0:has|have} been deleted.
* Example output:
* [
* '0' => [
* [
* 'forms' => [ 'message', 'messages' ],
* 'original' => '{0:message|messages}',
* ],
* [
* 'forms' => [ 'has', 'have' ],
* 'original' => '{0:has|have}',
* ],
* ],
* '1' => [
* [
* 'forms' => [ 'week', 'weeks' ],
* 'original' => '{1:week|weeks}',
* ],
* ],
* ]
*
* @param string $text
* @return array
*/
public
static
function
getPluralInstances
(
string
$text
):
array
{
// ldns = Large Deeply-Nested Structure
$ldns
=
[];
// Named variables seem to be supported by the spec, but we limit ourselves
// only to numbers. Example syntax {0:message|messages}
$regex
=
'/
\{
(
\d
+):([^}]+)
\}
/Us'
;
$matches
=
[];
preg_match_all
(
$regex
,
$text
,
$matches
,
PREG_SET_ORDER
);
foreach
(
$matches
as
$instance
)
{
$original
=
$instance
[
0
];
$variable
=
$instance
[
1
];
$forms
=
explode
(
'|'
,
$instance
[
2
]
);
$ldns
[
$variable
]
??=
[];
$ldns
[
$variable
][]
=
[
'forms'
=>
$forms
,
'original'
=>
$original
,
];
}
return
$ldns
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Aug 18 2026, 09:06 (6 w, 4 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
03/af/c4a02455ed593c2521dba4113cb3
Default Alt Text
SmartFormatPlural.php (1 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment