|
1
|
|
|
<?php |
|
2
|
|
|
namespace RazonYang\MediaWiki\ZhConverter; |
|
3
|
|
|
|
|
4
|
|
|
use MediaWiki\Logger\NullSpi; |
|
5
|
|
|
|
|
6
|
1 |
|
defined('MEDIAWIKI') || define('MEDIAWIKI', true); |
|
7
|
1 |
|
defined('MEDIAWIKI_PATH') || define('MEDIAWIKI_PATH', ComposerHelper::getVendorPath() . '/mediawiki/core'); |
|
8
|
|
|
|
|
9
|
1 |
|
require_once __DIR__ . '/functions.php'; |
|
10
|
1 |
|
require_once MEDIAWIKI_PATH . '/includes/GlobalFunctions.php'; |
|
11
|
1 |
|
require_once MEDIAWIKI_PATH . '/includes/AutoLoader.php'; |
|
12
|
|
|
|
|
13
|
1 |
|
set_include_path(get_include_path() . PATH_SEPARATOR . MEDIAWIKI_PATH); |
|
14
|
|
|
|
|
15
|
1 |
|
global $IP; |
|
16
|
1 |
|
$IP = MEDIAWIKI_PATH; |
|
17
|
|
|
|
|
18
|
|
|
class ZhConverter |
|
19
|
|
|
{ |
|
20
|
|
|
const ZH_CN = 'zh-cn'; // 中文简体 |
|
21
|
|
|
const ZH_TW = 'zh-tw'; // 台湾繁体 |
|
22
|
|
|
const ZH_HK = 'zh-hk'; // 香港繁体 |
|
23
|
|
|
const ZH_MO = 'zh-mo'; // 澳门繁体 |
|
24
|
|
|
|
|
25
|
|
|
private static $converter; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @return \LanguageConverter |
|
29
|
|
|
*/ |
|
30
|
10 |
|
public static function getConverter() |
|
31
|
|
|
{ |
|
32
|
10 |
|
if (static::$converter === null) { |
|
|
|
|
|
|
33
|
|
|
/* Initialize some global variables needed */ |
|
34
|
1 |
|
global $wgRequest, $wgMemc; |
|
35
|
1 |
|
global $wgLocalisationCacheConf, $wgDisabledVariants, $wgExtraLanguageNames; |
|
36
|
1 |
|
global $wgLangConvMemc, $wgMessageCacheType, $wgObjectCaches; |
|
37
|
1 |
|
global $wgLanguageConverterCacheType, $wgMWLoggerDefaultSpi, $wgMainWANCache, $wgWANObjectCaches; |
|
38
|
1 |
|
$wgRequest = new WebRequest(); |
|
39
|
1 |
|
$wgMemc = new FakeMemCachedClient(); |
|
40
|
1 |
|
$wgLocalisationCacheConf['class'] = FakeMemCachedClient::class; |
|
41
|
1 |
|
$wgLocalisationCacheConf['storeClass'] = 'LCStore_Null'; |
|
42
|
1 |
|
$wgDisabledVariants = []; |
|
43
|
1 |
|
$wgExtraLanguageNames = []; |
|
44
|
1 |
|
$wgLangConvMemc = new FakeMemCachedClient; |
|
45
|
1 |
|
defined('CACHE_NONE') || define('CACHE_NONE', 'FAKE'); |
|
46
|
|
|
$wgObjectCaches = [ |
|
47
|
1 |
|
'FAKE' => ['class' => FakeMemCachedClient::class], |
|
48
|
|
|
]; |
|
49
|
1 |
|
$wgMessageCacheType = 'FAKE'; |
|
50
|
1 |
|
$wgLanguageConverterCacheType = 'FAKE'; |
|
51
|
1 |
|
$wgMainWANCache = 'FAKE'; |
|
52
|
|
|
$wgWANObjectCaches = [ |
|
53
|
1 |
|
'FAKE' => [ |
|
54
|
|
|
'class' => 'WANObjectCache', |
|
55
|
|
|
'cacheId' => 'FAKE', |
|
56
|
|
|
'pool' => 'mediawiki-main-none', |
|
57
|
|
|
'relayerConfig' => ['class' => 'EventRelayerNull'] |
|
58
|
|
|
] |
|
59
|
|
|
]; |
|
60
|
1 |
|
$wgMWLoggerDefaultSpi = ['class' => NullSpi::class]; |
|
61
|
|
|
|
|
62
|
1 |
|
$lang = new \LanguageZh(); |
|
63
|
1 |
|
static::$converter = $lang->mConverter; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
10 |
|
return static::$converter; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Converts text to zh-*. |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $variant |
|
73
|
|
|
* @param string $text |
|
74
|
|
|
* |
|
75
|
|
|
* @return string |
|
76
|
|
|
*/ |
|
77
|
10 |
|
public static function to($variant, $text) |
|
78
|
|
|
{ |
|
79
|
10 |
|
return static::getConverter()->translate($text, $variant); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Converts text to zh-cn. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $text |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
2 |
|
public static function toCN($text) |
|
89
|
|
|
{ |
|
90
|
2 |
|
return self::to(self::ZH_CN, $text); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Converts text to zh-tw. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string $text |
|
97
|
|
|
* @return string |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public static function toTW($text) |
|
100
|
|
|
{ |
|
101
|
2 |
|
return self::to(self::ZH_TW, $text); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Converts text to zh-hk. |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $text |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
2 |
|
public static function toHK($text) |
|
111
|
|
|
{ |
|
112
|
2 |
|
return self::to(self::ZH_HK, $text); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Converts text to zh-mo. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $text |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
2 |
|
public static function toMO($text) |
|
122
|
|
|
{ |
|
123
|
2 |
|
return self::to(self::ZH_MO, $text); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|