locale_test.module
5.42 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* @file
* Mock module for locale layer tests.
*/
/**
* Implements hook_locale().
*/
function locale_test_locale($op = 'groups') {
switch ($op) {
case 'groups':
return array('custom' => t('Custom'));
}
}
/**
* Implements hook_boot().
*
* For testing domain language negotiation, we fake it by setting
* the HTTP_HOST here
*/
function locale_test_boot() {
if (variable_get('locale_test_domain')) {
$_SERVER['HTTP_HOST'] = variable_get('locale_test_domain');
}
}
/**
* Implements hook_init().
*/
function locale_test_init() {
locale_test_store_language_negotiation();
if (isset($GLOBALS['language']) && isset($GLOBALS['language']->provider)) {
drupal_set_message(t('Language negotiation provider: @name', array('@name' => $GLOBALS['language']->provider)));
}
}
/**
* Implements hook_language_types_info().
*/
function locale_test_language_types_info() {
if (variable_get('locale_test_language_types', FALSE)) {
return array(
'test_language_type' => array(
'name' => t('Test'),
'description' => t('A test language type.'),
),
'fixed_test_language_type' => array(
'fixed' => array('test_language_provider'),
),
);
}
}
/**
* Implements hook_menu().
*
* @return array
*/
function locale_test_menu() {
$items = array();
$items['locale_test_plural_format_page'] = array(
'page callback' => 'locale_test_plural_format_page',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_language_types_info_alter().
*/
function locale_test_language_types_info_alter(array &$language_types) {
if (variable_get('locale_test_content_language_type', FALSE)) {
unset($language_types[LANGUAGE_TYPE_CONTENT]['fixed']);
}
}
/**
* Implements hook_language_negotiation_info().
*/
function locale_test_language_negotiation_info() {
if (variable_get('locale_test_language_negotiation_info', FALSE)) {
$info = array(
'callbacks' => array(
'language' => 'locale_test_language_provider',
),
'file' => drupal_get_path('module', 'locale_test') .'/locale_test.module',
'weight' => -10,
'description' => t('This is a test language provider.'),
);
return array(
'test_language_provider' => array(
'name' => t('Test'),
'types' => array(LANGUAGE_TYPE_CONTENT, 'test_language_type', 'fixed_test_language_type'),
) + $info,
'test_language_provider_ts' => array(
'name' => t('Type-specific test'),
'types' => array('test_language_type'),
) + $info,
);
}
}
/**
* Implements hook_language_negotiation_info_alter().
*/
function locale_test_language_negotiation_info_alter(array &$language_providers) {
if (variable_get('locale_test_language_negotiation_info_alter', FALSE)) {
unset($language_providers[LOCALE_LANGUAGE_NEGOTIATION_INTERFACE]);
}
}
/**
* Store the last negotiated languages.
*/
function locale_test_store_language_negotiation() {
$last = array();
foreach (language_types() as $type) {
$last[$type] = $GLOBALS[$type]->language;
}
variable_set('locale_test_language_negotiation_last', $last);
}
/**
* Test language provider.
*/
function locale_test_language_provider($languages) {
return 'it';
}
/**
* Returns markup for locale_get_plural testing.
*
* @return array
*/
function locale_test_plural_format_page() {
$tests = _locale_test_plural_format_tests();
$result = array();
foreach ($tests as $test) {
$string_param = array(
'@lang' => $test['language'],
'@locale_get_plural' => locale_get_plural($test['count'], $test['language'])
);
$result[] = array(
'#prefix' => '<br/>',
'#markup' => format_string('Language: @lang, locale_get_plural: @locale_get_plural.', $string_param),
);
}
return $result;
}
/**
* Helper function with list of test cases
*
* @return array
*/
function _locale_test_plural_format_tests() {
return array(
// Test data for English (no formula present).
array(
'count' => 1,
'language' => 'en',
'expected-result' => 0,
),
array(
'count' => 0,
'language' => 'en',
'expected-result' => 1,
),
array(
'count' => 5,
'language' => 'en',
'expected-result' => 1,
),
// Test data for French (simpler formula).
array(
'count' => 1,
'language' => 'fr',
'expected-result' => 0,
),
array(
'count' => 0,
'language' => 'fr',
'expected-result' => 1,
),
array(
'count' => 5,
'language' => 'fr',
'expected-result' => 1,
),
// Test data for Croatian (more complex formula).
array(
'count' => 1,
'language' => 'hr',
'expected-result' => 0,
),
array(
'count' => 21,
'language' => 'hr',
'expected-result' => 0,
),
array(
'count' => 0,
'language' => 'hr',
'expected-result' => 2,
),
array(
'count' => 2,
'language' => 'hr',
'expected-result' => 1,
),
array(
'count' => 8,
'language' => 'hr',
'expected-result' => 2,
),
// Test data for Hungarian (nonexistent language).
array(
'count' => 1,
'language' => 'hu',
'expected-result' => -1,
),
array(
'count' => 21,
'language' => 'hu',
'expected-result' => -1,
),
array(
'count' => 0,
'language' => 'hu',
'expected-result' => -1,
),
);
}