return $this->mPreferredVariant;
}
- # FIXME rewrite code for parsing http header. The current code
- # is written specific for detecting zh- variants
if( !$this->mPreferredVariant ) {
// see if some supported language variant is set in the
// http header, but we don't set the mPreferredVariant
// variable in case this is called before the user's
// preference is loaded
- $pv=$this->mMainLanguageCode;
- if(array_key_exists('HTTP_ACCEPT_LANGUAGE', $_SERVER)) {
- $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
- $zh = strstr($header, $pv.'-');
- if($zh) {
- $ary = split("[,;]",$zh);
- $pv = $ary[0];
+ if( array_key_exists( 'HTTP_ACCEPT_LANGUAGE', $_SERVER ) ) {
+ $acceptLanguage = strtolower( $_SERVER['HTTP_ACCEPT_LANGUAGE'] );
+
+ // explode by comma
+ $result = explode(',', $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;
+ }
+ }
+
+ foreach( $languages as $language ) {
+ // strip whitespace
+ $language = trim( $language );
+ if( in_array( $language, $this->mVariants ) ) {
+ return $language;
+ break;
+ }
}
}
- // don't try to return bad variant
- if(in_array( $pv, $this->mVariants ))
- return $pv;
}
return $this->mMainLanguageCode;
* @public
*/
function armourMath($text){
+ // we need to convert '-{' and '}-' to '-{' and '}-'
+ // to avoid a unwanted '}-' appeared after the math-image.
+ $text = strtr( $text, array('-{' => '-{', '}-' => '}-') );
$ret = $this->mMarkup['begin'] . 'R|' . $text . $this->mMarkup['end'];
return $ret;
}
$flags=array_diff($flags,array('S'));
$flags_temp = array();
foreach ($variants as $variant) {
+ // try to find flags like "zh-hans", "zh-hant"
+ // allow syntaxes like "-{zh-hans;zh-hant|XXXX}-"
if ( in_array($variant, $flags) )
$flags_temp[] = $variant;
}
- if ( count($flags_temp) == 0 )
+ if ( count($flags_temp) !== 0 )
$flags = $flags_temp;
}
if ( count($flags) == 0 )
$bidtable = array();
$unidtable = array();
$markup = $this->mConverter->mMarkup;
+ $variants = $this->mConverter->mVariants;
+
+ // varsep_pattern for preg_split:
+ // text should be splited by ";" only if a valid variant
+ // name exist after the markup, for example:
+ // -{zh-hans:<span style="font-size:120%;">xxx</span>;zh-hant:<span style="font-size:120%;">yyy</span>;}-
+ // we should split it as:
+ // array(
+ // [0] => 'zh-hans:<span style="font-size:120%;">xxx</span>'
+ // [1] => 'zh-hant:<span style="font-size:120%;">yyy</span>'
+ // [2] => ''
+ // )
+ $varsep_pattern = '/' . $markup['varsep'] . '\s*' . '(?=';
+ foreach( $variants as $variant ) {
+ $varsep_pattern .= $variant . '\s*' . $markup['codesep'] . '|'; // zh-hans:xxx;zh-hant:yyy
+ $varsep_pattern .= '[^;]*?' . $markup['unidsep'] . '\s*' . $variant
+ . '\s*' . $markup['codesep'] . '|'; // xxx=>zh-hans:yyy; xxx=>zh-hant:zzz
+ }
+ $varsep_pattern .= '\s*$)/';
+
+ $choice = preg_split($varsep_pattern, $rules);
- $choice = explode($markup['varsep'], $rules);
- foreach($choice as $c) {
+ foreach( $choice as $c ) {
$v = explode($markup['codesep'], $c, 2);
if( count($v) != 2 )
continue;// syntax error, skip
$to = trim($v[1]);
$v = trim($v[0]);
$u = explode($markup['unidsep'], $v);
- if( count($u) == 1 ) {
+ // if $to is empty, strtr() could return a wrong result
+ if( count($u) == 1 && $to && in_array( $v, $variants ) ) {
$bidtable[$v] = $to;
} else if(count($u) == 2){
$from = trim($u[0]);
$v = trim($u[1]);
- if( array_key_exists( $v, $unidtable ) && !is_array( $unidtable[$v] ) )
+ if( array_key_exists( $v, $unidtable ) && !is_array( $unidtable[$v] )
+ && $to && in_array( $v, $variants ) )
$unidtable[$v] = array( $from=>$to );
- else
+ elseif ( $to && in_array( $v, $variants ) )
$unidtable[$v][$from] = $to;
}
// syntax error, pass
$this->parseFlags();
$flags = $this->mFlags;
- //convert to specified variant
+ // convert to specified variant
+ // syntax: -{zh-hans;zh-hant[;...]|<text to convert>}-
if( count( array_diff( $flags, $variants ) ) == 0 and count( $flags ) != 0 ) {
- if ( in_array( $variant, $flags ) )
+ if ( in_array( $variant, $flags ) ) // check if current variant in flags
+ // then convert <text to convert> to current language
$this->mRules = $this->mConverter->autoConvert( $this->mRules, $variant );
- else {
+ else { // if current variant no in flags,
+ // then we check its fallback variants.
$variantFallbacks = $this->mConverter->getVariantFallbacks($variant);
foreach ( $variantFallbacks as $variantFallback ) {
+ // if current variant's fallback exist in flags
if ( in_array( $variantFallback, $flags ) ) {
+ // then convert <text to convert> to fallback language
$this->mRules = $this->mConverter->autoConvert( $this->mRules, $variantFallback );
break;
}
}
if( !in_array( 'R', $flags ) || !in_array( 'N', $flags ) ) {
- //FIXME: may cause trouble here...
- //strip since it interferes with the parsing, plus,
- //all spaces should be stripped in this tag anyway.
- $this->mRules = str_replace(' ', '', $this->mRules);
// decode => HTML entities modified by Sanitizer::removeHTMLtags
$this->mRules = str_replace('=>','=>',$this->mRules);