-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgettext-context.php
105 lines (89 loc) · 3.96 KB
/
gettext-context.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
if (! function_exists('pgettext')) {
/**
* Lookup a message in the current domain for the specified context.
*
* @param string $context Context of the message
* @param string $message The message being translated
* @return string Translated string if one is found in the translation table, or the submitted message if not found
*/
function pgettext(string $context, string $message): string
{
$context_message = "$context\004$message";
$translation = gettext($context_message);
// If the translation was not found...
if ($translation === $context_message) {
// Return original message
return $message;
}
return $translation;
}
}
if (! function_exists('npgettext')) {
/**
* The plural version of pgettext(). Some languages have more than one form for plural messages dependent on the count.
*
* @param string $context Context of the message
* @param string $singular The singular message ID
* @param string $plural The plural message ID
* @param int $number The number (e.g. item count) to determine the translation for the respective grammatical number
* @return string Correct plural form of message identified by $singular and $plural for $number
*/
function npgettext(string $context, string $singular, string $plural, int $number): string
{
$context_singular = "$context\004$singular";
$context_plural = "$context\004$plural";
$translation = ngettext($context_singular, $context_plural, $number);
// If the translation was not found...
if ($translation === $context_singular || $translation === $context_plural) {
// Use native function to return the appropriate string
return ngettext($singular, $plural, $number);
}
return $translation;
}
}
if (! function_exists('dpgettext')) {
/**
* The dpgettext() function allows you to override the current domain for a single message lookup for the specified context.
*
* @param string $domain The domain
* @param string $context Context of the message
* @param string $message The message being translated
* @return string Translated string if one is found in the translation table, or the submitted message if not found
*/
function dpgettext(string $domain, string $context, string $message): string
{
$context_message = "$context\004$message";
$translation = dgettext($domain, $context_message);
// If the translation was not found...
if ($translation === $context_message) {
// Return original message
return $message;
}
return $translation;
}
}
if (! function_exists('dnpgettext')) {
/**
* The dnpgettext() function allows you to override the current domain for a single plural message lookup for the specified context.
*
* @param string $domain The domain
* @param string $context Context of the message
* @param string $singular The singular message ID
* @param string $plural The plural message ID
* @param int $number The number (e.g. item count) to determine the translation for the respective grammatical number
* @return string Translated string if one is found in the translation table, or the submitted message if not found
*/
function dnpgettext(string $domain, string $context, string $singular, string $plural, int $number): string
{
$context_singular = "$context\004$singular";
$context_plural = "$context\004$plural";
$translation = dngettext($domain, $context_singular, $context_plural, $number);
// If the translation was not found...
if ($translation === $context_singular || $translation === $context_plural) {
// Use native function to return the appropriate string
return dngettext($domain, $singular, $plural, $number);
}
return $translation;
}
}