我希望列表以字母(而不是数字)列出。
我尝试使用type =“ a”,它将所有内容显示为a。
<?php
namespace Cocorico\TranslationBundle\Model\Manager;
use Cocorico\TranslationBundle\Model\Manager\Exception\TranslationKeyIsInvalid;
use Cocorico\TranslationBundle\Model\Manager\Exception\TranslationQuotaExceeded;
class TranslatorManager
{
protected $clientSecret;
protected $tokenUrl;
protected $translateUrl;
/**
* __construct
*
* @param string $clientSecret
* @param string $tokenUrl
* @param string $translateUrl
*/
public function __construct(
$clientSecret,
$tokenUrl,
$translateUrl
) {
$this->clientSecret = $clientSecret;
$this->tokenUrl = $tokenUrl;
$this->translateUrl = $translateUrl;
}
/**
* getTranslation returns translated string from the server replacing tags
*
* @param string $fromLanguage
* @param string $toLanguage
* @param array $text
* @return array
*/
public function getTranslation($fromLanguage, $toLanguage, $text = array())
{
$responseArray = array();
if (!$this->clientSecret) {
throw new TranslationKeyIsInvalid('missing key');
}
$xml = <<<XML
<TranslateArrayRequest>
<AppId/>
<From>{$fromLanguage}</From>
<Options>
<Category xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<ContentType xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2">text/plain</ContentType>
<ReservedFlags xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<State xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<Uri xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
<User xmlns="http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2" />
</Options>
<Texts>
XML;
foreach ($text as $inputStr) {
$inputStr = str_ireplace('<![CDATA', '', $inputStr);
$xml .= <<<XML
<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"><![CDATA[{$inputStr}]]></string>
XML;
}
$xml .= <<<XML
</Texts>
<To>{$toLanguage}</To>
</TranslateArrayRequest>
XML;
$response = $this->getTranslationResponse($xml);
$xmlObj = new \SimpleXMLElement($response);
if (!isset($xmlObj->TranslateArrayResponse)) {
throw new \LogicException('Response from translator is incomplete');
}
foreach ($xmlObj->TranslateArrayResponse as $translatedArrObj) {
$responseArray[] = (string)$translatedArrObj->TranslatedText;
}
if (!count($responseArray)) {
throw new \LogicException('Empty response');
}
return $responseArray;
}
/**
* @param $requestXml
* @return mixed
* @throws \Exception
*/
protected function getTranslationResponse($requestXml)
{
$curlHandler = curl_init();
curl_setopt($curlHandler, CURLOPT_URL, $this->translateUrl);
curl_setopt(
$curlHandler,
CURLOPT_HTTPHEADER,
array('Authorization: Bearer ' . $this->getAccessToken(), 'Content-Type: text/xml')
);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false);
if ($requestXml) {
//Set HTTP POST Request.
curl_setopt($curlHandler, CURLOPT_POST, true);
//Set data to POST in HTTP "POST" Operation.
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $requestXml);
}
$response = curl_exec($curlHandler);
$curlErrNo = curl_errno($curlHandler);
if ($curlErrNo) {
$curlError = curl_error($curlHandler);
throw new \Exception($curlError);
}
curl_close($curlHandler);
if (preg_match('/missing subscription key/i', $response)) {
throw new TranslationKeyIsInvalid('missing key');
}
return $response;
}
protected function getAccessToken()
{
$curlHandler = curl_init();
$dataString = json_encode('{body}');
curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $dataString);
curl_setopt(
$curlHandler,
CURLOPT_HTTPHEADER,
array(
'Content-Type: application/json',
'Content-Length: ' . strlen($dataString),
'Ocp-Apim-Subscription-Key: ' . $this->clientSecret
)
);
curl_setopt($curlHandler, CURLOPT_URL, $this->tokenUrl);
curl_setopt($curlHandler, CURLOPT_HEADER, false);
curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, true);
$strResponse = curl_exec($curlHandler);
curl_close($curlHandler);
if (preg_match('/Out of call volume quota/i', $strResponse)) {
throw new TranslationQuotaExceeded('quota exceeded for translation');
}
if (preg_match('/invalid subscription key/i', $strResponse)) {
throw new TranslationKeyIsInvalid('your key is invalid');
}
return $strResponse;
}
答案 0 :(得分:0)
因为每个<li>
都包裹在自己的<ol>
中,这意味着每个A
都会开始
相反,请使用一个<ol>
,并在其中添加<li>
...
<ol type="A">
<script>
salad = new Array();
salad[0]="feta";
salad[1]="tomatoes";
salad[2]="lettuce";
salad[3]="olives";
salad[4]="olive oil";
salad[5]="vinegar";
salad[6]="herbs";
salad[7]="seasoning";
for (i=0;i<salad.length;i=i+1) {
document.writeln("<li>" + salad[i] + "</li>");
}
</script>
</ol>
答案 1 :(得分:0)
您必须将ol
元素移出循环,作为列表脚本的包装。否则,循环每次都会创建一个新的ol
,始终以“ A”开头:
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<ol type='A'>
<script>
salad = new Array();
salad[0] = "feta";
salad[1] = "tomatoes";
salad[2] = "lettuce";
salad[3] = "olives";
salad[4] = "olive oil";
salad[5] = "vinegar";
salad[6] = "herbs";
salad[7] = "seasoning";
for (i = 0; i < salad.length; i = i + 1) {
document.writeln("<li>" + salad[i] + "</li>");
}
</script>
</ol>
</body>
</html>