Path: blob/master/src/infrastructure/markup/__tests__/PhutilTranslatedHTMLTestCase.php
12241 views
<?php12final class PhutilTranslatedHTMLTestCase extends PhutilTestCase {34public function testHTMLTranslations() {5$string = '%s awoke <strong>suddenly</strong> at %s.';6$when = '<4 AM>';78$translator = $this->newTranslator('en_US');910// When no components are HTML, everything is treated as a string.11$who = '<span>Abraham</span>';12$translation = $translator->translate(13$string,14$who,15$when);16$this->assertEqual(17'string',18gettype($translation));19$this->assertEqual(20'<span>Abraham</span> awoke <strong>suddenly</strong> at <4 AM>.',21$translation);2223// When at least one component is HTML, everything is treated as HTML.24$who = phutil_tag('span', array(), 'Abraham');25$translation = $translator->translate(26$string,27$who,28$when);29$this->assertTrue($translation instanceof PhutilSafeHTML);30$this->assertEqual(31'<span>Abraham</span> awoke <strong>suddenly</strong> at <4 AM>.',32$translation->getHTMLContent());3334$translation = $translator->translate(35$string,36$who,37new PhutilNumber(1383930802));38$this->assertEqual(39'<span>Abraham</span> awoke <strong>suddenly</strong> at 1,383,930,802.',40$translation->getHTMLContent());4142// In this translation, we have no alternatives for the first conversion.43$translator->setTranslations(44array(45'Run the command %s %d time(s).' => array(46array(47'Run the command %s once.',48'Run the command %s %d times.',49),50),51));5253$this->assertEqual(54'Run the command <tt>ls</tt> 123 times.',55(string)$translator->translate(56'Run the command %s %d time(s).',57hsprintf('<tt>%s</tt>', 'ls'),58123));59}6061private function newTranslator($locale_code) {62$locale = PhutilLocale::loadLocale($locale_code);63return id(new PhutilTranslator())64->setLocale($locale);65}6667}686970