如何使用Symfony2轻松实现从右到左的文本方向

时间:2011-11-24 13:06:29

标签: symfony twig right-to-left

我的问题在标题中。你能帮我实现从右到左文本方向的更好的解决方案。

1 个答案:

答案 0 :(得分:0)

例如,您可以制作类似这样的内容

<?php

namespace You\YourBundle\Constants;

    class LanguageConstants
    {
        const LANGUAGE_EN = 1;
        const LANGUAGE_HR = 2;
        const LANGUAGE_SR = 3;
        const LANGUAGE_BS = 4;
        const LANGUAGE_DE = 5;
        const LANGUAGE_PT = 6;
        const LANGUAGE_AR = 7;

        static private $constants = array(
            self::LANGUAGE_EN => 'English',
            self::LANGUAGE_HR => 'Hrvatski',
            self::LANGUAGE_SR => 'Srpski / Српски',
            self::LANGUAGE_BS => 'Bosanski / Босански',
            self::LANGUAGE_DE => 'Deutsch',
            self::LANGUAGE_PT => 'Português',
            self::LANGUAGE_AR => 'العربية',
        );

        static private $constantsURL = array(
            self::LANGUAGE_EN => 'en',
            self::LANGUAGE_HR => 'hr',
            self::LANGUAGE_SR => 'sr',
            self::LANGUAGE_BS => 'bs',
            self::LANGUAGE_DE => 'de',
            self::LANGUAGE_PT => 'pt',
            self::LANGUAGE_AR => 'ar',
        );

        static private $constantsRTL = array(
            self::LANGUAGE_EN => false,
            self::LANGUAGE_HR => false,
            self::LANGUAGE_SR => false,
            self::LANGUAGE_BS => false,
            self::LANGUAGE_DE => false,
            self::LANGUAGE_PT => false,
            self::LANGUAGE_AR => true,
        );

        static public function getLanguageConstants()
        {
            natsort(self::$constants);
            return self::$constants;
        }

        static public function getLanguageName($const)
        {
            return self::$constants[$const];
        }

        static public function getLanguageURL($const)
        {
            return self::$constantsURL[$const];
        }

        static public function getLanguageRTL($const)
        {
            return self::$constantsRTL[$const];
        }
    }

所以,这是一种方法。通过这种方式,您可以将所有语言数据放在一个位置,您可以通过调用getLanguageURL轻松获取yoururl / en / yourroute,并且可以使用适当的_local在控制器中调用getLanguageRTL并传入twig,以便如果R2L为true,则可以执行,然后dir = rtl。

你甚至可以做一个枝条扩展来从twig调用getLanguageRTL,但由于控制器是做所有“脏工作”的地方,为什么要这么麻烦。

祝你好运