显示阿拉伯文本

5

我希望能用阿拉伯语来显示25%*,效果应该是这样的:*%25

我尝试使用direction:rtf来显示它,但是只有在在其后面添加一些阿拉伯文本时才能正确显示。运行下面的代码片段以查看所指的情况:

var arabicword = "إلى";
var percent = "25%*";

document.getElementById('text2').innerHTML = arabicword+" "+percent;
document.getElementById('text1').innerHTML = percent;
.text {
  font-size:3em;
  width:300px;
  color:white;
  background: grey;
}

.arabic{
  background:green;
  direction:rtl;
}
<div id="text1" class="text arabic">0</div>
<br>
<div id="text2" class="text arabic">0</div>

Is there a reliable way of achieving this? I tried to add an arabic whitespace character, but there doesn't seem to be one

Thanks in advance

5个回答

3

从左到右标记 &lrm; 可以控制这个:

var arabicword = "إلى";
var percent = "25&lrm;%*"

document.getElementById('text2').innerHTML = arabicword+" "+percent;
document.getElementById('text1').innerHTML = percent;
.text {
  font-size:3em;
  width:300px;
  color:white;
  background: grey;
}

.arabic{
  background:green;
  direction:rtl;
}
<div id="text1" class="text arabic">0</div>
<br>
<div id="text2" class="text arabic">0</div>


1
当显示以非阿拉伯字符结尾的阿拉伯文本时,最好的解决方法是在非阿拉伯字符后包含Unicode RTL代码。
当在不同方向文本运行之间放置中性字符(例如标点符号)时,会发生这种情况。例如,阿拉伯Unicode表没有相应于字符!@#$%*&amp;的代码。
当您将任何这些拉丁字符添加到阿拉伯文本的末尾时,该字符会正确地定位在阿拉伯文本的右侧(即从右向左读取阿拉伯文本之前)。根据阿拉伯文本的方向,这是不正确的。
通过在中性拉丁字符后添加RTL Unicode“\u200F”,您指示拉丁字符保持RTL作为剩余文本。这将修复问题。无论句子的原始方向是RTL还是LTR,此方法都有效,因为阿拉伯字符始终采用RTL方向。
这也允许在同一句子中组合RTL和LTR文本。
以下是两个带有和不带有RTL Unicode的示例:

let Arabic = "هلا";            // Arabic text will always display in the right direction
let Output1 = Arabic + " %";  // add Neutral Latin Char to end 
let Output2 = Arabic + " %&"; // add Neutral Latin Char to end
let Output3 = Arabic + " *";  // add Neutral Latin Char to end

console.log("Output without the RTL Unicode:")
console.log(Output1);    // "هلا %"  This is incorrect output
console.log(Output2);    // "هلا %&" This is incorrect output
console.log(Output3);    // "هلا *"  This is incorrect output

//---------------------
let RTL = "\u200F";                   // The RTL Unicode
let Output_1 = Arabic + " %" + RTL;   // add Neutral Latin Char to end + RTL
let Output_2 = Arabic + " %&" + RTL;  // add Neutral Latin Char to end + RTL
let Output_3 = Arabic + " *" + RTL;   // add Neutral Latin Char to end + RTL

console.log("Output with the RTL Unicode:")
console.log(Output_1);
console.log(Output_2);
console.log(Output_3);

你可以在这里阅读有关RTL Unicode的更多信息:https://www.w3.org/TR/WCAG20-TECHS/H34.html

1

我通过在数字和百分号之间添加一个空格来实现这一点。不确定这是否是最好的方法,但它是可靠的。

var arabicword = "إلى";
var percent = "25 %*";

document.getElementById('text2').innerHTML = arabicword+" "+percent;
document.getElementById('text1').innerHTML = percent;
.text {
  font-size:3em;
  width:300px;
  color:white;
  background: grey;
}

.arabic{
  background:green;
  direction:rtl;
}
<div id="text1" class="text arabic">0</div>
<br>
<div id="text2" class="text arabic">0</div>


1
感谢您的建议。实际上,我找到了一个空的阿拉伯字符&#1564,在前面添加它可以解决这个问题。

var arblank = "&#1564;"
var arabicword = "إلى";
var percent = "25%*";

document.getElementById('text2').innerHTML = arabicword+" "+percent;
document.getElementById('text1').innerHTML = arblank+percent;
.text {
  font-size:3em;
  width:300px;
  color:white;
  background: grey;
}

.arabic{
  background:green;
  direction:rtl;
}
<div id="text1" class="text arabic">0</div>
<br>
<div id="text2" class="text arabic">0</div>


0

当阿拉伯文本中出现带有百分号的数字时,正确的写法是像这样写:

النسبة المئوية هي %25

这意味着%符号在数字右侧,就像英文文本中一样。

因此,无需将%符号的位置切换到数字左侧。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接