File size: 9,922 Bytes
1e92f2d |
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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
const path = require( 'path' );
const i18n = require( '..' );
// generate allowed file
const sourceFiles = [
'examples/i18n-test-examples.jsx',
'examples/i18n-test-example-second-file.jsx',
].map( ( file ) => path.join( __dirname, file ) );
/**
* In these tests we are taking the translate requests from
* examples/ and generating the corresponding php code and pot file
* and saving it into output.php. We then check to make sure
* the output file contains what we expect.
*/
describe( 'POT', () => {
let output;
beforeAll( () => {
output = i18n( {
projectName: 'i18nTest',
inputPaths: sourceFiles,
format: 'POT',
extras: [ 'date' ],
} );
} );
test( 'should have all the default headers', () => {
expect( output ).toEqual( expect.stringContaining( '"Project-Id-Version: _s i18nTest\\n"\n' ) );
expect( output ).toEqual( expect.stringContaining( '"Report-Msgid-Bugs-To:' ) );
expect( output ).toEqual( expect.stringContaining( '"POT-Creation-Date:' ) );
expect( output ).toEqual( expect.stringContaining( '"MIME-Version: 1.0\\n"\n' ) );
expect( output ).toEqual(
expect.stringContaining( '"Content-Type: text/plain; charset=UTF-8\\n"\n' )
);
expect( output ).toEqual( expect.stringContaining( '"Content-Transfer-Encoding: 8bit\\n"\n' ) );
expect( output ).toEqual( expect.stringContaining( '"PO-Revision-Date:' ) );
expect( output ).toEqual(
expect.stringContaining( '"Last-Translator: FULL NAME <EMAIL@ADDRESS>\\n"\n' )
);
expect( output ).toEqual(
expect.stringContaining( '"Language-Team: LANGUAGE <LL@li.org>\\n"\n' )
);
} );
test( 'should create a simple translation', () => {
expect( output ).toEqual(
expect.stringContaining( 'msgid "My hat has three corners."\nmsgstr ""\n' )
);
} );
test( 'should create a plural translation', () => {
expect( output ).toEqual(
expect.stringContaining(
'msgid "My hat has four corners."\nmsgid_plural "My hats have five corners."\nmsgstr[0] ""\n'
)
);
} );
test( 'should combine singular with plural translation', () => {
expect( output ).not.toEqual(
expect.stringContaining( 'msgid "My hat has four corners."\nmsgstr ""\n' )
);
} );
test( 'should create a context translation', () => {
expect( output ).toEqual(
expect.stringContaining( 'msgctxt "verb"\nmsgid "post"\nmsgstr ""\n' )
);
expect( output ).toEqual(
expect.stringContaining( 'msgctxt "verb2"\nmsgid "post2"\nmsgstr ""\n' )
);
} );
test( 'should prepend a translator comment', () => {
expect( output ).toEqual(
expect.stringContaining( '#. draft saved date format, see http://php.net/date' )
);
} );
test( 'should prepend the line number', () => {
expect( output ).toEqual(
expect.stringContaining(
'#: test/examples/i18n-test-examples.jsx:9\nmsgid "My hat has three corners too."'
)
);
} );
test( 'should combine strings', () => {
expect( output ).toEqual(
expect.stringMatching(
/#: test\/examples\/i18n-test-examples.jsx:\d+\n#: test\/examples\/i18n-test-examples.jsx:\d+\n#. Second ocurrence\nmsgid "My hat has three corners."/
)
);
} );
test( 'should pass through an sprintf as a regular translation', () => {
expect( output ).toEqual(
expect.stringContaining(
'msgid "Your city is %(city)s and your zip is %(zip)s."\nmsgstr ""\n'
)
);
} );
test( 'should allow string concatenating with + operator', () => {
expect( output ).toEqual(
expect.stringContaining(
[
'msgid ""',
'"This is a multi-line translation with \\"mixed quotes\\" and mixed \'single "',
'"quotes\'"',
'msgstr ""',
].join( '\n' )
)
);
} );
// we are updating the translate api for plurals to allow for:
// i18n.translate( 'single', 'plural', options );
// we will honor the prior syntax but raise a warning in the console
test( 'should handle the new plural syntax', () => {
expect( output ).toEqual(
expect.stringContaining( 'msgid "single test"\nmsgid_plural "plural test"\nmsgstr[0] ""\n' )
);
} );
test( 'should include default translations for number formatting strings', () => {
expect( output ).toEqual( expect.stringContaining( 'msgid "number_format_thousands_sep"\n' ) );
expect( output ).toEqual( expect.stringContaining( 'msgid "number_format_decimal_point"\n' ) );
} );
test( 'should find translations from multiple files', () => {
expect( output ).toEqual(
expect.stringContaining( 'msgid "My test has two files."\nmsgstr ""\n' )
);
} );
test( 'should find options with a literal string key', () => {
expect( output ).toEqual(
expect.stringContaining(
'msgctxt "context with a literal string key"\nmsgid "The string key text"\nmsgstr ""\n'
)
);
} );
test( 'should find options with new plural syntax', () => {
expect( output ).toEqual(
expect.stringContaining(
'msgctxt "context after new plural syntax"\nmsgid "My hat has one corner."\nmsgid_plural "My hat has many corners."\nmsgstr[0] ""\n'
)
);
} );
test( 'should handle template literals', () => {
expect( output ).toEqual( expect.stringContaining( 'msgid "My hat has six corners."' ) );
expect( output ).toEqual(
expect.stringContaining( 'msgid "My hat\\nhas seventeen\\ncorners."' )
);
} );
test( 'should properly handle unicode escapes', () => {
expect( output ).toEqual(
expect.stringContaining( 'This is how the test performed\\\\u2026' )
);
expect( output ).toEqual(
expect.stringContaining( "Here's how the post has performed so far\\\\u2026" )
);
} );
} );
describe( 'PHP', () => {
let output;
beforeAll( () => {
output = i18n( {
projectName: 'i18nTest',
inputPaths: sourceFiles,
phpArrayName: 'arrayName',
format: 'PHP',
extras: [ 'date' ],
} );
} );
test( 'should open with a php opening tag', () => {
expect( output ).toMatch( /^<\?php/ );
} );
test( 'should create an array with the passed-in name', () => {
expect( output ).toEqual( expect.stringContaining( '$arrayName = array(\n' ) );
} );
test( 'should create a simple __() translation', () => {
expect( output ).toEqual( expect.stringContaining( '__( "My hat has three corners." ),' ) );
} );
test( 'should create a plural _n() translation', () => {
expect( output ).toEqual(
expect.stringContaining(
'_n( "My hat has four corners.", "My hats have five corners.", 1 ),'
)
);
} );
test( 'should create a context _x() translation', () => {
expect( output ).toEqual( expect.stringContaining( '_x( "post", "verb" ),' ) );
} );
test( 'should allow `original` as initial string', () => {
expect( output ).toEqual( expect.stringContaining( '_x( "post2", "verb2" ),' ) );
} );
test( 'should prepend a translator comment', () => {
expect( output ).toEqual(
expect.stringContaining(
'/* translators: draft saved date format, see http://php.net/date */\n__( "g:i:s a" ),'
)
);
} );
test( 'should append the line number', () => {
expect( output ).toEqual(
expect.stringContaining(
'__( "My hat has three corners." ), // test/examples/i18n-test-examples.jsx:6'
)
);
} );
test( 'should pass through an sprintf as a regular __() method', () => {
expect( output ).toEqual(
expect.stringContaining( '__( "Your city is %(city)s and your zip is %(zip)s." ),' )
);
} );
test( 'should allow string concatenating with + operator', () => {
expect( output ).toEqual(
expect.stringContaining(
'__( "This is a multi-line translation with \\"mixed quotes\\" and mixed \'single quotes\'" ),'
)
);
} );
// we are updating the translate api for plurals to allow for:
// i18n.translate( 'single', 'plural', options );
// we will honor the prior syntax but raise a warning in the console
test( 'should handle the new plural syntax', () => {
expect( output ).toEqual( expect.stringContaining( '_n( "single test", "plural test", 1 ),' ) );
} );
test( 'should allow a variable placeholder for `count`', () => {
expect( output ).toEqual(
expect.stringContaining( '_n( "single test2", "plural test2", 1 ),' )
);
} );
test( 'should include default translations for momentjs object', () => {
expect( output ).toEqual( expect.stringContaining( '__( "number_format_thousands_sep" ),' ) );
expect( output ).toEqual( expect.stringContaining( '__( "number_format_decimal_point" ),' ) );
} );
test( 'should close the php array', () => {
expect( output ).toEqual( expect.stringContaining( '\n);' ) );
} );
test( 'should find translations from multiple files', () => {
expect( output ).toEqual( expect.stringContaining( '__( "My test has two files." ),' ) );
} );
test( 'should find options with a literal string key', () => {
expect( output ).toEqual( expect.stringContaining( 'context with a literal string key' ) );
} );
test( 'should handle template literals', () => {
expect( output ).toEqual( expect.stringContaining( 'My hat has six corners.' ) );
expect( output ).toEqual( expect.stringContaining( '"My hat\nhas seventeen\ncorners."' ) );
} );
} );
describe( 'PHP with an additional textdomain parameter', () => {
describe( 'that has no special symbols', () => {
test( 'should create a simple __() translation', () => {
const output = i18n( {
projectName: 'i18nTest',
inputPaths: sourceFiles,
phpArrayName: 'arrayName',
format: 'PHP',
extras: [ 'date' ],
textdomain: 'some_domain',
} );
expect( output ).toEqual(
expect.stringContaining( '__( "My hat has three corners.", "some_domain" ),' )
);
} );
} );
describe( 'that has double quotes', () => {
test( 'should escape double quotes', () => {
const output = i18n( {
projectName: 'i18nTest',
inputPaths: sourceFiles,
phpArrayName: 'arrayName',
format: 'PHP',
extras: [ 'date' ],
textdomain: '"some"weird-!=domain"',
} );
expect( output ).toEqual( expect.stringContaining( '"\\"some\\"weird-!=domain\\""' ) );
} );
} );
} );
|