Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / languages / classes / LanguageEn.php
1 <?php
2 /**
3 * English specific code.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * @ingroup Language
25 */
26 class EnConverter extends LanguageConverter {
27 /**
28 * Dummy methods required by base class.
29 */
30 function loadDefaultTables() {
31 $this->mTables = [
32 'en' => new ReplacementArray(),
33 'en-x-piglatin' => new ReplacementArray(),
34 ];
35 }
36
37 /**
38 * Translates text into Pig Latin. This allows developers to test the language variants
39 * functionality and user interface without having to switch wiki language away from default.
40 *
41 * @param string $text
42 * @param string $toVariant
43 * @return string
44 */
45 function translate( $text, $toVariant ) {
46 if ( $toVariant === 'en-x-piglatin' ) {
47 // Only process words composed of standard English alphabet, leave the rest unchanged.
48 // This skips some English words like 'naïve' or 'résumé', but we can live with that.
49 // Ignore single letters and words which aren't lowercase or uppercase-first.
50 return preg_replace_callback( '/[A-Za-z][a-z]+/', function ( $matches ) {
51 $word = $matches[0];
52 if ( preg_match( '/^[aeiou]/i', $word ) ) {
53 return $word . 'way';
54 } else {
55 return preg_replace_callback( '/^(qu|[^aeiou][^aeiouy]*)(.*)$/i', function ( $m ) {
56 $ucfirst = strtoupper( $m[1][0] ) === $m[1][0];
57 if ( $ucfirst ) {
58 return ucfirst( $m[2] ) . lcfirst( $m[1] ) . 'ay';
59 } else {
60 return $m[2] . $m[1] . 'ay';
61 }
62 }, $word );
63 }
64 }, $text );
65 } else {
66 return $text;
67 }
68 }
69 }
70
71 /**
72 * English
73 *
74 * @ingroup Language
75 */
76 class LanguageEn extends Language {
77 function __construct() {
78 global $wgUsePigLatinVariant, $wgHooks;
79
80 parent::__construct();
81
82 if ( $wgUsePigLatinVariant ) {
83 $this->mConverter = new EnConverter( $this, 'en', [ 'en', 'en-x-piglatin' ] );
84 $wgHooks['PageContentSaveComplete'][] = $this->mConverter;
85 }
86 }
87 }