<?php
-
/**
* Contains the LanguageConverter class and ConverterRule class
- * @ingroup Language
*
- * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
* @file
+ * @ingroup Language
*/
/**
/**
* Get preferred language variant.
- * @param $fromUser Boolean: get it from $wgUser's preferences
- * @param $fromHeader Boolean: get it from Accept-Language
* @return String: the preferred language code
*/
- public function getPreferredVariant( $fromUser = true, $fromHeader = false ) {
- global $wgDefaultLanguageVariant;
+ public function getPreferredVariant() {
+ global $wgDefaultLanguageVariant, $wgUser;
$req = $this->getURLVariant();
- if ( $fromUser && !$req ) {
+ if ( $wgUser->isLoggedIn() && !$req ) {
$req = $this->getUserVariant();
}
- if ( $fromHeader && !$req ) {
+ elseif ( !$req ) {
$req = $this->getHeaderVariant();
}
return $this->mMainLanguageCode;
}
+ /**
+ * Get default variant.
+ * This function would not be affected by user's settings or headers
+ * @return String: the default variant code
+ */
+ public function getDefaultVariant() {
+ global $wgDefaultLanguageVariant;
+
+ $req = $this->getURLVariant();
+
+ if ( $wgDefaultLanguageVariant && !$req ) {
+ $req = $this->validateVariant( $wgDefaultLanguageVariant );
+ }
+
+ if ( $req ) {
+ return $req;
+ }
+ return $this->mMainLanguageCode;
+ }
+
/**
* Validate the variant
* @param $variant String: the variant to validate
*
* @return Mixed: variant if one found, false otherwise.
*/
- protected function getURLVariant() {
+ public function getURLVariant() {
global $wgRequest;
- $ret = null;
if ( $this->mURLVariant ) {
return $this->mURLVariant;
*/
protected function getUserVariant() {
global $wgUser;
- $ret = null;
// memoizing this function wreaks havoc on parserTest.php
/* if ( $this->mUserVariant ) { */
return $this->mUserVariant = $this->validateVariant( $ret );
}
-
/**
* Determine the language variant from the Accept-Language header.
*
*/
protected function getHeaderVariant() {
global $wgRequest;
- $ret = null;
if ( $this->mHeaderVariant ) {
return $this->mHeaderVariant;
// see if some supported language variant is set in the
// http header.
- $acceptLanguage = $wgRequest->getHeader( 'Accept-Language' );
- if ( !$acceptLanguage ) {
+ $languages = array_keys( $wgRequest->getAcceptLang() );
+ if ( empty( $languages ) ) {
return null;
}
- // explode by comma
- $result = StringUtils::explode( ',', strtolower( $acceptLanguage ) );
- $languages = array();
-
- foreach ( $result as $elem ) {
- // if $elem likes 'zh-cn;q=0.9'
- if ( ( $posi = strpos( $elem, ';' ) ) !== false ) {
- // get the real language code likes 'zh-cn'
- $languages[] = substr( $elem, 0, $posi );
- } else {
- $languages[] = $elem;
- }
- }
-
$fallback_languages = array();
foreach ( $languages as $language ) {
- // strip whitespace
- $language = trim( $language );
$this->mHeaderVariant = $this->validateVariant( $language );
if ( $this->mHeaderVariant ) {
break;
$out = '';
$length = strlen( $text );
while ( $startPos < $length ) {
- $m = false;
$pos = strpos( $text, '-{', $startPos );
if ( $pos === false ) {
// No more markup, append final segment
$out .= $this->autoConvert( substr( $text, $startPos ), $variant );
- $startPos = $length;
return $out;
}
*/
function parseRules() {
$rules = $this->mRules;
- $flags = $this->mFlags;
$bidtable = array();
$unidtable = array();
$variants = $this->mConverter->mVariants;
$variant = $this->mConverter->getPreferredVariant();
}
- $variants = $this->mConverter->mVariants;
$this->parseFlags();
$flags = $this->mFlags;