Skip to content

Commit ffae2fc

Browse files
committed
fix: corrected plural handling in Arabic
1 parent 21af8fd commit ffae2fc

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

composer.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpmyfaq/src/phpMyFAQ/Language/Plurals.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
<?php
2-
3-
declare(strict_types=1);
1+
<?php declare(strict_types=1);
42

53
/**
64
* The plural class provides support for plural forms in phpMyFAQ translations.
@@ -64,6 +62,13 @@ private function plural(string $language, int $number): int
6462
{
6563
switch ($language) {
6664
case 'ar':
65+
// Arabic has 6 plural forms following GNU gettext standard:
66+
// Form 0: zero (n==0)
67+
// Form 1: one (n==1)
68+
// Form 2: two (n==2)
69+
// Form 3: few (n%100>=3 && n%100<=10) - e.g., 3, 10, 103, 210
70+
// Form 4: many (n%100>=11 && n%100<=99) - e.g., 11, 99, 111, 199
71+
// Form 5: other (everything else) - e.g., 100, 101, 102, 200, 1000
6772
if ($number === 0) {
6873
return 0;
6974
}
@@ -77,7 +82,7 @@ private function plural(string $language, int $number): int
7782
if ($n100 >= 3 && $n100 <= 10) {
7883
return 3;
7984
}
80-
if ($n100 >= 11 || $n100 === 1 || $n100 === 2) {
85+
if ($n100 >= 11 && $n100 <= 99) {
8186
return 4;
8287
}
8388
return 5;

phpmyfaq/src/phpMyFAQ/Strings.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ public static function preg_match_all(
147147
/**
148148
* Split string by a regexp.
149149
*/
150-
public static function preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array|bool // phpcs:ignore
151-
{
150+
public static function preg_split(string $pattern, string $subject, int $limit = -1, int $flags = 0): array|bool
151+
{ // phpcs:ignore
152152
return self::$instance->preg_split($pattern, $subject, $limit, $flags);
153153
}
154154

tests/phpMyFAQ/Language/PluralsTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,44 @@ public function testPluralFormsArabic(): void
111111
$this->assertEquals(5, $this->pluralMethod->invoke($this->plurals, 'ar', 100));
112112
}
113113

114+
/**
115+
* Test Arabic plural edge cases including numbers ending in 01-02
116+
* @throws \ReflectionException
117+
*/
118+
public function testArabicPluralEdgeCases(): void
119+
{
120+
// Form 0: zero
121+
$this->assertEquals(0, $this->pluralMethod->invoke($this->plurals, 'ar', 0));
122+
123+
// Form 1: one
124+
$this->assertEquals(1, $this->pluralMethod->invoke($this->plurals, 'ar', 1));
125+
126+
// Form 2: two
127+
$this->assertEquals(2, $this->pluralMethod->invoke($this->plurals, 'ar', 2));
128+
129+
// Form 3: 3-10 (last two digits)
130+
$this->assertEquals(3, $this->pluralMethod->invoke($this->plurals, 'ar', 3));
131+
$this->assertEquals(3, $this->pluralMethod->invoke($this->plurals, 'ar', 10));
132+
$this->assertEquals(3, $this->pluralMethod->invoke($this->plurals, 'ar', 103));
133+
$this->assertEquals(3, $this->pluralMethod->invoke($this->plurals, 'ar', 110));
134+
$this->assertEquals(3, $this->pluralMethod->invoke($this->plurals, 'ar', 203));
135+
136+
// Form 4: 11-99 (last two digits)
137+
$this->assertEquals(4, $this->pluralMethod->invoke($this->plurals, 'ar', 11));
138+
$this->assertEquals(4, $this->pluralMethod->invoke($this->plurals, 'ar', 99));
139+
$this->assertEquals(4, $this->pluralMethod->invoke($this->plurals, 'ar', 111));
140+
$this->assertEquals(4, $this->pluralMethod->invoke($this->plurals, 'ar', 199));
141+
142+
// Form 5: 100, 101, 102, etc.
143+
$this->assertEquals(5, $this->pluralMethod->invoke($this->plurals, 'ar', 100));
144+
$this->assertEquals(5, $this->pluralMethod->invoke($this->plurals, 'ar', 101));
145+
$this->assertEquals(5, $this->pluralMethod->invoke($this->plurals, 'ar', 102));
146+
$this->assertEquals(5, $this->pluralMethod->invoke($this->plurals, 'ar', 200));
147+
$this->assertEquals(5, $this->pluralMethod->invoke($this->plurals, 'ar', 201));
148+
$this->assertEquals(5, $this->pluralMethod->invoke($this->plurals, 'ar', 202));
149+
$this->assertEquals(5, $this->pluralMethod->invoke($this->plurals, 'ar', 1001));
150+
}
151+
114152
/**
115153
* @throws \ReflectionException
116154
*/

0 commit comments

Comments
 (0)