【问题标题】:Is it possible to get the writing mode and writing direction of the user?是否可以获取用户的书写方式和书写方向?
【发布时间】:2019-11-18 10:38:44
【问题描述】:

是否可以获取用户的写作模式?我想知道文本是 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

【问题讨论】:

    标签: javascript html css ime


    【解决方案1】:

    答案:

    使用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.21gigawatts 已修复。对此感到抱歉。
    【解决方案2】:

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

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

    navigator.language
    

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

    【讨论】:

    • 这引出了一个有趣的观点。获得方向和写作模式或语言是否是向使用不同语言的用户表示考虑的方式,而不是为网站编写的?例如,如果我有一个计算抵押贷款成本的页面,但没有本地语言环境中的文本,我应该怎么做?我应该添加 div 说,“我们还没有你的翻译,请联系我们提供它?”。这只是一个例子,但如何表示对其他语言访问者的考虑?
    • 如果 Chrome 和其他浏览器认为他们不知道发生了什么,它们会自动将您的网页内容翻译成用户的浏览器语言。通过始终在 标记的“lang”属性中设置正确的 ISO 语言代码,使这种自动翻译更容易。超出此范围的任何内容都将被视为侵入性。
    • 当您说自动翻译时,您是什么意思?比如从英语到斯瓦希里语或仍然是英语但方向是从右到左还是从下到上?
    • 我指的是谷歌浏览器的自动翻译功能。当您访问外语网站时,浏览器会询问您是否要将内容翻译成您的语言,或者如果从您的浏览历史中明显看出您不知道该语言,它会自动进行翻译。浏览器也会自动处理阅读方向。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多