forked from kimai/kimai
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TranslationsTest.php
102 lines (87 loc) · 3.52 KB
/
TranslationsTest.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
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Tests;
use PHPUnit\Framework\TestCase;
/**
* @group integration
*/
class TranslationsTest extends TestCase
{
public function testForWrongFileExtension()
{
$files = glob(__DIR__ . '/../translations/*.*');
foreach ($files as $file) {
self::assertStringEndsWith('.xlf', $file);
}
}
public function testForEmptyStrings()
{
$files = glob(__DIR__ . '/../translations/*.xlf');
foreach ($files as $file) {
$xml = simplexml_load_file($file);
/** @var \SimpleXMLElement $body */
$body = $xml->file->body;
foreach ($body->children() as $transUnit) {
self::assertNotEmpty(
(string) $transUnit->target,
sprintf(
'Found empty translation in language "%s" and file "%s" for key "%s"',
$xml->file->attributes()['target-language'],
basename($file),
(string) $transUnit->source
)
);
}
}
}
public function testReplacerWereNotTranslated()
{
$englishFiles = glob(__DIR__ . '/../translations/*.en.xlf');
foreach ($englishFiles as $englishFile) {
$english = simplexml_load_file($englishFile);
$trans = [];
/** @var \SimpleXMLElement $body */
$body = $english->file->body;
foreach ($body->children() as $transUnit) {
preg_match_all('/%[a-zA-Z]{1,}%/Uu', (string) $transUnit->target, $matches);
if (!empty($matches) && !empty($matches[0])) {
asort($matches[0]);
$trans[(string) $transUnit->source] = array_values($matches[0]);
}
}
if (empty($trans)) {
continue;
}
$expectedCounter = \count($trans);
$files = glob(__DIR__ . '/../translations/' . str_replace('.en.xlf', '', basename($englishFile)) . '*.xlf');
foreach ($files as $file) {
if ($englishFile === $file) {
continue;
}
$counter = 0;
$xml = simplexml_load_file($file);
$transLang = $trans;
/** @var \SimpleXMLElement $body */
$body = $xml->file->body;
foreach ($body->children() as $transUnit) {
$key = (string) $transUnit->source;
if (isset($transLang[$key])) {
// some special cases, which don't work properly - base translation should be changed
preg_match_all('/%[a-zA-Z]{1,}%/Uu', (string) $transUnit->target, $matches);
asort($matches[0]);
self::assertEquals($transLang[$key], array_values($matches[0]), sprintf('Invalid replacer "%s" in "%s"', $key, basename($file)));
$counter++;
unset($transLang[$key]);
}
}
$counter += \count($transLang);
self::assertEquals($expectedCounter, $counter, sprintf('Missing replacer in "%s", did not find translation keys: %s', basename($file), implode(', ', array_keys($transLang))));
}
}
}
}