问题
Nota: this is not a duplicate of Translating PHP date() for Multilingual Site . I've read it!
I'm sorry, I first have to explain how my framework works, so you can understand precisely where my problem is:
Here's how my Php code works (rough principles). Let's use for an example someone who wants to see the URL http://myweb.com/valid.php:
- in the file
valid.phpthe code include the right classes definition then create one object and call the functiondisplay()which displays the page. - in the file
valid.php, when the object is created, it analyses the host, and in the host there's the language (http://us.myweb.com/,http://cn.myweb.com/,http://fr.myweb.com/...) and the default language (if none found) is english - then I load a cached Php file where the are the translations. This is a
translationobject, that I'm calling$tfor short - and from now on, whenever I want a translation, I just do things like
$t->get('my_string').
Let's take an example with 2 languages file:
- 2 languages files:
cache.us.phpandcache.fr.php - in
cache.us.phpyou have a line like this:$thanks_for_the_fish = "Thanks for the fish". - in
cache.fr.phpyou have a line like this:$thanks_for_the_fish = "Merci pour le poisson". - I construct my page, include the right language file then call
$t->get('thanks_for_the_fish')and it's translated.
Now my problem comes with date formatting. With short date format it's not a problem:
- in
cache.us.php:$short_date_format = "m/d/Y, H:i". - in
cache.fr.php:$short_date_format = "d/m/Y à H:i".
But with long date format, I'm just working on french and I begun with something like:
- in
cache.fr.php:$long_date_format = "%s, %d %s %d". - then all the days:
$sunday = "dimanche",$monday = "lundi"and so on - then in my translation code:
Something like this (read carefully the comment in the code, my question is in it!):
static private $_TabStrDaysOfWeek = array(
0 => 'sunday',
1 => 'monday',
...,
6 => 'saturday'
);
public function translateDate($date_time)
{
$long_day = $this->tr->get(
$this->_TabStrDaysOfWeek[ $date_time->format('w') ]
);
/*
here's where I'm stuck:
what could be the code to be able to display:
- english:
Monday, 1st September 2006
Tuesday, 2nd September 2006
Wednesday, 3rd September 2006
Thursday, 4th September 2006
- french:
Lundi, 1 septembre 2006
Mardi, 2 septembre 2006
Mercredi, 3 septembre 2006
Jeudi, 4 septembre 2006
- arabian!!:
1 - Don't know
2 - Don't know
3 - Don't know
4 - Don't know
*/
}
... And I said arabian because I'll need it sooner or later, same for Mandarin Chinese. All my other translation problems are solved but this one!!
Any idea?
回答1:
For internationalization tasks, I'd strongly recommend using the PHP intl extension. It contains several classes for common internationalization tasks such as date/time formatting, number formatting, string transliteration and more. Specifically, the IntlDateFormatter class is able to format (and parse) a datetime for any available locale.
回答2:
Here's how I've done it: it seems there's no possibility other than do a switch and handle each language separately:
Here's what's in my cache:
$this->jour_dimanche = dimanche
$this->jour_lundi = lundi
$this->jour_mardi = mardi
$this->jour_mercredi = mercredi
$this->jour_jeudi = jeudi
$this->jour_vendredi = vendredi
$this->jour_samedi = samedi
$this->mois_janvier = janvier
$this->mois_fevrier = février
$this->mois_mars = mars
$this->mois_avril = avril
$this->mois_mai = mai
$this->mois_juin = juin
$this->mois_juillet = juillet
$this->mois_aout = août
$this->mois_septembre = septembre
$this->mois_octobre = octobre
$this->mois_novembre = novembre
$this->mois_decembre = décembre
// long date format = 'day, (month number) (month) (year)'
// '%s, %d %s %d' => 'Mardi, 2 juillet 2012'
$this->date_format_long = %\s, j %\s Y à H:i
...And my Php code:
public function translateDate($date_time, $first_upcase=true)
{
switch ($this->_trad->getLang()) {
/* TODO: all other languages */
case 'us':
case 'ar':
case 'es':
case 'cn':
throw new Exception("not handled yet");
break;
default:
/* default = french */
$day = $this->_trad->get(
self::$_TabStrDaysOfWeek[ $date_time->format('w') ]
);
$month = $this->_trad->get(
self::$_TabStrMonths[ $date_time->format('j') ]
);
$ret = sprintf(
$date_time->format(
$this->_trad->get('date_format_long')
),
$day,
$month
);
if ($first_upcase) {
$ret = ucfirst($ret);
}
break;
}
return $ret;
}
回答3:
A simple solution for you, check it out maybe it help you https://github.com/LeonardoCaitano/MyDateTime
来源:https://stackoverflow.com/questions/11290702/php-multilanguage-date-howto