forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmo.php
More file actions
193 lines (169 loc) · 6.86 KB
/
mo.php
File metadata and controls
193 lines (169 loc) · 6.86 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
<?php
/**
* @group pomo
*/
class Tests_POMO_MO extends WP_UnitTestCase {
public function test_mo_simple() {
$mo = new MO();
$mo->import_from_file( DIR_TESTDATA . '/pomo/simple.mo' );
$this->assertSame(
array(
'Project-Id-Version' => 'WordPress 2.6-bleeding',
'Report-Msgid-Bugs-To' => 'wp-polyglots@lists.automattic.com',
),
$mo->headers
);
$this->assertCount( 2, $mo->entries );
$this->assertSame( array( 'dyado' ), $mo->entries['baba']->translations );
$this->assertSame( array( 'yes' ), $mo->entries["kuku\nruku"]->translations );
}
public function test_mo_plural() {
$mo = new MO();
$mo->import_from_file( DIR_TESTDATA . '/pomo/plural.mo' );
$this->assertCount( 1, $mo->entries );
$this->assertSame( array( 'oney dragoney', 'twoey dragoney', 'manyey dragoney', 'manyeyey dragoney', 'manyeyeyey dragoney' ), $mo->entries['one dragon']->translations );
$this->assertSame( 'oney dragoney', $mo->translate_plural( 'one dragon', '%d dragons', 1 ) );
$this->assertSame( 'twoey dragoney', $mo->translate_plural( 'one dragon', '%d dragons', 2 ) );
$this->assertSame( 'twoey dragoney', $mo->translate_plural( 'one dragon', '%d dragons', -8 ) );
$mo->set_header( 'Plural-Forms', 'nplurals=5; plural=0' );
$this->assertSame( 'oney dragoney', $mo->translate_plural( 'one dragon', '%d dragons', 1 ) );
$this->assertSame( 'oney dragoney', $mo->translate_plural( 'one dragon', '%d dragons', 2 ) );
$this->assertSame( 'oney dragoney', $mo->translate_plural( 'one dragon', '%d dragons', -8 ) );
$mo->set_header( 'Plural-Forms', 'nplurals=5; plural=n%10==1 && n%100!=11 ? 0 : n%10>=2 && n%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2;' );
$this->assertSame( 'oney dragoney', $mo->translate_plural( 'one dragon', '%d dragons', 1 ) );
$this->assertSame( 'manyey dragoney', $mo->translate_plural( 'one dragon', '%d dragons', 11 ) );
$this->assertSame( 'twoey dragoney', $mo->translate_plural( 'one dragon', '%d dragons', 3 ) );
$mo->set_header( 'Plural-Forms', 'nplurals=2; plural=n !=1;' );
$this->assertSame( 'oney dragoney', $mo->translate_plural( 'one dragon', '%d dragons', 1 ) );
$this->assertSame( 'twoey dragoney', $mo->translate_plural( 'one dragon', '%d dragons', 2 ) );
$this->assertSame( 'twoey dragoney', $mo->translate_plural( 'one dragon', '%d dragons', -8 ) );
}
public function test_mo_context() {
$mo = new MO();
$mo->import_from_file( DIR_TESTDATA . '/pomo/context.mo' );
$this->assertCount( 2, $mo->entries );
$plural_entry = new Translation_Entry(
array(
'singular' => 'one dragon',
'plural' => '%d dragons',
'translations' => array( 'oney dragoney', 'twoey dragoney', 'manyey dragoney' ),
'context' => 'dragonland',
)
);
$this->assertEquals( $plural_entry, $mo->entries[ $plural_entry->key() ] );
$this->assertSame( 'dragonland', $mo->entries[ $plural_entry->key() ]->context );
$single_entry = new Translation_Entry(
array(
'singular' => 'one dragon',
'translations' => array( 'oney dragoney' ),
'context' => 'not so dragon',
)
);
$this->assertEquals( $single_entry, $mo->entries[ $single_entry->key() ] );
$this->assertSame( 'not so dragon', $mo->entries[ $single_entry->key() ]->context );
}
public function test_translations_merge() {
$host = new Translations();
$host->add_entry( new Translation_Entry( array( 'singular' => 'pink' ) ) );
$host->add_entry( new Translation_Entry( array( 'singular' => 'green' ) ) );
$guest = new Translations();
$guest->add_entry( new Translation_Entry( array( 'singular' => 'green' ) ) );
$guest->add_entry( new Translation_Entry( array( 'singular' => 'red' ) ) );
$host->merge_with( $guest );
$this->assertCount( 3, $host->entries );
$this->assertSame( array(), array_diff( array( 'pink', 'green', 'red' ), array_keys( $host->entries ) ) );
}
public function test_export_mo_file() {
$entries = array();
$entries[] = new Translation_Entry(
array(
'singular' => 'pink',
'translations' => array( 'розов' ),
)
);
$no_translation_entry = new Translation_Entry( array( 'singular' => 'grey' ) );
$entries[] = new Translation_Entry(
array(
'singular' => 'green',
'plural' => 'greens',
'translations' => array( 'зелен', 'зелени' ),
)
);
$entries[] = new Translation_Entry(
array(
'singular' => 'red',
'context' => 'color',
'translations' => array( 'червен' ),
)
);
$entries[] = new Translation_Entry(
array(
'singular' => 'red',
'context' => 'bull',
'translations' => array( 'бик' ),
)
);
$entries[] = new Translation_Entry(
array(
'singular' => 'maroon',
'plural' => 'maroons',
'context' => 'context',
'translations' => array( 'пурпурен', 'пурпурни' ),
)
);
$mo = new MO();
$mo->set_header( 'Project-Id-Version', 'Baba Project 1.0' );
foreach ( $entries as $entry ) {
$mo->add_entry( $entry );
}
$mo->add_entry( $no_translation_entry );
$temp_fn = $this->temp_filename();
$mo->export_to_file( $temp_fn );
$again = new MO();
$again->import_from_file( $temp_fn );
$this->assertSame( count( $entries ), count( $again->entries ) );
foreach ( $entries as $entry ) {
$this->assertEquals( $entry, $again->entries[ $entry->key() ] );
}
}
public function test_export_should_not_include_empty_translations() {
$entries = array();
$mo = new MO();
$mo->add_entry(
array(
'singular' => 'baba',
'translations' => array( '', '' ),
)
);
$temp_fn = $this->temp_filename();
$mo->export_to_file( $temp_fn );
$again = new MO();
$again->import_from_file( $temp_fn );
$this->assertCount( 0, $again->entries );
}
public function test_nplurals_with_backslashn() {
$mo = new MO();
$mo->import_from_file( DIR_TESTDATA . '/pomo/bad_nplurals.mo' );
$this->assertSame( '%d foro', $mo->translate_plural( '%d forum', '%d forums', 1 ) );
$this->assertSame( '%d foros', $mo->translate_plural( '%d forum', '%d forums', 2 ) );
$this->assertSame( '%d foros', $mo->translate_plural( '%d forum', '%d forums', -1 ) );
}
public function disabled_test_performance() {
$start = microtime( true );
$mo = new MO();
$mo->import_from_file( DIR_TESTDATA . '/pomo/de_DE-2.8.mo' );
// echo "\nPerformance: ".(microtime(true) - $start)."\n";
}
public function test_overloaded_mb_functions() {
if ( ( ini_get( 'mbstring.func_overload' ) & 2 ) === 0 ) {
$this->markTestSkipped( 'This test requires mbstring.func_overload to be enabled.' );
}
$mo = new MO();
$mo->import_from_file( DIR_TESTDATA . '/pomo/overload.mo' );
$this->assertSame( array( 'Табло' ), $mo->entries['Dashboard']->translations );
}
public function test_load_pot_file() {
$mo = new MO();
$this->assertFalse( $mo->import_from_file( DIR_TESTDATA . '/pomo/mo.pot' ) );
}
}