Php multilanguage date: howto?

后端 未结 3 398
隐瞒了意图╮
隐瞒了意图╮ 2020-12-21 06:20

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

相关标签:
3条回答
  • 2020-12-21 06:27

    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;
    }
    
    0 讨论(0)
  • 2020-12-21 06:31

    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.

    0 讨论(0)
  • 2020-12-21 06:46

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

    0 讨论(0)
提交回复
热议问题