LanguageEn.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. * @ingroup Language
  24. */
  25. class EnConverter extends LanguageConverter {
  26. /**
  27. * Dummy methods required by base class.
  28. */
  29. function loadDefaultTables() {
  30. $this->mTables = [
  31. 'en' => new ReplacementArray(),
  32. 'en-x-piglatin' => new ReplacementArray(),
  33. ];
  34. }
  35. /**
  36. * Translates text into Pig Latin. This allows developers to test the language variants
  37. * functionality and user interface without having to switch wiki language away from default.
  38. *
  39. * @param string $text
  40. * @param string $toVariant
  41. * @return string
  42. */
  43. function translate( $text, $toVariant ) {
  44. if ( $toVariant === 'en-x-piglatin' ) {
  45. // Only process words composed of standard English alphabet, leave the rest unchanged.
  46. // This skips some English words like 'naïve' or 'résumé', but we can live with that.
  47. // Ignore single letters and words which aren't lowercase or uppercase-first.
  48. return preg_replace_callback( '/[A-Za-z][a-z\']+/', function ( $matches ) {
  49. $word = $matches[0];
  50. if ( preg_match( '/^[aeiou]/i', $word ) ) {
  51. return $word . 'way';
  52. } else {
  53. return preg_replace_callback( '/^(s?qu|[^aeiou][^aeiouy]*)(.*)$/i', function ( $m ) {
  54. $ucfirst = strtoupper( $m[1][0] ) === $m[1][0];
  55. if ( $ucfirst ) {
  56. return ucfirst( $m[2] ) . lcfirst( $m[1] ) . 'ay';
  57. } else {
  58. return $m[2] . $m[1] . 'ay';
  59. }
  60. }, $word );
  61. }
  62. }, $text );
  63. } else {
  64. return $text;
  65. }
  66. }
  67. }
  68. /**
  69. * English
  70. *
  71. * @ingroup Language
  72. */
  73. class LanguageEn extends Language {
  74. function __construct() {
  75. global $wgUsePigLatinVariant, $wgHooks;
  76. parent::__construct();
  77. if ( $wgUsePigLatinVariant ) {
  78. $this->mConverter = new EnConverter( $this, 'en', [ 'en', 'en-x-piglatin' ] );
  79. $wgHooks['PageContentSaveComplete'][] = $this->mConverter;
  80. }
  81. }
  82. }