SimpleSAML\Locale\Language::getLanguageCookie PHP Method

getLanguageCookie() public static method

Retrieve the user-selected language from a cookie.
public static getLanguageCookie ( ) : string | null
return string | null The selected language or null if unset.
    public static function getLanguageCookie()
    {
        $config = \SimpleSAML_Configuration::getInstance();
        $availableLanguages = $config->getArray('language.available', array('en'));
        $name = $config->getString('language.cookie.name', 'language');
        if (isset($_COOKIE[$name])) {
            $language = strtolower((string) $_COOKIE[$name]);
            if (in_array($language, $availableLanguages, true)) {
                return $language;
            }
        }
        return null;
    }

Usage Example

 /**
  * Test SimpleSAML\Locale\Language::getLanguageCookie().
  */
 public function testGetLanguageCookie()
 {
     // test it works when no cookie is set
     \SimpleSAML_Configuration::loadFromArray(array(), '', 'simplesaml');
     $this->assertNull(Language::getLanguageCookie());
     // test that it works fine with defaults
     \SimpleSAML_Configuration::loadFromArray(array(), '', 'simplesaml');
     $_COOKIE['language'] = 'en';
     $this->assertEquals('en', Language::getLanguageCookie());
     // test that it works with non-defaults
     \SimpleSAML_Configuration::loadFromArray(array('language.available' => array('en', 'es', 'nn'), 'language.cookie.name' => 'xyz'), '', 'simplesaml');
     $_COOKIE['xyz'] = 'Es';
     // test values are converted to lowercase too
     $this->assertEquals('es', Language::getLanguageCookie());
 }
All Usage Examples Of SimpleSAML\Locale\Language::getLanguageCookie