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

时间:2019-07-08 21:51:11

标签: javascript html css ime

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

看来我可以从calculatedValue获取方向和书写方式。

#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

2 个答案:

答案 0 :(得分:2)

答案:

使用getComputedStyle Window方法。

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

为什么?

不同于简单地访问节点的style对象,该对象可能默认情况下返回空字符串(ae direction默认为“ ltr”,但将返回“”), getComputedStyle 将返回每次请求的样式值,包括默认值。


示例:

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);

答案 1 :(得分:1)

HTML文档具有“方向”属性,该属性指定其内容的语言方向,但这并不代表用户的阅读方向(即,美国的美国用户可以访问日语网站以及内容的阅读方向是RTL)。

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

navigator.language

这将为您提供一个ISO语言(即“ en-US”)代码,您可以将其与RTL语言的ISO代码数组(可以从Google轻松收集)进行比较,以及BTT的ISO代码列表语言。