Localize date format?

Are you responsible for making OCS work -- installing, upgrading, migrating or troubleshooting? Do you think you've found a bug? Post in this forum.

Moderators: jmacgreg, michael, John

Forum rules
The Public Knowledge Project Support Forum is moving to http://forum.pkp.sfu.ca

This forum will be maintained permanently as an archived historical resource, but all new questions should be added to the new forum. Questions will no longer be monitored on this old forum after March 30, 2015.
joelproko
Posts: 11
Joined: Tue Jul 02, 2013 5:25 am

Localize date format?

Postby joelproko » Tue Jul 02, 2013 5:38 am

Hi everyone
I'm currently trying to set up an OCS site in 4 different languages, and have hit a bit of a snag.
In English, the dates show up as "June 19, 2014", which is perfect. But in, say, German, they show up as "Juni 19, 2014", which is really bad. Now I could just change date_format_long et al., but then it would be "19. June 2014" in English, which would be totally wrong as well.
Is there a way to localize the date format too? That is, have date_format_long and its siblings depend on the locale the visitor is using, just like the rest of OCS? I have some experience with smarty, but I have no idea how to combine Applocale::Translate with Config::getVar or however this could be solved.
Greetings,
Joel

asmecher
Posts: 10015
Joined: Wed Aug 10, 2005 12:56 pm
Contact:

Re: Localize date format?

Postby asmecher » Wed Jul 03, 2013 10:21 am

Hi Joel,

This would require a modification. See lib/pkp/classes/template/PKPTemplateManager.inc.php, near line 102 (depending on your version):

Code: Select all

$this->assign('datetimeFormatShort', Config::getVar('general', 'datetime_format_short'));
$this->assign('datetimeFormatLong', Config::getVar('general', 'datetime_format_long'));
$this->assign('timeFormat', Config::getVar('general', 'time_format'));
This is where date/time formats are fetched from the configuration file and made available to the templates. Later on, near line 115, the line:

Code: Select all

$this->assign('currentLocale', $locale);
...is responsible for exposing the current locale to the templates.

You will need to rearrange this code a little bit so that different formats are supplied to the templates depending on the value of the $locale variable.

Regards,
Alec Smecher
Public Knowledge Project Team

joelproko
Posts: 11
Joined: Tue Jul 02, 2013 5:25 am

Re: Localize date format?

Postby joelproko » Tue Jul 09, 2013 7:05 am

Ah thanks, just about what I found myself. For others who wonder, this is how I solved the problem:

lib/pkp/classes/template/PKPTemplateManager.inc.php (starting from line 90)

Code: Select all

      $this->assign('dateFormatTrunc', Config::getVar('general', 'date_format_trunc'));
      $this->assign('dateFormatShort', Config::getVar('general', 'date_format_short'));
      //$this->assign('dateFormatLong', Config::getVar('general', 'date_format_long')); //REMOVED by J.P. to make locale-dependant
      $this->assign('datetimeFormatShort', Config::getVar('general', 'datetime_format_short'));
      //$this->assign('datetimeFormatLong', Config::getVar('general', 'datetime_format_long')); //REMOVED by J.P. to make locale-dependant
      $this->assign('timeFormat', Config::getVar('general', 'time_format'));
      $this->assign('allowCDN', Config::getVar('general', 'enable_cdn'));

      $locale = AppLocale::getLocale();
      $this->assign('currentLocale', $locale);
      //BEGIN manual insert by J.P. {
      if (Config::getVar('general', 'date_format_long_'.$locale)!="")
         $this->assign('dateFormatLong', Config::getVar('general', 'date_format_long_'.$locale));
      else
         $this->assign('dateFormatLong', Config::getVar('general', 'date_format_long'));
      if (Config::getVar('general', 'datetime_format_long_'.$locale)!=0)
         $this->assign('datetimeFormatLong', Config::getVar('general', 'datetime_format_long_'.$locale));
      else
         $this->assign('datetimeFormatLong', Config::getVar('general', 'datetime_format_long'));
      //END manual insert by J.P. }


config.inc.php (starting from line 52)

Code: Select all

; Short and long date formats
date_format_trunc = "%m-%d"
date_format_short = "%Y-%m-%d"
date_format_long = "%B %e, %Y"
date_format_long_en_US = "%B %e, %Y"
date_format_long_de_DE = "%e. %B %Y"
date_format_long_fr_CA = "%e %B %Y"
date_format_long_it_IT = "%e %B %Y"
datetime_format_short = "%Y-%m-%d %H:%M"
datetime_format_long = "%B %e, %Y - %H:%M"
datetime_format_long_en_US = "%B %e, %Y - %H:%M"
datetime_format_long_de_DE = "%e. %B %Y - %H:%M"
datetime_format_long_fr_CA = "%e %B %Y - %H:%M"
datetime_format_long_it_IT = "%e %B %Y - %H:%M"
time_format = "%H:%M"


This could of course be expanded to also apply to date_format_short etc.; I only needed to change the long ones.
I'm aware it isn't the most beautiful solution, what with going to the config file instead of a corresponding localization file, but at least the formats aren't hardcoded in the class file.


Return to “OCS Technical Support”

Who is online

Users browsing this forum: No registered users and 1 guest