Jump to content

Problem displaying a datetime field in spanish


landitus
 Share

Recommended Posts

Hi all! As always, a pleasure working with PW! But I'm stuck with the following. I have a blog-type page where each entry has a date time field. I've configured the date output as: "j \d\e F \d\e Y". But the month is shown in English (ex. November) I need to use the date in spanish, but I've read some php docs and they say I should use setlocale with strftime. But the output date I get from PW should be as an Unix timestamp I believe.

How would you approach this?

Link to comment
Share on other sites

Try this where you output the date:

<?php
setlocale(LC_TIME, 'es_ES'); // substitute your locale if not es_ES
echo strftime('%e de %B de %Y', $page->getUnformatted('date')); // substitute your date field name

This should produce the following output for today's date:

8 de noviembre de 2011

We're going to be providing localization support for the date field with ProcessWire 2.2, but the method described here will continue to be backwards compatible.

Note you can also put the setlocale() part in your /site/config.php if you prefer.

What locale you can use depends somewhat on what are installed on your server. I think that es_ES (or es_ES.UTF-8) is fairly standard, but if you want to get a list of locales you can type this in the unix shell:

locale -a 
  • Like 1
Link to comment
Share on other sites

Just 5 min ago used setlocale for finnish content. Had to install it first (on ubuntu): sudo apt-get install language-pack-fi-base and after that this spell did the magic: setlocale(LC_ALL, array('fi_FI.UTF-8','fi_FI@euro','fi_FI','finnish'));

I found help from here: http://www.php.net/manual/en/function.setlocale.php#90919

Actually on ubuntu it seems that only different en_ locales are installed by default. Probably most web hosts have most popular languages covered.

Link to comment
Share on other sites

  • 3 months later...

Actually LanguageSupport includes a setLocale setting, but it needs to be defined with the language pack. If you go into Setup > Languages > [your language] > add translation file >

Then paste in:

/wire/modules/LanguageSupport/LanguageSupport.module

You'll see a field come up where you can set the locale. Maybe I should have this disabled by default? (if the translation doesn't define it)

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Just to make sure I understand correctly. Do you think it's best to replace this (in LanguageSupport.module):

setlocale(LC_ALL, $this->_('C')); 

with this?

$locale = $this->_('none'); 
if($locale != 'none') setlocale(LC_ALL, $locale); 

The above would only call setlocale() if the language defined a locale in the translations.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...