Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I previously was able to change the numbers of the date and time (into a number and date system of another language, like Khmer) for each post using a script like this in my functions.php:

function KhmerNumDate ($text) {
$text = str_replace('1', '១', $text);
$text = str_replace('2', '២', $text);
$text = str_replace('3', '៣', $text);
$text = str_replace('4', '៤', $text);
$text = str_replace('5', '៥', $text);
$text = str_replace('6', '៦', $text);
$text = str_replace('7', '៧', $text);
$text = str_replace('8', '៨', $text);
$text = str_replace('9', '៩', $text);
$text = str_replace('0', '០', $text); 
return $text;
}


add_filter('date', 'KhmerNumDate');
add_filter('the_date', 'KhmerNumDate');
add_filter('the_time', 'KhmerNumDate');

But now it is not working - is my code good? Would the code differ based on which theme I am using (I am currently using a modified child theme of Twenty-Twelve)?

share|improve this question
add comment

2 Answers

I would like suggest using a Wordpress built-in function for your specific need which is date_i18n($date_format, $time, $gmt). Just pass the php date() format and the timestamp and Wordpress will display the date in your language (Language defined in wp-config.php).

Example from the codex page:

echo date_i18n(get_option('date_format') ,strtotime("11/15-1976")); 
share|improve this answer
    
Unfortunately, for those languages that do not use roman numbers (1 2 3...) it will not change codex.wordpress.org/…. So to change the numbering system, you have to use a script (to my knowledge). But you are right for month names - I have modified my question to show that (because actually month names and day names can just be translated in the .po files of the theme). –  Nathan Nov 5 '12 at 16:26
    
There is also a Wordpress function that convert numbers into local format: number_format_i18n($number), there is no codex reference for this function, but it should return the number in the localized format of the blog. –  Ahmad M Nov 5 '12 at 16:44
add comment

Turns out it was because of the way the theme Twenty Twelve works (in their effort to make translation easy...).

The date for posts in Twenty Twelve is prepared by a function in functions.php called twentytwelve_entry_meta()

So to replace/translate the numbers in the date, I look for the line with $date= in the twentytwelve_entry_meta() function (or preferably replicate the twentytwelve_entry_meta() function in a child functions.php as I did):

$date = sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a>',
        esc_url( get_permalink() ),
        esc_attr( get_the_time() ),
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() )

    );

    //my added code - the_curlang() is used with the xili-language plugin, it returns the current language of the page

$languagedate = the_curlang();

if ( $languagedate == 'km_kh' ) {
$date = str_replace('1', '១', $date);
$date = str_replace('2', '២', $date);
$date = str_replace('3', '៣', $date);
$date = str_replace('4', '៤', $date);
$date = str_replace('5', '៥', $date);
$date = str_replace('6', '៦', $date);
$date = str_replace('7', '៧', $date);
$date = str_replace('8', '៨', $date);
$date = str_replace('9', '៩', $date);
$date = str_replace('0', '០', $date); 
}

Then everything looks great!

I'm not much of a programmer, so my code might not be the best option, but at least it works.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.