Follow up r80376. Added missing file FORMAT.
[lhc/web/wiklou.git] / languages / classes / LanguageZh.php
1 <?php
2
3 require_once( dirname( __FILE__ ) . '/../LanguageConverter.php' );
4 require_once( dirname( __FILE__ ) . '/LanguageZh_hans.php' );
5
6 /**
7 * @ingroup Language
8 */
9 class ZhConverter extends LanguageConverter {
10
11 function __construct( $langobj, $maincode,
12 $variants = array(),
13 $variantfallbacks = array(),
14 $flags = array(),
15 $manualLevel = array() ) {
16 $this->mDescCodeSep = ':';
17 $this->mDescVarSep = ';';
18 parent::__construct( $langobj, $maincode,
19 $variants,
20 $variantfallbacks,
21 $flags,
22 $manualLevel );
23 $names = array(
24 'zh' => '原文',
25 'zh-hans' => '简体',
26 'zh-hant' => '繁體',
27 'zh-cn' => '大陆',
28 'zh-tw' => '台灣',
29 'zh-hk' => '香港',
30 'zh-mo' => '澳門',
31 'zh-sg' => '新加坡',
32 'zh-my' => '大马',
33 );
34 $this->mVariantNames = array_merge( $this->mVariantNames, $names );
35 }
36
37 function loadDefaultTables() {
38 require( dirname( __FILE__ ) . "/../../includes/ZhConversion.php" );
39 $this->mTables = array(
40 'zh-hans' => new ReplacementArray( $zh2Hans ),
41 'zh-hant' => new ReplacementArray( $zh2Hant ),
42 'zh-cn' => new ReplacementArray( array_merge( $zh2Hans, $zh2CN ) ),
43 'zh-hk' => new ReplacementArray( array_merge( $zh2Hant, $zh2HK ) ),
44 'zh-mo' => new ReplacementArray( array_merge( $zh2Hant, $zh2HK ) ),
45 'zh-my' => new ReplacementArray( array_merge( $zh2Hans, $zh2SG ) ),
46 'zh-sg' => new ReplacementArray( array_merge( $zh2Hans, $zh2SG ) ),
47 'zh-tw' => new ReplacementArray( array_merge( $zh2Hant, $zh2TW ) ),
48 'zh' => new ReplacementArray
49 );
50 }
51
52 function postLoadTables() {
53 $this->mTables['zh-cn']->merge( $this->mTables['zh-hans'] );
54 $this->mTables['zh-hk']->merge( $this->mTables['zh-hant'] );
55 $this->mTables['zh-mo']->merge( $this->mTables['zh-hant'] );
56 $this->mTables['zh-my']->merge( $this->mTables['zh-hans'] );
57 $this->mTables['zh-sg']->merge( $this->mTables['zh-hans'] );
58 $this->mTables['zh-tw']->merge( $this->mTables['zh-hant'] );
59 }
60
61 /* there shouldn't be any latin text in Chinese conversion, so no need
62 to mark anything.
63 $noParse is there for compatibility with LanguageConvert::markNoConversion
64 */
65 function markNoConversion( $text, $noParse = false ) {
66 return $text;
67 }
68
69 function convertCategoryKey( $key ) {
70 return $this->autoConvert( $key, 'zh' );
71 }
72 }
73
74 /**
75 * class that handles both Traditional and Simplified Chinese
76 * right now it only distinguish zh_hans, zh_hant, zh_cn, zh_tw, zh_sg and zh_hk.
77 *
78 * @ingroup Language
79 */
80 class LanguageZh extends LanguageZh_hans {
81
82 function __construct() {
83 global $wgHooks;
84 parent::__construct();
85
86 $variants = array( 'zh', 'zh-hans', 'zh-hant', 'zh-cn', 'zh-hk', 'zh-mo', 'zh-my', 'zh-sg', 'zh-tw' );
87
88 $variantfallbacks = array(
89 'zh' => array( 'zh-hans', 'zh-hant', 'zh-cn', 'zh-tw', 'zh-hk', 'zh-sg', 'zh-mo', 'zh-my' ),
90 'zh-hans' => array( 'zh-cn', 'zh-sg', 'zh-my' ),
91 'zh-hant' => array( 'zh-tw', 'zh-hk', 'zh-mo' ),
92 'zh-cn' => array( 'zh-hans', 'zh-sg', 'zh-my' ),
93 'zh-sg' => array( 'zh-hans', 'zh-cn', 'zh-my' ),
94 'zh-my' => array( 'zh-hans', 'zh-sg', 'zh-cn' ),
95 'zh-tw' => array( 'zh-hant', 'zh-hk', 'zh-mo' ),
96 'zh-hk' => array( 'zh-hant', 'zh-mo', 'zh-tw' ),
97 'zh-mo' => array( 'zh-hant', 'zh-hk', 'zh-tw' ),
98 );
99 $ml = array(
100 'zh' => 'disable',
101 'zh-hans' => 'unidirectional',
102 'zh-hant' => 'unidirectional',
103 );
104
105 $this->mConverter = new ZhConverter( $this, 'zh',
106 $variants, $variantfallbacks,
107 array(),
108 $ml );
109
110 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
111 }
112
113 # this should give much better diff info
114 function segmentForDiff( $text ) {
115 return preg_replace(
116 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
117 "' ' .\"$1\"", $text );
118 }
119
120 function unsegmentForDiff( $text ) {
121 return preg_replace(
122 "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
123 "\"$1\"", $text );
124 }
125
126 /**
127 * auto convert to zh-hans and normalize special characters.
128 *
129 * @param $string String
130 * @param $autoVariant String, default to 'zh-hans'
131 * @return String
132 */
133 function normalizeForSearch( $string, $autoVariant = 'zh-hans' ) {
134 wfProfileIn( __METHOD__ );
135
136 // always convert to zh-hans before indexing. it should be
137 // better to use zh-hans for search, since conversion from
138 // Traditional to Simplified is less ambiguous than the
139 // other way around
140 $s = $this->mConverter->autoConvert( $string, $autoVariant );
141 // LanguageZh_hans::normalizeForSearch
142 $s = parent::normalizeForSearch( $s );
143 wfProfileOut( __METHOD__ );
144 return $s;
145
146 }
147
148 function convertForSearchResult( $termsArray ) {
149 $terms = implode( '|', $termsArray );
150 $terms = self::convertDoubleWidth( $terms );
151 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
152 $ret = array_unique( explode( '|', $terms ) );
153 return $ret;
154 }
155 }
156