From d8375bee241b4d134aff901c7d072aa1374c860b Mon Sep 17 00:00:00 2001 From: Liangent Date: Sun, 28 Jul 2013 01:01:54 +0200 Subject: [PATCH] New language variant 'en-x-piglatin' for easier variant testing Guarded by the $wgUsePigLatinVariant variable, off by default. Pig Latin is a language game where words in English are altered according to the following rules: * Words starting with a vowel have a '-way' suffix appended. * Words starting with a consonant have the initial consonants (or 'qu' group) moved to the end and an '-ay' suffix appended. https://en.wikipedia.org/wiki/Pig_Latin * Added 'en-x-piglatin' as a language name. * Added 'en' to LanguageConverter::$languagesWithVariants. * Added LanguageEn class and its corresponding EnConverter which provides one-way translation from English to Pig Latin. * Some minor internal changes in code that assumed that English doesn't have a language class or converter. Bug: T45547 Depends-On: I1d9691c784032669979f8109c9a5f65cbf4122c9 Change-Id: I7fa2d85d6364958c5138366e8b4504a2697a8731 --- RELEASE-NOTES-1.30 | 10 +++ autoload.php | 2 + includes/DefaultSettings.php | 6 ++ languages/Language.php | 10 ++- languages/LanguageConverter.php | 1 + languages/classes/LanguageEn.php | 85 +++++++++++++++++++ languages/data/Names.php | 1 + .../phpunit/includes/auth/AuthManagerTest.php | 2 +- tests/phpunit/languages/LanguageTest.php | 5 +- 9 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 languages/classes/LanguageEn.php diff --git a/RELEASE-NOTES-1.30 b/RELEASE-NOTES-1.30 index f2cb871466..0f5cb47a7c 100644 --- a/RELEASE-NOTES-1.30 +++ b/RELEASE-NOTES-1.30 @@ -27,6 +27,7 @@ production. ParserOptions would pollute the parser cache. Callers should use WikiPage::makeParserOptions() to create the ParserOptions object and only change options that affect the parser cache key. +* (T45547) $wgUsePigLatinVariant added (off by default). === New features in 1.30 === * (T37247) Output from Parser::parse() will now be wrapped in a div with @@ -36,6 +37,10 @@ production. specific tags to be added by users. * Added a 'ParserOptionsRegister' hook to allow extensions to register additional parser options. +* (T45547) Included Pig Latin, a language game in English, as a + LanguageConverter variant. This allows English-speaking developers + to develop and test LanguageConverter more easily. Pig Latin can be + enabled by setting $wgUsePigLatinVariant to true. === Languages updated in 1.30 === @@ -76,6 +81,11 @@ changes to languages because of Phabricator reports. * … +==== Pig Latin added ==== +* (T45547) Added Pig Latin, a made-up English variant (en-x-piglatin), + for easier variant development and testing. Disabled by default. It can be + enabled by setting $wgUsePigLatinVariant to true. + === Other changes in 1.30 === * The use of an associative array for $wgProxyList, where the IP address is in the key instead of the value, is deprecated (e.g. [ '127.0.0.1' => 'value' ]). diff --git a/autoload.php b/autoload.php index 6c5aff5d3d..849df4de33 100644 --- a/autoload.php +++ b/autoload.php @@ -426,6 +426,7 @@ $wgAutoloadLocalClasses = [ 'EmailNotification' => __DIR__ . '/includes/mail/EmailNotification.php', 'EmaillingJob' => __DIR__ . '/includes/jobqueue/jobs/EmaillingJob.php', 'EmptyBagOStuff' => __DIR__ . '/includes/libs/objectcache/EmptyBagOStuff.php', + 'EnConverter' => __DIR__ . '/languages/classes/LanguageEn.php', 'EncryptedPassword' => __DIR__ . '/includes/password/EncryptedPassword.php', 'EnhancedChangesList' => __DIR__ . '/includes/changes/EnhancedChangesList.php', 'EnotifNotifyJob' => __DIR__ . '/includes/jobqueue/jobs/EnotifNotifyJob.php', @@ -701,6 +702,7 @@ $wgAutoloadLocalClasses = [ 'LanguageConverter' => __DIR__ . '/languages/LanguageConverter.php', 'LanguageCu' => __DIR__ . '/languages/classes/LanguageCu.php', 'LanguageDsb' => __DIR__ . '/languages/classes/LanguageDsb.php', + 'LanguageEn' => __DIR__ . '/languages/classes/LanguageEn.php', 'LanguageEs' => __DIR__ . '/languages/classes/LanguageEs.php', 'LanguageEt' => __DIR__ . '/languages/classes/LanguageEt.php', 'LanguageFi' => __DIR__ . '/languages/classes/LanguageFi.php', diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 9436aa66a8..920e2e8520 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -3056,6 +3056,12 @@ $wgDisableTitleConversion = false; */ $wgDefaultLanguageVariant = false; +/** + * Whether to enable the pig latin variant of English (en-x-piglatin), + * used to ease variant development work. + */ +$wgUsePigLatinVariant = false; + /** * Disabled variants array of language variant conversion. * diff --git a/languages/Language.php b/languages/Language.php index fb45cf7868..f84e21e53e 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -203,10 +203,11 @@ class Language { /** * Create a language object for a given language code * @param string $code + * @param $fallback boolean Whether we're going through language fallback chain * @throws MWException * @return Language */ - protected static function newFromCode( $code ) { + protected static function newFromCode( $code, $fallback = false ) { if ( !Language::isValidCode( $code ) ) { throw new MWException( "Invalid language code \"$code\"" ); } @@ -220,7 +221,7 @@ class Language { } // Check if there is a language class for the code - $class = self::classFromCode( $code ); + $class = self::classFromCode( $code, $fallback ); if ( class_exists( $class ) ) { $lang = new $class; return $lang; @@ -4338,10 +4339,11 @@ class Language { /** * @param string $code + * @param boolean $fallback Whether we're going through language fallback chain * @return string Name of the language class */ - public static function classFromCode( $code ) { - if ( $code == 'en' ) { + public static function classFromCode( $code, $fallback = true ) { + if ( $fallback && $code == 'en' ) { return 'Language'; } else { return 'Language' . str_replace( '-', '_', ucfirst( $code ) ); diff --git a/languages/LanguageConverter.php b/languages/LanguageConverter.php index 5382df4989..19d644c57e 100644 --- a/languages/LanguageConverter.php +++ b/languages/LanguageConverter.php @@ -36,6 +36,7 @@ class LanguageConverter { * @var array */ static public $languagesWithVariants = [ + 'en', 'gan', 'iu', 'kk', diff --git a/languages/classes/LanguageEn.php b/languages/classes/LanguageEn.php new file mode 100644 index 0000000000..dcb7a9194b --- /dev/null +++ b/languages/classes/LanguageEn.php @@ -0,0 +1,85 @@ +mTables = [ + 'en' => new ReplacementArray(), + 'en-x-piglatin' => new ReplacementArray(), + ]; + } + + /** + * Translates text into Pig Latin. This allows developers to test the language variants + * functionality and user interface without having to switch wiki language away from default. + * + * @param $text string + * @param $toVariant string + * @return string + */ + function translate( $text, $toVariant ) { + if ( $toVariant === 'en-x-piglatin' ) { + // Only process words composed of standard English alphabet, leave the rest unchanged. + // This skips some English words like 'naïve' or 'résumé', but we can live with that. + // Ignore single letters and words which aren't lowercase or uppercase-first. + return preg_replace_callback( '/[A-Za-z][a-z]+/', function ( $matches ) { + $word = $matches[0]; + if ( preg_match( '/^[aeiou]/i', $word ) ) { + return $word . 'way'; + } else { + return preg_replace_callback( '/^(qu|[^aeiou][^aeiouy]*)(.*)$/i', function ( $m ) { + $ucfirst = strtoupper( $m[1][0] ) === $m[1][0]; + if ( $ucfirst ) { + return ucfirst( $m[2] ) . lcfirst( $m[1] ) . 'ay'; + } else { + return $m[2] . $m[1] . 'ay'; + } + }, $word ); + } + }, $text ); + } else { + return $text; + } + } +} + +/** + * English + * + * @ingroup Language + */ +class LanguageEn extends Language { + function __construct() { + global $wgUsePigLatinVariant, $wgHooks; + + parent::__construct(); + + if ( $wgUsePigLatinVariant ) { + $this->mConverter = new EnConverter( $this, 'en', [ 'en', 'en-x-piglatin' ] ); + $wgHooks['PageContentSaveComplete'][] = $this->mConverter; + } + } +} diff --git a/languages/data/Names.php b/languages/data/Names.php index 76ced3e4a6..21479f1c20 100644 --- a/languages/data/Names.php +++ b/languages/data/Names.php @@ -146,6 +146,7 @@ class Names { 'en' => 'English', # English 'en-ca' => 'Canadian English', # Canadian English 'en-gb' => 'British English', # British English + 'en-x-piglatin' => 'Igpay Atinlay', # Pig Latin (for variant development) 'eo' => 'Esperanto', # Esperanto 'es' => 'español', # Spanish 'et' => 'eesti', # Estonian diff --git a/tests/phpunit/includes/auth/AuthManagerTest.php b/tests/phpunit/includes/auth/AuthManagerTest.php index 015fb3e633..a8405992fe 100644 --- a/tests/phpunit/includes/auth/AuthManagerTest.php +++ b/tests/phpunit/includes/auth/AuthManagerTest.php @@ -611,7 +611,7 @@ class AuthManagerTest extends \MediaWikiTestCase { $this->assertSame( 'de', $user->getOption( 'language' ) ); $this->assertSame( 'zh', $user->getOption( 'variant' ) ); - $this->setMwGlobals( 'wgContLang', \Language::factory( 'en' ) ); + $this->setMwGlobals( 'wgContLang', \Language::factory( 'fr' ) ); $user = \User::newFromName( self::usernameForCreation() ); $user->addToDatabase(); diff --git a/tests/phpunit/languages/LanguageTest.php b/tests/phpunit/languages/LanguageTest.php index 22fd7b8510..a474f20c6f 100644 --- a/tests/phpunit/languages/LanguageTest.php +++ b/tests/phpunit/languages/LanguageTest.php @@ -1739,9 +1739,8 @@ class LanguageTest extends LanguageClassesTestCase { [ 'zh', 'zh', 'zh is defined as the parent language of zh, ' . 'because zh converter can convert zh-cn to zh' ], [ 'zh-invalid', null, 'do not be fooled by arbitrarily composed language codes' ], - [ 'en-gb', null, 'en does not have converter' ], - [ 'en', null, 'en does not have converter. Although FakeConverter ' - . 'handles en -> en conversion but it is useless' ], + [ 'de-formal', null, 'de does not have converter' ], + [ 'de', null, 'de does not have converter' ], ]; } -- 2.20.1