We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent c98db08 commit 1adc0d6Copy full SHA for 1adc0d6
2 files changed
src/XML/Assert/LanguageTrait.php
@@ -11,7 +11,26 @@
11
*/
12
trait LanguageTrait
13
{
14
- private static string $language_regex = '/^([a-z]{2}|[i]-[a-z]+|[x]-[a-z]{1,8})(-[a-z]{1,8})*$/Di';
+ /**
15
+ * BCP 47 language tag validator (RFC 5646)
16
+ * - Full syntax validation including grandfathered and private-use tags
17
+ * - Named capture groups for easy subtag extraction
18
+ * - 'x' flag + comments + whitespace for maximum readability
19
+ * - Case-insensitive (/i)
20
+ */
21
+ private static string $language_regex = '/^
22
+ (?:
23
+ (
24
+ en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux
25
+ |i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE
26
+ )
27
+ |(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang)
28
+ )$
29
+ |^((?:[a-z]{2,3}(?:(?:-[a-z]{3}){1,3})?)|[a-z]{4}|[a-z]{5,8})(?:-([a-z]{4}))?(?:-([a-z]{2}|\d{3}))
30
+ ?((?:-(?:[\da-z]{5,8}|\d[\da-z]{3}))*)
31
+ ?((?:-[\da-wy-z](?:-[\da-z]{2,8})+)*)
32
+ ?(-x(?:-[\da-z]{1,8})+)?$
33
+ |^(x(?:-[\da-z]{1,8})+)$/mxi';
34
35
36
/**
tests/XML/Assert/LanguageTest.php
@@ -43,9 +43,10 @@ public static function provideValidLanguage(): array
43
return [
44
'one part' => [true, 'es'],
45
'two parts' => [true, 'en-US'],
46
- 'many parts' => [true, 'es-this-goes-on-forever'],
47
- 'x-case' => [true, 'x-klingon'],
48
- 'i-case' => [true, 'i-sami-no'],
+ 'many parts' => [true, 'tlh-Kore-AQ-fonipa'],
+ 'three-letter primary code' => [true, 'nso'],
+ 'x-case' => [true, 'x-my-custom-language'],
49
+ 'i-case' => [true, 'i-klingon'],
50
];
51
}
52
@@ -59,6 +60,7 @@ public static function provideInvalidLanguage(): array
59
60
'empty string' => [false, ''],
61
'whitespace' => [false, 'en- us'],
62
'too long' => [false, 'toolongLanguageCode'],
63
+ 'not-grandfathered' => [false, 'i-sami-no'],
64
65
66
0 commit comments