javascript - Magento::翻译 javascript 文件中的文本

标签 javascript php magento translation

Magento 使用一个系统来翻译模板文件中的文本,使用:

$this->__('要翻译的文本。');

Mage::helper('modulename')->__('text to be translated.');.

这很好用。 但是当我将文本添加到 javascript 文件时,我不能使用这两种方法。

有没有一种方法可以对 javascript 文件的翻译做类似的事情?

最佳答案

您可以在模板文件 yourfile.phtml 中执行此操作。 JavaScript js/mage/translate.js 文件必须包含在您的 HTML header 中(Magento 默认情况下这样做)。

<script type="text/javascript">
    Translator.add('You should take care of this confirmation message!','<?php echo Mage::helper('yourmodule')->__('You should take care of this confirmation message!')?>');
</script>

从 Magento 1.7 开始,您可以将文件 jstranslator.xml 添加到 etc/文件夹下的模块中,并像这样设置以下字符串:

<jstranslator>
    <!-- validation.js -->
    <validate-no-html-tags translate="message" module="core">
        <message>HTML tags are not allowed</message>
    </validate-no-html-tags>
    <validate-select translate="message" module="core">
        <message>Please select an option.</message>
    </validate-select>
</jstranslator>

然后借助 CSV 文件,像为 PHP 那样翻译字符串。这会将翻译添加到 JavaScript 代码中,如下面的 var Translator = new Translate(...)

关于javascript - Magento::翻译 javascript 文件中的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7751094/

相关文章:

php shell_exec() - ampps - scrapy 命令的权限被拒绝

php - Facebook 动态产品广告像素(添加到购物车,成功)- Magento

php - 将 google reCaptcha 集成到现有的付款表单中

javascript - CDN 上有哪些著名的 JavaScript 库,URL 是什么?

javascript - 用户选择选项未使用 jQuery 更新

javascript - Flow 不支持某些 JavaScript (ECMAScript) 内置方法,我该怎么办?

php - 注意: undefined offset php

magento - 在 magento2 中以编程方式创建简单的产品

php - Magento 为媒体使用第二个数据库 - SQLSTATE [42S02] : Base table or view not found

javascript - 销毁克隆元素(无法选择第一个实例)