Drupal 上下文多语言翻译
PHP in Drupal 7 and 8:
$short_may = t('May'); $long_may = t('May', array(), array('context' => 'Long month name'));
JavaScript in Drupal 7 versions < 7.10:
shortMay = Drupal.t('May'); longMay = .... not possible in JS ....
JavaScript in Drupal 7 versions > 7.9 and Drupal 8:
shortMay = Drupal.t('May'); longMay = Drupal.t('May', {}, {context: "Long month name"});
Translation in Twig templates
使用
t
(或trans
)过滤器可以进行简单的文本翻译,而对于更复杂的场景,例如具有占位符和奇异值的字符串,支持整个{%trans%}
块(由Twig i18n扩展定义) /复数组合。
文本
{{ 'Hello Earth.'|trans }} {{ 'Hello Earth.'|t }}
单数/复数
{% set count = 1 %} {% trans %} Hello star. {% plural count %} Hello {{ count }} stars. {% endtrans %}
转义字符
{% set string = '&"<>' %} {% trans %} Escaped: {{ string }} {% endtrans %} {% trans %} Placeholder: {{ string|placeholder }} {% endtrans %}
注释中的翻译
Drupal7
<?php function file_entity_info() { return array( 'file' => array( 'label' => t('File'), // ... ), ); } ?>
Drupal8
<?php /** * Defines the file entity class. * * @ContentEntityType( * id = "file", * label = @Translation("File"), * .... */ class File { } ?>