Currently when editing a node with a date field, the date value that comes from the database appears only in english, even if you have your website with another language as the default language and you have the completed translation of the date module. This problem appears when you have created your date field with one of these widget types: "Text Field with custom input format" or "Text Field with Date Pop-up calendar". In my case my website is only in spanish and everytime the users want to edit a node with a date field, even if the date is correct they have to change it because it appears in english and if they press the button Save (Guardar) without doing it then the website shows an error saying that the date they have entered is not valid. As you can see this is totally against usability and is definitely a headache for our users.

The reason of the problem is that this module is using a PHP function that only works in english, date_format().
I managed to solve this issue modifying 2 lines of code and adding 2 small functions to the date_popup.module file, wich is inside the module/date/date_popup folder. In my case i only needed to translate the month names so i didn't change the day names.

I hope this people include this fix (or a better one)

Lines changed:
247: '#default_value' => (!empty($element['#value']['date']) || !empty($edit['date'])) && is_object($date) ? traducir_fecha(date_format($date, $date_format)) : '',

259: $sub_element['#description'] = ' '. t('Format: @date', array('@date' => traducir_fecha(date($date_format, time()))));

This one is for the hint message that appear below the date field.

The 2 small functions are:

<pre>
/*Added by Efrain Herrera - Latinosoft
 * latinosoft@gmail.com
 * latinosoft@latinosoftpsa.com
 * Colombia
 * 
 * Changes the months to the regexp format
 * */

function cambiar_a_regexp(&$valor,$key){
	$valor= '/'.$valor.'/';
}


/*Added by Efrain Herrera - Latinosoft
 * latinosoft@gmail.com
 * latinosoft@latinosoftpsa.com
 * Colombia
 * 
 * Translate month names from english to your user's language
 * */
function traducir_fecha($fecha){	
	//the date_month_names_untranslated function is defined in the date_api.module file
	//the date_month_names and date_month_names_abbr functions are defined in the date_api.module file

	//we created an array with the month names wich is used as patterns
	$patterns = date_month_names_untranslated()+array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	//we added the / character before and after each month name. (this is to match the preg_replace regexp format)
	array_walk($patterns,'cambiar_a_regexp');
	
	//we created an array with the same values translated
	$replacements = date_month_names(TRUE)+date_month_names_abbr(TRUE);

	return preg_replace($patterns, $replacements, $fecha);
}
</pre>
CommentFileSizeAuthor
#4 675374-4.patch2.54 KBroderik
#3 675374.patch2.51 KBroderik
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

latinosoft’s picture

Ignore the PRE tags on the code above, this was my first post ;p

iNade’s picture

Status: Needs review » Reviewed & tested by the community

Reviewed and tested by... me : works like a charm!

What about a patch ?

Tanks.

roderik’s picture

Title: When editing a node the date value on a date field is showed only in english ignoring your website language » Edit widgets format dates in English always (causing validation errors or data corruption)
Assigned: latinosoft » Unassigned
Status: Reviewed & tested by the community » Needs review
FileSize
2.51 KB

Your subroutine isn't necesssary. There's code in Drupal which can format dates correctly -- format_date().
Or probably better - there's an extended function named date_format_date().

Patch attached. Please test and set RTBC again.

As a side note:
I also witnessed a creepy side effect of the buggy code... silent data corruption.
- entered a date as '8 mei 2009' (Dutch) and pressed 'Preview'. Same effect would be if you edited an existing node...
- the date came up as '8 May 2009' (as noted above). I did nothing and pressed 'Preview' again...
- the date was validated (against expectation) and... changed to '8 Dec 2008' !

roderik’s picture

FileSize
2.54 KB

Silly mistake. The #description parts of the form elements would generate an error -- because I was passing a timestamp instead of a DateTime object.

I am now passing date_make_date('now') instead of time(). No idea if there's unnecessary overhead in that. I assume that the committer will see that at once.

mvc’s picture

Status: Needs review » Reviewed & tested by the community

patch #4 works perfectly for me on all languages on a multilingual site. thanks, roderik.

AndyThornton’s picture

Thanks very much for this patch. We have a multilingual site and had this complaint. The patch works well. It looks like a variation of this has been rolled into the dev version of the module, right?

Cheers,
Andy

KarenS’s picture

Status: Reviewed & tested by the community » Fixed

This is already in the dev version, there may have been another issue that made the same change.

Status: Fixed » Closed (fixed)
Issue tags: -date field showed only english ignoring language when editing node

Automatically closed -- issue fixed for 2 weeks with no activity.