Silex : allow user to change langage by clicking on html element and keeping clean URL

前端 未结 4 907
一生所求
一生所求 2021-01-19 07:18

I am using Silex and Twig for a website and I want to allow the user to change the langage of the site.

My problem

Right now, it works if I change the loca

4条回答
  •  一个人的身影
    2021-01-19 07:59

    In you case its just the matter of adding proper links to allow users so they can switch language. Rest of the work is already done by you. The language switcher dropdown must contain all supported languages including English. See below am example image.

    Instead of redirecting the users to http://yourwebsite.com/[fr]/my-account you must redirect to a special method like http://www.yourwebsite.com/language_change?lang=fr. You will get HTTP_REFERRER in $_SERVER variable so you can redirect users back to the original page they were at. This way you can preserve users current visiting page/url.

    Secondly, Ajax vs Redirect: Its better to use a redirect instead of AJAX. If you use AJAX you will increase the complicity to yourself.

    When working with multilingual website, we must follow these best practices.

    • Specific routes per language

    • Menus, labels and forms

    • The content of the website

    • Extra: translate FOSUserBundle

提交回复
热议问题