By default, text runs from left-to-right in web pages, as all Indo-European languages are written in this direction. However Arabic, Hebrew, Urdu and several other languages are written in the opposite direction, i.e. right-to-left. Getting the text itself onto the page is not a particular issue, but forming it to run in the right direction can be.
默认情况下,文本在网页中从左到右运行,因为所有印欧语系都以此方向编写。 但是,阿拉伯语,希伯来语,乌尔都语和其他几种语言是相反的方向,即从右到左。 将文本本身放到页面上并不是一个特殊的问题,但是可以使其形成正确的方向。
第一步:设置文字方向 (Step One: Set The Direction Of The Text)
The base direction of text in a web page is determined by the dir
attribute in the <html>
tag. dir
by default is set to “ltr”
(left to right), and does not need to be explicitly declared. To set the opposite direction, we set dir
to “rtl
” (right to left):
网页中文本的基本方向由<html>
标记中的dir
属性确定。 dir
默认情况下设置为“ltr”
(从左到右),不需要显式声明。 要设置相反的方向,我们将dir
设置为“ rtl
”(从右到左):
<html dir="rtl">
At the same time we should declare the language being used on the page (assuming that the body text is primarily in one language). I will use Hebrew for this example:
同时,我们应该声明页面上使用的语言(假设主体文本主要是一种语言)。 我将在本例中使用希伯来语:
<html lang="he" dir=”rtl” xml:lang="he" xmlns="http://www.w3.org/1999/xhtml">
So long as you are using UTF-8
encoding on your pages, as we discussed in setting up your HTML document, everything else should follow naturally. I would recommend that you follow general recommendations for creating body copy: writing text in a correctly configured word processor or other tool, and copying and pasting the text into appropriate markup in your document.
只要您在页面上使用UTF-8
编码(如我们在设置HTML文档中所讨论的那样),其他所有内容自然都会遵循。 我建议您遵循有关创建正文的一般建议:在正确配置的文字处理器或其他工具中编写文本,以及将文本复制并粘贴到文档中的适当标记中。
<p><span dir="rtl" lang="he">על רקע חילוקי הדעות בין ירושליה ישראלית</span></p>
第二步:处理异常 (Step Two: Deal With Exceptions)
Assuming that the majority of the document was in Hebrew and you wanted to write an English paragraph somewhere in the body, you would declare that paragraph as an exception:
假设文档的大部分内容是希伯来语,并且您想在正文中的某个位置写一个英文段落,则可以将该段落声明为例外:
<p lang="en" dir="ltr">I have reverted to English for a moment</p>
If it was only a word or a short phrase inside a sentence that you wanted to effect, you would use <span>
around the relevant text, with the same values for lang
and dir
using in the <span>
as used for the paragraph above.
如果只是要在句子中使用的单词或简短短语,则可以在相关文本周围使用<span>
,并在<span>
中使用与上段相同的lang
和dir
值。
步骤3:形成标题 (Step 3: Form The Title)
The one remaining issue is that text in the <title>
tag will ignore directionality, and be left-to-right no matter what we have stated. We cannot use the dir
attribute there; instead, we use HTML entities, in the form of Unicode control characters:
剩下的一个问题是<title>
标记中的文本将忽略方向性,并且无论我们说了什么,都是从左到右的。 我们不能在那里使用dir
属性; 而是使用Unicode控制字符形式HTML实体:
<title>‫ שרי השביעייה דנו בתשובת ישראל לארה"ב‬</title>
For more information and examples, I encourage you to read Ahmad Alfy’s excellent piece on converting sites to right-to-left languages.
有关更多信息和示例,我鼓励您阅读Ahmad Alfy关于将网站转换为从右到左语言的出色文章 。
翻译自: https://thenewcode.com/88/Using-Arabic-Hebrew-and-Other-Right-To-Left-Languages-In-Web-Pages