是否有采用“_('string')”格式的 underscorejs 对象构造函数?

Is there underscorejs object constructor that takes " _('string') " format?

我正在研究 html 模板并对其进行剖析(更具体地说是 Suitecommerce Reference Impl - 一种带有 Webfront 功能的 ERP 解决方案)。

这是模板中的一个片段。

    <li>
        <a href="#" data-touchpoint="customercenter" data-hashtag="#ordershistory">
                        <%= _('Order History').translate() %>
        </a>
    </li>

正常html标签之间的%标签是用backbonejs和underscorejs写的。

由于 underscorejs 函数采用 _.function() 形式,我没有得到 _('string').

的目的

有 backbonejs/underscorejs 开发人员吗?

它看起来像一个翻译库,旨在将文本翻译成用户浏览器中指定的语言,可能类似于 this one

translate.js 的文档页面上,您似乎可以使用传统的 underscore.js 样式调用该函数,或者您可以在下划线函数中发送主要参数:

_.translate(text...) - this is the main translate function and is also the base object (i.e. you can call _(text)). Translates the first parameter. If called with multiple parameters, it will call a "format" function with the 1st parameter translated followed by the additional parameters.

使用下划线函数访问翻译库是很常见的约定(例如,see Django):translated_text = _('Text to translate')

我建议查看脚本以查看是否正在使用语言翻译库。

当您调用 _(obj) 时,下划线会包裹 obj 参数。然后可以在包装对象上调用任何 Underscore 方法,而无需修改 obj.

的原型

您正在使用的库之一似乎已将 translate() 方法添加到 Underscore 原型,而 _(string).translate() 是在您的字符串上调用该方法的方法。

这是另一个以类似方式扩展 Underscore 的例子:

_.mixin({
  logToConsole: function(str) {
    console.log(str)
  }
})

_('text to log').logToConsole()

参考文献:

Underscore OOP (wrapping)

Underscore mixin method