Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

datetime - Php multilanguage date: howto?

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.php the code include the right classes definition then create one object and call the function display() 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 translation object, that I'm calling $t for 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.php and cache.fr.php
  • in cache.us.php you have a line like this: $thanks_for_the_fish = "Thanks for the fish".
  • in cache.fr.php you 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...