MediaWiki  master
EnConverter.php
Go to the documentation of this file.
1 <?php
27 
28  public function __construct( $langobj ) {
29  parent::__construct( $langobj, 'en', [ 'en', 'en-x-piglatin' ] );
30  }
31 
35  protected function loadDefaultTables() {
36  $this->mTables = [
37  'en' => new ReplacementArray(),
38  'en-x-piglatin' => new ReplacementArray(),
39  ];
40  }
41 
50  public function translate( $text, $toVariant ) {
51  if ( $toVariant !== 'en-x-piglatin' ) {
52  return $text;
53  }
54 
55  // Only process words composed of standard English alphabet, leave the rest unchanged.
56  // This skips some English words like 'naïve' or 'résumé', but we can live with that.
57  // Ignore single letters and words which aren't lowercase or uppercase-first.
58  return preg_replace_callback( '/[A-Za-z][a-z\']+/', function ( $matches ) {
59  $word = $matches[0];
60  if ( preg_match( '/^[aeiou]/i', $word ) ) {
61  return $word . 'way';
62  }
63 
64  return preg_replace_callback( '/^(s?qu|[^aeiou][^aeiouy]*)(.*)$/i', function ( $m ) {
65  $ucfirst = strtoupper( $m[1][0] ) === $m[1][0];
66  if ( $ucfirst ) {
67  return ucfirst( $m[2] ) . lcfirst( $m[1] ) . 'ay';
68  }
69 
70  return $m[2] . $m[1] . 'ay';
71  }, $word );
72  }, $text );
73  }
74 }
EnConverter\__construct
__construct( $langobj)
Definition: EnConverter.php:28
$matches
$matches
Definition: NoLocalSettings.php:24
EnConverter\translate
translate( $text, $toVariant)
Translates text into Pig Latin.
Definition: EnConverter.php:50
EnConverter
Definition: EnConverter.php:26
ReplacementArray
Wrapper around strtr() that holds replacements.
Definition: ReplacementArray.php:24
EnConverter\loadDefaultTables
loadDefaultTables()
Dummy methods required by base class.
Definition: EnConverter.php:35
LanguageConverter
Base class for multi-variant language conversion.
Definition: LanguageConverter.php:35