Index: trunk/extensions/LanguageSelector/StubAutoLang.php |
— | — | @@ -1,32 +0,0 @@ |
2 | | -<?php |
3 | | - |
4 | | -class StubAutoLang extends StubObject { |
5 | | - protected $requestedLanguage; |
6 | | - |
7 | | - function __construct() { |
8 | | - parent::__construct( 'wgLang' ); |
9 | | - } |
10 | | - |
11 | | - function __call( $name, $args ) { |
12 | | - return $this->_call( $name, $args ); |
13 | | - } |
14 | | - |
15 | | - function setRequestedLanguage( $code ) { |
16 | | - $this->requestedLanguage = $code; |
17 | | - } |
18 | | - |
19 | | - //partially copied from StubObject.php. There should be a better way... |
20 | | - function _newObject() { |
21 | | - global $wgContLanguageCode, $wgContLang, $wgLanguageSelectorDetectLanguage; |
22 | | - |
23 | | - $code = $this->requestedLanguage; |
24 | | - if (!$code) $code = wfLanguageSelectorDetectLanguage( $wgLanguageSelectorDetectLanguage ); |
25 | | - |
26 | | - if( $code == $wgContLanguageCode ) { |
27 | | - return $wgContLang; |
28 | | - } else { |
29 | | - $obj = Language::factory( $code ); |
30 | | - return $obj; |
31 | | - } |
32 | | - } |
33 | | -} |
\ No newline at end of file |
Index: trunk/extensions/LanguageSelector/LanguageSelector.js |
— | — | @@ -1,4 +1,4 @@ |
2 | | -addOnloadHook(function() { |
| 2 | +$( function() { |
3 | 3 | var i = 1; |
4 | 4 | while ( true ) { |
5 | 5 | var btn = document.getElementById("languageselector-commit-"+i); |
— | — | @@ -21,4 +21,4 @@ |
22 | 22 | |
23 | 23 | i++; |
24 | 24 | } |
25 | | -}); |
\ No newline at end of file |
| 25 | +}); |
Index: trunk/extensions/LanguageSelector/LanguageSelector.php |
— | — | @@ -74,14 +74,19 @@ |
75 | 75 | $wgHooks['BeforePageDisplay'][] = 'wfLanguageSelectorBeforePageDisplay'; |
76 | 76 | $wgHooks['GetCacheVaryCookies'][] = 'wfLanguageSelectorGetCacheVaryCookies'; |
77 | 77 | $wgHooks['ParserFirstCallInit'][] = 'wfLanguageSelectorSetHook'; |
| 78 | +$wgHooks['UserGetLanguageObject'][] = 'wfLanguageSelectorGetLanguageCode'; |
78 | 79 | |
79 | 80 | $wgExtensionFunctions[] = 'wfLanguageSelectorExtension'; |
80 | 81 | |
81 | 82 | $wgParserOutputHooks['languageselector'] = 'wfLanguageSelectorAddJavascript'; |
82 | 83 | |
83 | | -$wgLanguageSelectorRequestedLanguage = null; |
84 | | -$wgLanguageSelectorFormUsed = false; |
| 84 | +# Ressource loader |
| 85 | +$wgResourceModules['ext.languageSelector'] = array( |
| 86 | + 'scripts' => 'LanguageSelector.js', |
85 | 87 | |
| 88 | + 'localBasePath' => dirname( __FILE__ ), |
| 89 | + 'remoteExtPath' => 'LanguageSelector' |
| 90 | +); |
86 | 91 | |
87 | 92 | $dir = dirname(__FILE__) . '/'; |
88 | 93 | $wgExtensionMessagesFiles['LanguageSelector'] = $dir . 'LanguageSelector.i18n.php'; |
— | — | @@ -94,74 +99,95 @@ |
95 | 100 | } |
96 | 101 | |
97 | 102 | function wfLanguageSelectorExtension() { |
98 | | - global $wgLanguageSelectorLanguages, $wgLanguageSelectorDetectLanguage, |
99 | | - $wgLanguageSelectorRequestedLanguage, $wgLanguageSelectorLocation, |
100 | | - $wgLanguageSelectorShowAll, $wgCommandLineMode; |
101 | | - global $wgUser, $wgLang, $wgRequest, $wgCookiePrefix, $wgCookiePath, $wgHooks; |
| 103 | + global $wgLanguageSelectorLanguages, $wgLanguageSelectorLocation, |
| 104 | + $wgLanguageSelectorShowAll, $wgHooks; |
102 | 105 | |
103 | 106 | if ( $wgLanguageSelectorLanguages === null ) { |
104 | 107 | $wgLanguageSelectorLanguages = array_keys( Language::getLanguageNames( !$wgLanguageSelectorShowAll ) ); |
105 | 108 | sort( $wgLanguageSelectorLanguages ); |
106 | 109 | } |
107 | 110 | |
| 111 | + if ( $wgLanguageSelectorLocation != LANGUAGE_SELECTOR_MANUAL && $wgLanguageSelectorLocation != LANGUAGE_SELECTOR_AT_TOP_OF_TEXT ) { |
| 112 | + switch ( $wgLanguageSelectorLocation ) { |
| 113 | + case LANGUAGE_SELECTOR_IN_TOOLBOX: |
| 114 | + $wgHooks['SkinTemplateToolboxEnd'][] = 'wfLanguageSelectorSkinHook'; |
| 115 | + break; |
| 116 | + default: |
| 117 | + $wgHooks['SkinTemplateOutputPageBeforeExec'][] = 'wfLanguageSelectorSkinTemplateOutputPageBeforeExec'; |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +function wfLanguageSelectorGetLanguageCode( $user, &$code ) { |
| 124 | + global $wgLanguageSelectorLanguages, $wgLanguageSelectorDetectLanguage, |
| 125 | + $wgCommandLineMode, $wgRequest, $wgContLang; |
| 126 | + |
108 | 127 | if ( $wgCommandLineMode ) { |
109 | 128 | return true; |
110 | 129 | } |
111 | 130 | |
112 | 131 | $setlang = $wgRequest->getVal( 'setlang' ); |
113 | | - if ( $setlang && !in_array( $setlang, $wgLanguageSelectorLanguages ) ) |
114 | | - $setlang = null; //ignore invalid |
| 132 | + if ( $setlang && !in_array( $setlang, $wgLanguageSelectorLanguages ) ) { |
| 133 | + $setlang = null; // ignore invalid |
| 134 | + } |
115 | 135 | |
116 | 136 | if ( $setlang ) { |
117 | | - setcookie( $wgCookiePrefix . 'LanguageSelectorLanguage', $setlang, 0, $wgCookiePath ); |
118 | | - $wgLanguageSelectorRequestedLanguage = $setlang; |
| 137 | + $wgRequest->response()->setcookie( 'LanguageSelectorLanguage', $setlang ); |
| 138 | + $requestedLanguage = $setlang; |
119 | 139 | } else { |
120 | | - $wgLanguageSelectorRequestedLanguage = @$_COOKIE[$wgCookiePrefix.'LanguageSelectorLanguage']; |
| 140 | + $requestedLanguage = $wgRequest->getCookie( 'LanguageSelectorLanguage' ); |
121 | 141 | } |
122 | 142 | |
123 | | - if ( $setlang && !$wgUser->isAnon() ) { |
124 | | - if ( $setlang != $wgUser->getOption( 'language' ) ) { |
125 | | - $wgUser->setOption( 'language', $wgLanguageSelectorRequestedLanguage ); |
126 | | - $wgUser->saveSettings(); |
127 | | - // Reset $wgLang so that user's language is immediately changed |
128 | | - $wgLang = new StubUserLang; |
| 143 | + if ( $setlang && !$user->isAnon() ) { |
| 144 | + if ( $setlang != $user->getOption( 'language' ) ) { |
| 145 | + $user->setOption( 'language', $requestedLanguage ); |
| 146 | + $user->saveSettings(); |
| 147 | + $code = $requestedLanguage; |
129 | 148 | } |
130 | 149 | } |
131 | 150 | |
132 | | - if ( !$wgRequest->getVal( 'uselang' ) && $wgUser->isAnon() ) { |
133 | | - if ( $wgLanguageSelectorRequestedLanguage || $wgLanguageSelectorDetectLanguage != LANGUAGE_SELECTOR_USE_CONTENT_LANG ) { |
134 | | - $wgLang = new StubAutoLang; |
135 | | - $wgLang->setRequestedLanguage( $wgLanguageSelectorRequestedLanguage ); |
136 | | - } |
137 | | - } |
| 151 | + if ( !$wgRequest->getVal( 'uselang' ) && $user->isAnon() ) { |
| 152 | + if ( $wgLanguageSelectorDetectLanguage != LANGUAGE_SELECTOR_USE_CONTENT_LANG ) { |
| 153 | + if ( $requestedLanguage ) { |
| 154 | + $code = $requestedLanguage; |
| 155 | + } else { |
| 156 | + $languages = $wgRequest->getAcceptLang(); |
138 | 157 | |
139 | | - if ( $wgLanguageSelectorLocation != LANGUAGE_SELECTOR_MANUAL && $wgLanguageSelectorLocation != LANGUAGE_SELECTOR_AT_TOP_OF_TEXT ) { |
140 | | - switch ( $wgLanguageSelectorLocation ) { |
141 | | - case LANGUAGE_SELECTOR_IN_TOOLBOX: |
142 | | - $wgHooks['SkinTemplateToolboxEnd'][] = 'wfLanguageSelectorSkinHook'; |
143 | | - break; |
144 | | - default: |
145 | | - $wgHooks['SkinTemplateOutputPageBeforeExec'][] = 'wfLanguageSelectorSkinTemplateOutputPageBeforeExec'; |
146 | | - break; |
| 158 | + // see if the content language is accepted by the client. |
| 159 | + if ( $wgLanguageSelectorDetectLanguage != LANGUAGE_SELECTOR_PREFER_CONTENT_LANG |
| 160 | + || !array_key_exists( $wgContLang->getCode(), $languages ) ) |
| 161 | + { |
| 162 | + |
| 163 | + // look for a language that is acceptable to the client |
| 164 | + // and known to the wiki. |
| 165 | + foreach( $languages as $reqCode => $q ) { |
| 166 | + if ( in_array( $reqCode, $wgLanguageSelectorLanguages ) ) { |
| 167 | + $code = $reqCode; |
| 168 | + break; |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + } |
147 | 173 | } |
148 | 174 | } |
149 | 175 | |
| 176 | + return true; |
150 | 177 | } |
151 | 178 | |
152 | 179 | function wfLanguageSelectorBeforePageDisplay( &$out ) { |
153 | | - global $wgExtensionAssetsPath, $wgLanguageSelectorLocation, $wgLanguageSelectorFormUsed; |
| 180 | + global $wgLanguageSelectorLocation; |
154 | 181 | |
| 182 | + if ( $wgLanguageSelectorLocation == LANGUAGE_SELECTOR_MANUAL ) { |
| 183 | + return true; |
| 184 | + } |
| 185 | + |
155 | 186 | if ( $wgLanguageSelectorLocation == LANGUAGE_SELECTOR_AT_TOP_OF_TEXT ) { |
156 | 187 | $html = wfLanguageSelectorHTML( $out->getTitle() ); |
157 | 188 | $out->mBodytext = $html . $out->mBodytext; |
158 | 189 | } |
159 | 190 | |
160 | | - if ( $wgLanguageSelectorFormUsed || |
161 | | - $wgLanguageSelectorLocation != LANGUAGE_SELECTOR_MANUAL ) |
162 | | - { |
163 | | - $out->addScriptFile( $wgExtensionAssetsPath . |
164 | | - '/LanguageSelector/LanguageSelector.js' ); |
165 | | - } |
| 191 | + $out->addModules( 'ext.languageSelector' ); |
166 | 192 | |
167 | 193 | return true; |
168 | 194 | } |
— | — | @@ -254,76 +280,6 @@ |
255 | 281 | return true; |
256 | 282 | } |
257 | 283 | |
258 | | -function wfLanguageSelectorDetectLanguage( $mode ) { |
259 | | - global $wgContLang, $wgLanguageSelectorLanguages; |
260 | | - |
261 | | - $contLang = $wgContLang->getCode(); |
262 | | - |
263 | | - if ( !$mode || $mode == LANGUAGE_SELECTOR_USE_CONTENT_LANG ) { |
264 | | - return $contLang; |
265 | | - } |
266 | | - |
267 | | - /** |
268 | | - * get accepted languages from Accept-Languages |
269 | | - * HTTP header. |
270 | | - */ |
271 | | - $accept = @$_SERVER["HTTP_ACCEPT_LANGUAGE"]; |
272 | | - |
273 | | - if ( empty( $accept ) ) |
274 | | - return $contLang; |
275 | | - |
276 | | - $accept = explode( ',', $accept ); |
277 | | - |
278 | | - /** |
279 | | - * normalize accepted languages |
280 | | - */ |
281 | | - $languages = array(); |
282 | | - foreach ( $accept as $lan ) { |
283 | | - @list( $value, $qpart ) = explode( ';', trim( $lan ) ); |
284 | | - $match = array(); |
285 | | - if( !isset( $qpart ) ) { |
286 | | - $languages[$value] = 1.0; |
287 | | - } elseif( preg_match( '/q\s*=\s*(\d*\.\d+)/', $qpart, $match ) ) { |
288 | | - $languages[$value] = floatval( $match[1] ); |
289 | | - } |
290 | | - } |
291 | | - |
292 | | - /** |
293 | | - * see if the content language is accepted by the |
294 | | - * client. |
295 | | - */ |
296 | | - if ( $mode == LANGUAGE_SELECTOR_PREFER_CONTENT_LANG && array_key_exists( $contLang, $languages ) ) { |
297 | | - return $contLang; |
298 | | - } |
299 | | - |
300 | | - arsort( $languages, SORT_NUMERIC ); |
301 | | - |
302 | | - /** |
303 | | - * look for a language that is acceptable to the client |
304 | | - * and known to the wiki. |
305 | | - */ |
306 | | - foreach( $languages as $code => $q ) { |
307 | | - /** |
308 | | - * TODO: only accept languages for which an implementation exists. |
309 | | - * this is disabled, because it's slow. Note that this code is |
310 | | - * executed for every page request! |
311 | | - */ |
312 | | - /* |
313 | | - global $IP; |
314 | | - $langfile="$IP/languages/Language".str_replace('-', '_', ucfirst($code)).".php"; |
315 | | - if(!file_exists($langfile)) { |
316 | | - continue; |
317 | | - } |
318 | | - */ |
319 | | - |
320 | | - if ( in_array( $code, $wgLanguageSelectorLanguages ) ) { |
321 | | - return $code; |
322 | | - } |
323 | | - } |
324 | | - |
325 | | - return $contLang; |
326 | | -} |
327 | | - |
328 | 284 | function wfLanguageSelectorAddNewAccount( $u ) { |
329 | 285 | global $wgUser, $wgLang; |
330 | 286 | |
— | — | @@ -339,19 +295,16 @@ |
340 | 296 | } |
341 | 297 | |
342 | 298 | function wfLanguageSelectorAddJavascript( $outputPage, $parserOutput, $data ) { |
343 | | - global $wgLanguageSelectorFormUsed; |
344 | | - |
345 | | - $wgLanguageSelectorFormUsed = true; |
| 299 | + $outputPage->addModules( 'ext.languageSelector' ); |
346 | 300 | } |
347 | 301 | |
348 | 302 | function wfLanguageSelectorHTML( Title $title, $style = null, $class = null, $selectorstyle = null, $buttonstyle = null, $showCode = null ) { |
349 | 303 | global $wgLanguageSelectorLanguages, $wgLang, $wgContLang, $wgScript, |
350 | | - $wgLanguageSelectorShowCode, $wgLanguageSelectorFormUsed; |
| 304 | + $wgLanguageSelectorShowCode; |
351 | 305 | |
352 | 306 | if ( $showCode === null ) { |
353 | 307 | $showCode = $wgLanguageSelectorShowCode; |
354 | 308 | } |
355 | | - $wgLanguageSelectorFormUsed = true; |
356 | 309 | |
357 | 310 | static $id = 0; |
358 | 311 | $id += 1; |