javascript 中从右到左区域设置的连接字符串

发布于 2024-11-29 08:34:20 字数 64 浏览 2 评论 0原文

看来 + 不是处理 JavaScript 中字符串连接的正确运算符。处理 LTR 和 RTL 案件有哪些替代方案?

It seems + is not the right operator to handle the concatenation of strings in JavaScript. what are some alternatives to handle the both the ltr and rtl cases?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花坊 2024-12-06 08:34:20

问题是, + 根本不是连接字符串的正确运算符。或者也许是这样,但连接字符串是一个国际化错误。

实际上应该对它们进行格式化,而不是简单地将它们连接起来。因此,您实际上应该做的是使用占位符:

var somePattern = "This language is written {0}.";
var someMessage = somePattern.format("LTR");

这样,翻译人员就可以重新排序句子,包括词序。我相信它可以解决您的问题。

对于格式化功能,让我引用这个优秀的答案:

String.prototype.format = function() {
    var args = arguments;

    return this.replace(/\{(\d+)\}/g, function() {
        return args[arguments[1]];
    });
};

编辑:添加有关方向标记的信息。

有时,当您有多个占位符时,您可能会失去对字符串方向的控制,即 {0}/{1} 仍会显示为第一个/第二个,而不是所需的第二个/最后一个。要解决此问题,您可以向模式添加强方向性标记,即 {0}‏/{1} 是一个 HTML 实体,解析为 Unicode 代码点 U+200F,即从右到左的强方向性标记。

The problem is, + is not right operator to concatenate strings at all. Or maybe it is, but concatenating string is an Internationalization bug.

Instead of simply concatenating them, one should actually format them. So what you should actually do, is use placeholders:

var somePattern = "This language is written {0}.";
var someMessage = somePattern.format("LTR");

This way, the translator would be able to re-order the sentence, including word order. And I believe it solves your problem.

For formatting function, let me quote this excellent answer:

String.prototype.format = function() {
    var args = arguments;

    return this.replace(/\{(\d+)\}/g, function() {
        return args[arguments[1]];
    });
};

EDIT: Adding information about directionality marks.

Sometimes, when you have multiple placeholders you may lose the control of string direction, i.e. {0}/{1} would still be shown as first/second instead of desired second/last. To fix this, you would add Strong Directionality Mark to the pattern, i.e. {0}‏/{1}. is an HTML entity that resolves to Unicode code point U+200F, that is right-to-left strong directionality mark.

与他有关 2024-12-06 08:34:20

实际上,假设两个字符串都已本地化,并且您希望右侧的字符串在逻辑上显示在左侧的字符串之后,则+有时工作正常。阿拉伯语等语言的字符串应在屏幕上显示为 RTL(从右到左),但在内存中字符顺序仍应为 LTR(从左到右)。因此 + 运算符在逻辑上与用于生成任何语言的术语“有序列表”是一致的。

但也有一些场景+并不能正确解决问题。在某些情况下,正确的解决方案是遵循包含语言的语法。例如,您真的将英语单词嵌入到阿拉伯语句子中吗?或者反之亦然?无论如何,这里的解决方案是进行字符串格式化,其中本地化的包含句子有一个外来术语的占位符,例如 {0}

第三种情况是如果因为只是两个独立的句子而没有语法关系怎么办?在这种情况下,没有正确的排序。例如,如果您有一个英语句子显示在一个阿拉伯语句子前面。讲英语的人可能会读 LTR 句子(先左句,然后右句)。讲阿拉伯语的人可能会读 RTL 句子。无论哪种方式,每个人都不清楚作者打算阅读句子的顺序。:)

Actually, assuming both string are localized and you want the string on the right to be displayed logically after the string on the left, then + sometimes works fine. Strings in languages such as Arabic should be displayed RTL (right to left) on the screen, but the character ordering is still meant to be LTR (left to right) in memory. So + operator is logically consistent to use for generating an 'ordered list' of terms in any language.

But there are also scenarios where + does not solve the problem correctly. There are scenarios where the correct solution is to follow the grammar of the containing language. For instance, are you really embedding an English word in an Arabic sentence? Or vice versa? Regardless, the solution here is to do string formatting, where the containing sentence localized has a placeholder for the foreign term, like {0}.

The third case is what if there is no grammatical relationship because it is just two separate sentences? In this case there is no correct ordering. E.g. if you have an English sentence displayed in front of an Arabic sentence. An English speaker will probably read the sentences LTR (left sentence first, then right). An Arabic speaker will probably read the sentences RTL. Either way it's unclear to everyone which order the author intended the sentences to be read in. :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文