Index: trunk/phase3/tests/LanguageConverterTest.php |
— | — | @@ -28,7 +28,7 @@ |
29 | 29 | |
30 | 30 | $wgRequest = new FauxRequest(array()); |
31 | 31 | $wgUser = new User; |
32 | | - $wgContLang = Language::factory( 'tg-latn' ); |
| 32 | + $wgContLang = Language::factory( 'tg' ); |
33 | 33 | |
34 | 34 | $this->assertEquals('tg', $this->lc->getPreferredVariant(false, false)); |
35 | 35 | $this->assertEquals('tg', $this->lc->getPreferredVariant(false, true)); |
— | — | @@ -41,7 +41,7 @@ |
42 | 42 | $this->assertEquals('tg', $this->lc->getPreferredVariant(false, false)); |
43 | 43 | $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(false, true)); |
44 | 44 | $this->assertEquals('tg', $this->lc->getPreferredVariant(true, false)); |
45 | | - $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true)); |
| 45 | + $this->assertEquals('tg', $this->lc->getPreferredVariant(true, true)); |
46 | 46 | |
47 | 47 | $wgRequest->setHeader('Accept-Language', 'tg;q=1'); |
48 | 48 | $this->lc = new TestConverter( $this->lang, 'tg', |
— | — | @@ -57,7 +57,7 @@ |
58 | 58 | $this->assertEquals('tg', $this->lc->getPreferredVariant(false, false)); |
59 | 59 | $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(false, true)); |
60 | 60 | $this->assertEquals('tg', $this->lc->getPreferredVariant(true, false)); |
61 | | - $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true)); |
| 61 | + $this->assertEquals('tg', $this->lc->getPreferredVariant(true, true)); |
62 | 62 | |
63 | 63 | $wgRequest->setHeader('Accept-Language', 'en, tg-latn;q=1'); |
64 | 64 | $this->lc = new TestConverter( $this->lang, 'tg', |
— | — | @@ -65,7 +65,7 @@ |
66 | 66 | $this->assertEquals('tg', $this->lc->getPreferredVariant(false, false)); |
67 | 67 | $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(false, true)); |
68 | 68 | $this->assertEquals('tg', $this->lc->getPreferredVariant(true, false)); |
69 | | - $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true)); |
| 69 | + $this->assertEquals('tg', $this->lc->getPreferredVariant(true, true)); |
70 | 70 | $wgRequest->setHeader('Accept-Language', ''); |
71 | 71 | |
72 | 72 | $wgUser = User::newFromId("admin"); |
— | — | @@ -95,7 +95,16 @@ |
96 | 96 | $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, false)); |
97 | 97 | $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true)); |
98 | 98 | |
| 99 | + $wgRequest->setVal('variant', null); |
| 100 | + $wgDefaultLanguageVariant = 'tg'; |
| 101 | + $this->lc = new TestConverter( $this->lang, 'tg', |
| 102 | + array( 'tg', 'tg-latn' ) ); |
| 103 | + $this->assertEquals('tg', $this->lc->getPreferredVariant(false, false)); |
| 104 | + $this->assertEquals('tg', $this->lc->getPreferredVariant(false, true)); |
| 105 | + $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, false)); |
| 106 | + $this->assertEquals('tg-latn', $this->lc->getPreferredVariant(true, true)); |
99 | 107 | |
| 108 | + |
100 | 109 | } |
101 | 110 | } |
102 | 111 | |
Index: trunk/phase3/languages/LanguageConverter.php |
— | — | @@ -142,62 +142,52 @@ |
143 | 143 | |
144 | 144 | // see if the preference is set in the request |
145 | 145 | $req = $wgRequest->getText( 'variant' ); |
146 | | - if ( in_array( $req, $this->mVariants ) ) { |
147 | | - $this->mPreferredVariant = $req; |
148 | | - return $this->mPreferredVariant; |
149 | | - } |
150 | 146 | |
151 | | - if ( $fromUser ) { |
152 | | - // bug 21974, don't return $this->mPreferredVariant if |
153 | | - // $fromUser = false |
154 | | - if ( $this->mPreferredVariant ) { |
155 | | - return $this->mPreferredVariant; |
156 | | - } |
157 | | - |
158 | | - // figure out user lang without constructing wgLang to avoid |
159 | | - // infinite recursion |
160 | | - $defaultUserLang = $wgUser->getOption( 'language' ); |
161 | | - |
162 | | - // get language variant preference from logged in users |
163 | | - // Don't call this on stub objects because that causes infinite |
164 | | - // recursion during initialisation |
165 | | - if ( $wgUser->isLoggedIn() ) { |
166 | | - $this->mPreferredVariant = $wgUser->getOption( 'variant' ); |
167 | | - } |
168 | | - |
169 | | - } else { |
170 | | - $defaultUserLang = $this->mMainLanguageCode; |
| 147 | + if ( !$req ) { |
| 148 | + $req = $wgRequest->getVal( 'uselang' ); |
171 | 149 | } |
172 | | - $userLang = $wgRequest->getVal( 'uselang', $defaultUserLang ); |
173 | 150 | |
174 | | - // see if interface language is same as content, if not, prevent |
175 | | - // conversion |
176 | | - if ( ! in_array( $userLang, $this->mVariants ) ) { |
177 | | - // no conversion |
178 | | - $this->mPreferredVariant = $this->mMainLanguageCode; |
179 | | - return $this->mPreferredVariant; |
180 | | - } elseif ( $this->mPreferredVariant ) { |
181 | | - // if the variant was set above and it iss a variant of |
182 | | - // the content language |
183 | | - return $this->mPreferredVariant; |
| 151 | + if ( $fromUser && !$req ) { |
| 152 | + $req = $this->getUserVariant(); |
184 | 153 | } |
185 | 154 | |
186 | | - // see if default variant is globaly set |
187 | | - if ( $wgDefaultLanguageVariant != false |
188 | | - && in_array( $wgDefaultLanguageVariant, $this->mVariants ) ) { |
189 | | - $this->mPreferredVariant = $wgDefaultLanguageVariant; |
190 | | - return $this->mPreferredVariant; |
| 155 | + if ( $fromHeader && !$req ) { |
| 156 | + $req = $this->getHeaderVariant(); |
191 | 157 | } |
192 | 158 | |
193 | | - $headerVariant = $this->getHeaderVariant(); |
194 | | - if ( $fromHeader && $headerVariant ) { |
195 | | - return $headerVariant; |
| 159 | + if ( $wgDefaultLanguageVariant && !$req ) { |
| 160 | + $req = $wgDefaultLanguageVariant; |
196 | 161 | } |
197 | 162 | |
| 163 | + if ( in_array( $req, $this->mVariants ) ) { |
| 164 | + return $req; |
| 165 | + } |
198 | 166 | return $this->mMainLanguageCode; |
199 | 167 | } |
200 | 168 | |
201 | 169 | /** |
| 170 | + * Determine the user has a variant set. |
| 171 | + * |
| 172 | + * @returns mixed variant if one found, false otherwise. |
| 173 | + */ |
| 174 | + function getUserVariant() { |
| 175 | + global $wgUser; |
| 176 | + |
| 177 | + // get language variant preference from logged in users |
| 178 | + // Don't call this on stub objects because that causes infinite |
| 179 | + // recursion during initialisation |
| 180 | + if ( $wgUser->isLoggedIn() ) { |
| 181 | + return $wgUser->getOption( 'variant' ); |
| 182 | + } |
| 183 | + else { |
| 184 | + // figure out user lang without constructing wgLang to avoid |
| 185 | + // infinite recursion |
| 186 | + return $wgUser->getOption( 'language' ); |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + |
| 191 | + /** |
202 | 192 | * Determine the language variant from the Accept-Language header. |
203 | 193 | * |
204 | 194 | * @returns mixed variant if one found, false otherwise. |
— | — | @@ -550,6 +540,7 @@ |
551 | 541 | if ( $wgDisableLangConversion ) return $text; |
552 | 542 | |
553 | 543 | $plang = $this->getPreferredVariant(); |
| 544 | + |
554 | 545 | $tarray = StringUtils::explode( $this->mMarkup['end'], $text ); |
555 | 546 | $converted = ''; |
556 | 547 | |