3

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?

Community
  • 1
  • 1
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
  • You should keep weekday names separate and translated and put them in long date string. Something like `$french_monday.', 1 septembre 2006';` – egis Jul 02 '12 at 09:30
  • If so, how would you do with english? Arabian? Mandarin chinese? – Olivier Pons Jul 02 '12 at 09:32
  • 1
    Sorry, I'm in a bit of a hurry, so maybe I skipped some important part, but is there a reason not to use the PHP `intl` extension? The [IntlDateFormatter](http://www.php.net/manual/en/class.intldateformatter.php) class seems to do exactly what you need. – Jakub Lédl Jul 02 '12 at 09:50
  • Thanks a lot your suggestion could be a valid option, may I ask you to write a detailed answer so that I can check it as valid, to help the community? Thanks a lot! – Olivier Pons Jul 03 '12 at 14:20

3 Answers3

4

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.

Jakub Lédl
  • 1,805
  • 3
  • 17
  • 26
  • Thank you very much, I've installed this extension and I'll modify my multilanguage class to use this incredibly powerful tool. – Olivier Pons Jul 04 '12 at 18:52
0

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;
}
Olivier Pons
  • 15,363
  • 26
  • 117
  • 213
0

A simple solution for you, check it out maybe it help you https://github.com/LeonardoCaitano/MyDateTime