Development

Changeset 6690

You must first sign up to be able to contribute.

Changeset 6690

Show
Ignore:
Timestamp:
12/24/07 13:56:19 (1 year ago)
Author:
fabien
Message:

added sfWebRequest::getPreferredCulture()

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1.1/lib/request/sfWebRequest.class.php

    r6510 r6690  
    553553 
    554554    return isset($pathArray['REQUEST_METHOD']) ? $pathArray['REQUEST_METHOD'] : 'GET'; 
     555  } 
     556 
     557  /** 
     558   * Returns the preferred culture for the current request. 
     559   * 
     560   * @param  array  An array of ordered cultures available 
     561   * 
     562   * @return string The preferred culture 
     563   */ 
     564  public function getPreferredCulture(array $cultures = null) 
     565  { 
     566    $preferredCultures = $this->getLanguages(); 
     567 
     568    if (is_null($cultures)) 
     569    { 
     570      return isset($preferredCultures[0]) ? $preferredCultures[0] : null; 
     571    } 
     572 
     573    if (!$preferredCultures) 
     574    { 
     575      return $cultures[0]; 
     576    } 
     577 
     578    $preferredCultures = array_values(array_intersect($preferredCultures, $cultures)); 
     579 
     580    return isset($preferredCultures[0]) ? $preferredCultures[0] : null; 
    555581  } 
    556582 
  • branches/1.1/test/unit/request/sfWebRequestTest.php

    r4957 r6690  
    1111require_once(dirname(__FILE__).'/../../bootstrap/unit.php'); 
    1212 
    13 $t = new lime_test(13, new lime_output_color()); 
     13$t = new lime_test(16, new lime_output_color()); 
    1414 
    1515class myRequest extends sfWebRequest 
     
    3535$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-us,en;q=0.5,fr;q=0.3'; 
    3636$t->is($request->getLanguages(), array('en_US', 'en', 'fr'), '->getLanguages() returns an array with all accepted languages'); 
     37 
     38// ->getPreferredCulture() 
     39$t->diag('->getPreferredCulture()'); 
     40$request->languages = null; 
     41$_SERVER['HTTP_ACCEPT_LANGUAGE'] = ''; 
     42$t->is($request->getPreferredCulture(array('fr', 'en')), 'fr', '->getPreferredCulture() returns the first given culture if the client do not send an ACCEPT_LANGUAGE header'); 
     43 
     44$request->languages = null; 
     45$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-us,en;q=0.5,fr;q=0.3'; 
     46$t->is($request->getPreferredCulture(array('fr', 'en')), 'en', '->getPreferredCulture() returns the preferred culture'); 
     47 
     48$request->languages = null; 
     49$_SERVER['HTTP_ACCEPT_LANGUAGE'] = 'en-us,en;q=0.5,fr'; 
     50$t->is($request->getPreferredCulture(array('fr', 'en')), 'fr', '->getPreferredCulture() returns the preferred culture'); 
    3751 
    3852// ->getCharsets()