@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final 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 <4 AM>.',
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 <kbd>ls</kbd> 123 times.',
56 (string)$translator->translate(
57 'Run the command %s %d time(s).',
58 hsprintf('<kbd>%s</kbd>', '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}