0

I would like to force locale for my Symfony 2.5 application to ru_RU.UTF-8. I want this locale to be used for strftime() function.

I have the following configuration in my app/config.yml:

framework:
    default_locale: "ru_RU.UTF-8"

I'm using the following code in one of my controllers to debug this:

//setlocale(LC_TIME, 'ru_RU.UTF-8');
var_dump(setlocale(LC_TIME, 0));
var_dump(strftime('%B', time()));
die();

When executed like this, it shows: string(1) "C" string(6) "August".

However, when first line is uncommented it shows: string(11) "ru_RU.UTF-8" string(12) "Август", so the locale is installed in the system and works correctly.

Ho do I make Symfony to always use locale specified in the config?


$ locale -a:

C
C.UTF-8
en_US.utf8
POSIX
ru_RU
ru_RU.iso88595
ru_RU.utf8
Slava Fomin II
  • 26,865
  • 29
  • 124
  • 202
  • 1
    If I correctly understand, the Symfony uses a locale for the translations (http://symfony.com/doc/current/book/translation.html). In other words, it doesn't use function `setlocale()` – Serge Kvashnin Aug 13 '14 at 20:04

2 Answers2

1

The locale setting for Symfony doesn't influence the PHP settings, it's only a Symfony setting.

You have to use setlocale() to configure PHP's locale. You can place this in the front controllers (web/app.php and web/app_dev.php) or create a listener for kernel.request.

Wouter J
  • 41,455
  • 15
  • 107
  • 112
  • Thanks, I've added `setlocale` to my front controllers as a workaround. I guess I will keep it that way for now. But, is there a way to configure PHP (FPM) to use some specific locale though? – Slava Fomin II Aug 13 '14 at 23:01
  • @SlavaFominII you need to set the locale in the actually enviorment you are running in. In your shell (I'm assuming you're running unix/linux) do `locale -a` if the value doesn't match what you want then find your locale config file and add this to your /etc/default/locale `export LANG=en_US.UTF-8` replacing en_US.UTF-8 with whatever you want for your locale . – echochamber Aug 14 '14 at 02:47
0

Any solution based on setlocale() (as in @Wouter J's answer) will sooner or later give you a headache, since it relies on the locale setting of the current machine. So whenever you switch server, it might stop working. And it can differ between your development and production server.

A better approach is to drop strftime() in favor of IntlDateFormatter::formatObject:

\IntlDateFormatter::formatObject($entity->getTimestamp(), \IntlDateFormatter::FULL, 'de_AT');

This only requires that you have the PHP intl extension installed. There's also a Symfony replacement for PHP's native \IntlDateFormatter but I guess that's only necessary if you don't have the intl extension available.

For Twig you can use the localizeddate filter. Installation instructions: https://stackoverflow.com/a/24390635/1668200

Thomas Landauer
  • 7,857
  • 10
  • 47
  • 99