Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
494 views
in Technique[技术] by (71.8m points)

php - Symfony2: How can I set twig |date("d F, Y") filter to output months in Swedish?

I'm having problems with the |date("d F, Y") filter in my twig templates.

I want the months to be outputted in Swedish. I have tried setting "locale: sv" in my parameters.yml files but I get no effect.

It was working before I upgraded to from Symfony 2.1 to 2.3 so I think that might have something to do with it.

Any thoughts on how to fix this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The Twig Intl Extension

You can use the Twig Intl Extension found in fabpot's official Twig extension repository.

It provides a localized date filter which can be used like this:

{{ date | localizeddate('full', 'none', app.request.locale ) }}

use app.request.locale as third parameter for current locale or just 'sv'.

Integration into your project

add the official extensions to your composer.json using:

composer require twig/extensions:1.0.*@dev
composer update twig/extensions

config.yml

#enable intl extensions
services:
    twig.extension.intl:
        class: Twig_Extensions_Extension_Intl
        tags:
            - { name: twig.extension }

quick tip:

another handy extension is the Text extension providing truncate,...etc filters

services:
    twig.extension.text:
        class: Twig_Extensions_Extension_Text
        tags:
            - { name: twig.extension }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...