是否可以获取用户的书写方式和书写方向?

Is it possible to get the writing mode and writing direction of the user?

是否可以获取用户的书写模式?我想知道文本是 RTL 还是 LTR 和 TB。

看来我可以从computedValue中得到方向和书写方式

#div2 {
  writing-mode: vertical-lr;
}

#div1 {
  direction: rtl;
}

p {
  background-color: #FFEECC;
}
<p>While the characters in most scripts are written from left to right, certain scripts are written from right to left.</p>

<p id="div1">While the characters in most scripts are written from left to right, certain scripts are written from right to left. In some documents, in particular those written with the Arabic or Hebrew script, and in some mixed-language contexts, text in a single (visually displayed) block may appear with mixed directionality. This phenomenon is called bidirectionality, or "bidi" for short.</p>

<p id="div2">While the characters in most scripts are written from left to right, certain scripts are written from right to left. I</p>

或者是否有推荐的获取此信息的方式,例如 IME 模式?

获得方向和写作模式或语言是对其他语言的体谅吗?

https://www.w3.org/TR/css-writing-modes-3/

https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Relationship_of_Flexbox_to_Other_Layout_Methods#Writing_Modes

答案:

使用getComputedStyleWindow方法。

 getComputedStyle(document.body).direction;
 getComputedStyle(document.body)["writing-mode"];

为什么?

不同于简单地访问节点的 style 对象,默认情况下可能 return 一个空字符串(a.e。direction 默认为 "ltr"但 return ""), getComputedStyle 将 return 每次请求的样式值,包括默认值。


示例:

let direction = getComputedStyle(document.body).direction;
let writing_mode = getComputedStyle(document.body)["writing-mode"];
console.log(direction, writing_mode);


旁白:

也可以和Destructuring Assignment结合使用:

let {direction, "writing-mode": writing_mode} = getComputedStyle(document.body);

let {direction, "writing-mode": writing_mode} = getComputedStyle(document.body);
console.log(direction, writing_mode);

HTML文档有一个"direction"属性指定其内容的语言方向,但这并不代表用户的阅读方向(即在美国的美国用户可以访问日本网站,内容阅读方向为RTL)。

相反,您可以使用 Javascript 获取用户的浏览器语言以检查全局 属性:

navigator.language

这将为您提供一个 ISO 语言(即 "en-US")代码,您可以将其与一组用于 RTL 语言(很容易从 google 收集)的 ISO 代码进行比较,以及BTT 语言的 ISO 代码列表。