Magento :: 从 javascript 文件翻译文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7751094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:26:59  来源:igfitidea点击:

Magento :: Translate text from javascript files

javascriptphpmagentotranslationmagento-1.x

提问by Chris

Magento uses a system for translating text in the template files using:

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

$this->__('text to be translated.');

$this->__('text to be translated.');

or

或者

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

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

This works quite well. But when I add text to a javascript file I can't use these two methods.

这很有效。但是当我向 javascript 文件添加文本时,我不能使用这两种方法。

Is there a way I could do a similar thing with the translations for javascript files?

有没有办法对 javascript 文件的翻译做类似的事情?

回答by Sylvain Rayé

You can do it in a template file yourfile.phtml. The javascript script js/mage/translate.js must be included in your html header (Magento does it by default).

您可以在模板文件 yourfile.phtml 中执行此操作。javascript 脚本 js/mage/translate.js 必须包含在您的 html 标头中(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>

EDIT: You can since Magento 1.7 add a file jstranslator.xml into your module under the etc/ folder and set the following string like that:

编辑:从 Magento 1.7 开始,您可以在 etc/ 文件夹下的模块中添加一个文件 jstranslator.xml 并设置以下字符串:

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

Then translate the string as you do it for PHP thanks to CSV file This, will add the translation to the javascript code like the following var Translator = new Translate(...)

多亏了 CSV 文件,然后像 PHP 那样翻译字符串 这会将翻译添加到 javascript 代码中,如下所示 var Translator = new Translate(...)

回答by Roman Snitko

Just use the following method in your scripts:

只需在脚本中使用以下方法:

Translator.translate('Some phrase');

回答by Malith

This is the correct way for translating JavaScript strings withing .phtml file

这是使用 .phtml 文件翻译 JavaScript 字符串的正确方法

Translator.add({"To be translated":"<?php echo $this->_('To be translated'); ?>"});

Update: fixed typo.

更新:修正了错字。

回答by Анастасия Ермолик

Use this is in jsfile:

js文件中使用:

Translator.translate('Some phrase');

But to make it work you should define this translation in phtml:

但是为了让它工作,你应该在phtml 中定义这个翻译:

Translator.add('Some phrase', "<?php echo $this->__('Some phrase'); ?>");