Search code examples
phpangularjssymfonyangular-translate

Setting cookie for AngularJS translate locale using PHP


My app uses Symfony and AngularJS. I have translations some with Symfony itself and some with AngularJS angular-translate. How do I set a cookie or session variable to change language for AngularJS from PHP?


Solution

  • Set the cookie via PHP:

    <?php setcookie("_locale", "en"); ?>
    

    and retrieve it from angular with ngCookies:

    angular.module('app', ['ngCookies'])
    .controller('ExampleController', ['$cookies', function($cookies) {
         // Retrieving the cookie
         var locale = $cookies.get('_locale');
         // Do something with locale
    }]);