transliterate example

// Here we rely on intl to validate the $string, $start and $end arguments             // and to slice the string. Slicing is done by replacing the part if $string             // between $start and $end by a unique cookie that can be reliably used to             // identify which part of $string should be transliterated.
            static $cookie;
            static $transliterator;

            $cookie ??= hash('xxh128', random_bytes(8));
            $this->transliterator ??= clone $transliterator ??= \Transliterator::createFromRules('[:any:]* > '.$cookie);

            if (false === $result = $this->transliterator->transliterate($string$start$end)) {
                return false;
            }

            $parts = explode($cookie$result);
            $start = \strlen($parts[0]);
            $length = -\strlen($parts[1]) ?: null;
            $string = substr($string$start$length);

            return $parts[0].(\strlen($string) === strcspn($string$quickCheck) ? $string : strtr($string$this->map)).$parts[1];
        }
    }
}

  public function testPhpTransliteration($langcode$original$expected$unknown_character = '?', $max_length = NULL) {
    $transliterator_class = new PhpTransliteration();
    $actual = $transliterator_class->transliterate($original$langcode$unknown_character$max_length);
    $this->assertSame($expected$actual);
  }

  /** * Provides data for self::testPhpTransliteration(). * * @return array * An array of arrays, each containing the parameters for * self::testPhpTransliteration(). */
  public function providerTestPhpTransliteration() {
    

class EmojiTransliteratorTest extends TestCase
{
    /** * @dataProvider provideTransliterateTests */
    public function testTransliterate(string $locale, string $input, string $expected)
    {
        $tr = EmojiTransliterator::create('emoji-'.$locale);

        $this->assertSame($expected$tr->transliterate($input));
    }

    public static function provideTransliterateTests(): iterable
    {
        yield [
            'fr',
            'un 😺, 🐈‍⬛, et a 🦁 vont au 🏞️',
            'un chat qui sourit, chat noir, et a tête de lion vont au parc national️',
        ];
        yield [
            'en',
            
while (\strlen($s) - 1 > $i = strspn($s, self::ASCII)) {
            if (0 < --$i) {
                $str->string .= substr($s, 0, $i);
                $s = substr($s$i);
            }

            if (!$rule = array_shift($rules)) {
                $rules = []; // An empty rule interrupts the next ones             }

            if ($rule instanceof \Transliterator) {
                $s = $rule->transliterate($s);
            } elseif ($rule instanceof \Closure) {
                $s = $rule($s);
            } elseif ($rule) {
                if ('nfd' === $rule = strtolower($rule)) {
                    normalizer_is_normalized($s, self::NFD) ?: $s = normalizer_normalize($s, self::NFD);
                } elseif ('nfkd' === $rule) {
                    normalizer_is_normalized($s, self::NFKD) ?: $s = normalizer_normalize($s, self::NFKD);
                } elseif ('[:nonspacing mark:] remove' === $rule) {
                    $s = preg_replace('/\p{Mn}++/u', '', $s);
                } elseif ('latin-ascii' === $rule) {
                    $s = str_replace(self::TRANSLIT_FROM, self::TRANSLIT_TO, $s);
                }
$configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('transliteration')
    );
  }

  /** * {@inheritdoc} */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row$destination_property) {
    $new_value = $this->transliteration->transliterate($value, LanguageInterface::LANGCODE_DEFAULT, '_');
    $new_value = strtolower($new_value);
    $new_value = preg_replace($this->replacePattern, '_', $new_value);
    return preg_replace('/_+/', '_', $new_value);
  }

}
while (\strlen($s) - 1 > $i = strspn($s, self::ASCII)) {
            if (0 < --$i) {
                $str->string .= substr($s, 0, $i);
                $s = substr($s$i);
            }

            if (!$rule = array_shift($rules)) {
                $rules = []; // An empty rule interrupts the next ones             }

            if ($rule instanceof \Transliterator) {
                $s = $rule->transliterate($s);
            } elseif ($rule instanceof \Closure) {
                $s = $rule($s);
            } elseif ($rule) {
                if ('nfd' === $rule = strtolower($rule)) {
                    normalizer_is_normalized($s, self::NFD) ?: $s = normalizer_normalize($s, self::NFD);
                } elseif ('nfkd' === $rule) {
                    normalizer_is_normalized($s, self::NFKD) ?: $s = normalizer_normalize($s, self::NFKD);
                } elseif ('[:nonspacing mark:] remove' === $rule) {
                    $s = preg_replace('/\p{Mn}++/u', '', $s);
                } elseif ('latin-ascii' === $rule) {
                    $s = str_replace(self::TRANSLIT_FROM, self::TRANSLIT_TO, $s);
                }
if ($langcode == 'zz') {
          // The default transliteration of Ä is A, but change it to Z for testing.           $overrides[0xC4] = 'Z';
          // Also provide transliterations of two 5-byte characters from           // http://wikipedia.org/wiki/Gothic_alphabet.           $overrides[0x10330] = 'A';
          $overrides[0x10338] = 'Th';
        }
      });
    $transliteration = new PhpTransliteration(NULL, $module_handler);

    $actual = $transliteration->transliterate($original$langcode);
    $this->assertSame($expected$actual, "'$printable' transliteration to '$actual' is identical to '$expected' for language '$langcode' in service instance.");
  }

  /** * Provides test data for testPhpTransliterationWithAlter. * * @return array */
  public function providerTestPhpTransliterationWithAlter() {
    $random_generator = new Random();
    $random = $random_generator->string(10);
    

  public function testMachineNameController(array $request_params$expected_content) {
    $request = Request::create('', 'GET', $request_params);
    $json = $this->machineNameController->transliterate($request);
    $this->assertEquals($expected_content$json->getContent());
  }

  /** * Tests the pattern validation. */
  public function testMachineNameControllerWithInvalidReplacePattern() {
    $request = Request::create('', 'GET', ['text' => 'Bob', 'langcode' => 'en', 'replace' => 'Alice', 'replace_pattern' => 'Bob', 'replace_token' => 'invalid']);

    $this->expectException(AccessDeniedHttpException::class);
    $this->expectExceptionMessage("Invalid 'replace_token' query parameter.");
    

  public function transliterate(Request $request) {
    $text = $request->query->get('text');
    $langcode = $request->query->get('langcode');
    $replace_pattern = $request->query->get('replace_pattern');
    $replace_token = $request->query->get('replace_token');
    $replace = $request->query->get('replace');
    $lowercase = $request->query->get('lowercase');

    $transliterated = $this->transliteration->transliterate($text$langcode, '_');
    if ($lowercase) {
      $transliterated = mb_strtolower($transliterated);
    }

    if (isset($replace_pattern) && isset($replace)) {
      if (!isset($replace_token)) {
        throw new AccessDeniedHttpException("Missing 'replace_token' query parameter.");
      }
      elseif (!$this->tokenGenerator->validate($replace_token$replace_pattern)) {
        throw new AccessDeniedHttpException("Invalid 'replace_token' query parameter.");
      }

      
/** * {@inheritdoc} */
  public function getMachineNameSuggestion() {
    $definition = $this->getPluginDefinition();
    $admin_label = $definition['admin_label'];

    // @todo This is basically the same as what is done in     // \Drupal\system\MachineNameController::transliterate(), so it might make     // sense to provide a common service for the two.     $transliterated = $this->transliteration()->transliterate($admin_label, LanguageInterface::LANGCODE_DEFAULT, '_');
    $transliterated = mb_strtolower($transliterated);

    $transliterated = preg_replace('@[^a-z0-9_.]+@', '', $transliterated);

    return $transliterated;
  }

  /** * {@inheritdoc} */
  public function getPreviewFallbackString() {
    
Home | Imprint | This part of the site doesn't use cookies.