Language Translation for PHP core date() formatting
OK, so I have a large and complicated "upcoming events" PHP module that I work with frequently, I'm currently working on a multi-lingual (French / English) site and I need to translate the language of my PHP dates.
What I DO NOT WANT TO DO is nest a ton of conditional PHP within the function itself. Can someone help me to come up with a series of conditionals statements that check if
<?php if (ICL_LANGUAGE_CODE == 'fr') { } ?>
is true and then simply change the month variables after the function has processed. Forgive my ingorance, I'm just a bit slow when it comes to syntax. Can someone tell me what it would look like to check if a specific output is echoed and then translate it. The following is not working
<?php if (ICL_LANG开发者_开发问答UAGE_CODE == 'fr') { February == Fevrier } ?>
EDIT:
I've considered using a localization framework, however I'm developing this site on WordPress and using the WPML (wordpress multi-lingual plugin). It's been a real life-saver. I'm hesitant to add complex frameworks. Is this one lightweight? I'd prefer to just translate the strings that I know to expect. The only formats are - Jan 01 // January 1, 2011 ... day names don't appear on the site and other string translations would be unnecessary.
Create arrays with your strings, with keys for each supported language:
$months = array(
1=>array(
'fr'=>'Janvier',
'en'=>'January'
),
2=>array(
'fr'=>'Février',
'en'=>'February'
),
3=>array(
'fr'=>'Mars',
'en'=>'March'
) // and so on...
);
$days = array(
1=>array(
'en'=>'Monday',
'fr'=>'Lundi'
),
2=>array(
'en'=>'Tuesday',
'fr'=>'Mardi'
) // and so on...
);
Then you can access the strings like this:
$days[$day_of_week][ICL_LANGUAGE_CODE]
$days[$month][ICL_LANGUAGE_CODE]
...and so on.
The suggestions you got above are all valid and would work fine.
WPML includes a String Translation facility which can also do this and keep the code clean. The advantage would be that your code will include texts in one language and the translation will be done in WPML's String Translation editor.
If you ever choose to add more languages, you don't need to change anything in the code. Just add more translations in WPML.
Have a look here: http://wpml.org/documentation/support/translation-for-texts-by-other-plugins-and-themes/
You'll need to use *icl_register_string* to register translatable strings. Then, when outputting, pass through *icl_t*. This mechanism is similar to GetText, with the exception that it supports both static and dynamic texts.
If you do this, I suggest that you choose a new context field for your strings. This would make it easy to locate them in the strings translation editor.
WPML caches results and loads all strings with the same context together, so you're not going to feel any performance hit by this.
This is what I use to quickly translate date to french in wordpress:
<?php setlocale(LC_TIME, "fr_FR"); ?>
<?php echo utf8_encode(strftime("%e %B, %G", strtotime(the_date('','','',FALSE)))); ?>
Try this approach if your project is not highload, which is enough flexible:
function t_date($format, $date=FALSE, $lang=LANG)
{
$months['January'] = array('uz'=>'Yanvar');
$months['February'] = array('uz'=>'Fevral');
$months['March'] = array('uz'=>'Mart');
$months['April'] = array('uz'=>'Aprel');
$months['May'] = array('uz'=>'May');
$months['June'] = array('uz'=>'Iyun');
$months['July'] = array('uz'=>'Iyul');
$months['August'] = array('uz'=>'Avgust');
$months['September'] = array('uz'=>'Sentabr');
$months['October'] = array('uz'=>'Oktabr');
$months['November'] = array('uz'=>'Noyabr');
$months['December'] = array('uz'=>'Dekabr');
$days['Monday'] = array('uz'=>'Dushanba');
$days['Tuesday'] = array('uz'=>'Seshanba');
$days['Wednesday'] = array('uz'=>'Chorshanba');
$days['Thursday'] = array('uz'=>'Payshanba');
$days['Friday'] = array('uz'=>'Juma');
$days['Saturday'] = array('uz'=>'Shanba');
$days['Sunday'] = array('uz'=>'Yakshanba');
if ($date)
$date = date($format, strtotime($date));
else
$date = date($format, time());
foreach ($months as $key => $val)
$date = str_replace($key, $val[$lang], $date);
foreach ($days as $key => $val)
$date = str_replace($key, $val[$lang], $date);
return $date;
}
Changing php language to wp
if (ICL_LANGUAGE_CODE == 'fr') {
setlocale(LC_ALL, 'fr_FR');
} else if (ICL_LANGUAGE_CODE == 'de') {
setlocale(LC_ALL, 'de_DE');
} else if (ICL_LANGUAGE_CODE == 'it') {
setlocale(LC_ALL, 'it_IT');
} else if (ICL_LANGUAGE_CODE == 'en') {
setlocale(LC_ALL, 'en_EN');
}
String from time
$translated_date = strftime("%e %B %Y",strtotime($post->post_date));
Safe encoding
$translated_date = utf8_encode($translated_date);
精彩评论