Welcome Guest, Not a member yet? Register   Sign In
Localization - where to put the code?
#1

My projects often show dates and times in a localized format. In my case: Dutch.
For that purpose, I have the following lines of code in my index.php:
Code:
date_default_timezone_set('Europe/Amsterdam');
setlocale(LC_TIME, 'Dutch');
setlocale(LC_ALL, array('Dutch_Netherlands', 'Dutch', 'nl_NL', 'nl', 'nl_NL.UTF-8'));
I'm not sure whether index.php is the right place for that.
Most updates of CI require overwriting the index.php with a new version, which means loss of custom code in that file.
What's the best place to put my localization code?
Reply
#2

You could use your application/config/config.php file. One way would be to put the lines of code there. They should execute but I haven't tested that. Another way would be to place only the values there ('Europe/Amsterdam', 'Dutch', etc.). Then, in your default controller, you could have the code that retrieves the values and sets the local. For example,
PHP Code:
date_default_timezone_set($this->config->item('timezone'); 

That's just my opinion. I am no authority in best practices!
Hey, don't work without a PHP debugger. Several free IDEs have this features built in. Two are NetBeans and CodeLobster. Without a debugger, it's like you're driving with a blindfold on -- you are going to crash!
Reply
#3

My index.php includes a call to date_default_timezone_set('UTC'). When I display times/dates to the user, I convert them to the user's timezone.

PHP Code:
$origDate = new DateTime($dateFromDb, new DateTimeZone('UTC'));
$origDate->setTimeZone(new DateTimeZone($userTimeZone));
echo 
$origDate->format($userFormat); 
Reply
#4

I know this is an old thread, but I have been experimenting with config.php lately. Like RobertSF suggested, code that is in config.php is always executed. So I don't put this code in index.php anymore. This makes updating my sites to a new CI-version much easier. In most cases, the system folder and the index.php file must be replaced.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB