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

发布于 2024-12-17 20:05:14 字数 40 浏览 1 评论 0原文

我的问题就在标题里。您能帮我实现从右到左文本方向的更好解决方案吗?

My question is in the title. Can you help me to implement better solution for right-to-left text direction.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

素年丶 2024-12-24 20:05:14

例如,您可以制作类似这样的东西

<?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 中获取 en,并且以同样的方式,你可以在控制器中使用适当的 _local 调用 getLanguageRTL 并传递给 twig,以便如果 R2L true 那么 dir=rtl 你可以这样做。

您甚至可以创建一个 twig 扩展来从 twig 调用 getLanguageRTL,但既然控制器是做所有“肮脏工作”的地方,为什么还要麻烦呢。

此致

For example, you can make something like this

<?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];
        }
    }

So, this is one way to go. This way you can have all your language data in one place, you can easily get en in yoururl/en/yourroute by calling getLanguageURL, and in same maner you can call getLanguageRTL in your controller with a proper _local and pass in to twig so that you could do if R2L true then dir=rtl.

You can even make a twig extension to call getLanguageRTL from twig but since controller is the place to do all "dirty works", why bother.

Best regards

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文