我想根据一天中的时间来创建问候。
类似(纯粹作为示例(:
{% if hours < 12 %} {{translate('Morning')}} {% elseif hours < 18 %} {{translate('Evening')}} {% else %} {{translate('Night')}} {% endif %}
,我所拥有的,我正在这里联系以检查是否有人可以帮助我。
这就是JavaScript中的外观,但我想使用twig:
var thehours = new Date().getHours();
var themessage;
var morning = ('Good morning');
var afternoon = ('Good afternoon');
var evening = ('Good evening');
if (thehours >= 0 && thehours < 12) {
themessage = morning;
} else if (thehours >= 12 && thehours < 17) {
themessage = afternoon;
} else if (thehours >= 17 && thehours < 24) {
themessage = evening;
}
$('.greeting').append(themessage);
任何帮助都将不胜感激。
如果您使用的是Translator组件(composer require symfony/translation
(。
您可以使用此处定义的transchoice
{% currentHour = 'now'|date('H') %}
{% transchoice currentHour %}
[0,12[ Good morning |]12,17[ Good afternoon |]17,24[ Good evening
{% endtranschoice %}
此解决方案允许您处理我们不说早上好的语言,例如法语:
{% transchoice currentHour %}
[0,17[ Bonjour |]17,24[ Bonsoir
{% endtranschoice %}