问候取决于一天中的时间

时间:2018-02-27 13:19:34

标签: twig

我想在Twig中根据一天中的时间创建一个问候语。

像(仅作为一个例子):

{% 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);

任何帮助都将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

如果您使用的是译者组件(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 %}

此解决方案允许您处理我们不能说早上好的语言,例如法语:

void movement () {
        moveVector = Vector3.zero;
        moveVector.x = Input.GetAxisRaw("Horizontal");
        moveVector.z = Input.GetAxisRaw("Vertical");

        if (controller.isGrounded) {
            verticalVelocity = -1;

            if (Input.GetButtonDown("Jump")) {
                verticalVelocity = jumpforce;
            }

        } else {
            verticalVelocity -= gravity * Time.deltaTime;
            moveVector = lastMove;
        }

        moveVector.y = 0;
        moveVector.Normalize ();
        moveVector *= playerspeed;
        moveVector.y = verticalVelocity;

        worldMove = transform.TransformDirection (moveVector);
        controller.Move (worldMove * Time.deltaTime);

        //controller.Move (moveVector.z * transform.forward * Time.deltaTime);
        //controller.Move (moveVector.x * transform.right * Time.deltaTime);
        //controller.Move (moveVector.y * transform.up * Time.deltaTime);

        //controller.Move (moveVector * Time.deltaTime);
        lastMove = moveVector;
    }