Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/infrastructure/markup/__tests__/PhutilTranslatedHTMLTestCase.php
12241 views
1
<?php
2
3
final class PhutilTranslatedHTMLTestCase extends PhutilTestCase {
4
5
public function testHTMLTranslations() {
6
$string = '%s awoke <strong>suddenly</strong> at %s.';
7
$when = '<4 AM>';
8
9
$translator = $this->newTranslator('en_US');
10
11
// When no components are HTML, everything is treated as a string.
12
$who = '<span>Abraham</span>';
13
$translation = $translator->translate(
14
$string,
15
$who,
16
$when);
17
$this->assertEqual(
18
'string',
19
gettype($translation));
20
$this->assertEqual(
21
'<span>Abraham</span> awoke <strong>suddenly</strong> at <4 AM>.',
22
$translation);
23
24
// When at least one component is HTML, everything is treated as HTML.
25
$who = phutil_tag('span', array(), 'Abraham');
26
$translation = $translator->translate(
27
$string,
28
$who,
29
$when);
30
$this->assertTrue($translation instanceof PhutilSafeHTML);
31
$this->assertEqual(
32
'<span>Abraham</span> awoke <strong>suddenly</strong> at &lt;4 AM&gt;.',
33
$translation->getHTMLContent());
34
35
$translation = $translator->translate(
36
$string,
37
$who,
38
new PhutilNumber(1383930802));
39
$this->assertEqual(
40
'<span>Abraham</span> awoke <strong>suddenly</strong> at 1,383,930,802.',
41
$translation->getHTMLContent());
42
43
// In this translation, we have no alternatives for the first conversion.
44
$translator->setTranslations(
45
array(
46
'Run the command %s %d time(s).' => array(
47
array(
48
'Run the command %s once.',
49
'Run the command %s %d times.',
50
),
51
),
52
));
53
54
$this->assertEqual(
55
'Run the command <tt>ls</tt> 123 times.',
56
(string)$translator->translate(
57
'Run the command %s %d time(s).',
58
hsprintf('<tt>%s</tt>', 'ls'),
59
123));
60
}
61
62
private function newTranslator($locale_code) {
63
$locale = PhutilLocale::loadLocale($locale_code);
64
return id(new PhutilTranslator())
65
->setLocale($locale);
66
}
67
68
}
69
70