Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Transliterator Laravel Package

behat/transliterator

Abandoned PHP transliteration utility (ported from Perl Text-Unidecode). Provides static methods via Behat\Transliterator\Transliterator. Dataset hasn’t been updated since 2016; consider symfony/string, ext-intl Transliterator, or iconv //TRANSLIT.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation:

    composer require behat/transliterator
    

    (Note: Only use this for legacy Laravel apps or temporary needs. For new projects, prefer Str::slug() or symfony/string for slugs, or PHP’s Transliterator for transliteration.)

  2. First Use Case: Generate a transliterated string (e.g., for slugs or ASCII-compatible text):

    use Behat\Transliterator\Transliterator;
    
    $slug = Transliterator::transliterate('Café au Lait');
    // Output: "Cafe au Lait"
    
  3. Where to Look First:

    • Class: Behat\Transliterator\Transliterator (static methods only).
    • Documentation: README (limited; see static methods in src/Transliterator.php).
    • Alternatives: Laravel’s Str::slug() or symfony/string for slugs; PHP’s Transliterator for locale-aware transliteration.

Implementation Patterns

Usage Patterns

  1. Slug Generation (Legacy Code):

    $title = 'Hôtel & Café';
    $slug = Transliterator::transliterate($title);
    // Output: "Hotel Cafe" (if no additional processing)
    

    (Note: Add Str::lower() and Str::slug() for production-ready slugs.)

  2. Filename Sanitization:

    $filename = Transliterator::transliterate('Résumé.pdf');
    // Output: "Resume.pdf"
    
  3. Normalization for APIs:

    $normalized = Transliterator::transliterate($userInput);
    // Ensure API endpoints handle ASCII-only text.
    

Workflows

  1. Legacy Migration:

    • Replace hardcoded transliteration logic (e.g., regex-based) with the package’s static method.
    • Example:
      // Before (regex)
      $slug = preg_replace('/[^a-z0-9]/i', '-', strtolower($title));
      
      // After (package)
      $slug = strtolower(Transliterator::transliterate($title));
      
  2. Wrapper for Abstraction: Create a service class to isolate the package for future swaps:

    class TransliteratorService {
        public function transliterate(string $text): string {
            return Transliterator::transliterate($text);
        }
    }
    

    Register in Laravel’s IoC container:

    $app->singleton(TransliteratorService::class, function () {
        return new TransliteratorService();
    });
    
  3. Integration with Laravel Validation:

    use Illuminate\Support\Facades\Validator;
    
    $validator = Validator::make($request->all(), [
        'title' => 'required|string|max:255',
    ], [
        'title.required' => 'A title is required.',
    ]);
    
    if ($validator->fails()) {
        return redirect()->back()->withErrors($validator);
    }
    
    $slug = strtolower(Transliterator::transliterate($request->title));
    

Integration Tips

  1. Combine with Laravel Helpers: Use the package alongside Laravel’s Str helper for robust slugs:

    $slug = Str::slug(Transliterator::transliterate($title));
    
  2. Cache Transliterated Strings: For high-traffic apps, cache results to avoid repeated processing:

    $transliterated = Cache::remember("transliterate_{$title}", now()->addHours(1), function () use ($title) {
        return Transliterator::transliterate($title);
    });
    
  3. Locale-Aware Fallback: If locale-specific transliteration is needed, use PHP’s Transliterator as a fallback:

    if (extension_loaded('intl')) {
        $transliterated = Transliterator::create('Any-Latin; Latin-ASCII')->transliterate($title);
    } else {
        $transliterated = Transliterator::transliterate($title);
    }
    

Gotchas and Tips

Pitfalls

  1. Outdated Unicode Support:

    • The dataset was last updated in 2016. New Unicode characters (e.g., emojis, rare scripts) may not transliterate correctly.
    • Example: ß may not always map to ss as expected in German locales.
    • Fix: Validate outputs against business rules or switch to IntlTransliterator.
  2. Licensing Risks:

    • Mixed licenses in the original dataset may cause legal issues for commercial projects.
    • Mitigation: Audit dependencies or use alternatives like symfony/string (MIT-licensed).
  3. PHP Version Incompatibilities:

    • Officially supports PHP 7.2+, but may break on PHP 8.2+ due to unmaintained code.
    • Fix: Pin to PHP 8.1 in composer.json or migrate to alternatives.
  4. No Locale Support:

    • Unlike PHP’s Transliterator, this package does not respect locale-specific rules (e.g., Turkish dotted ‘i’).
    • Workaround: Use iconv($string, 'ASCII//TRANSLIT) for simple cases.
  5. Performance for Bulk Operations:

    • Static methods are lightweight, but repeated calls in loops may add overhead.
    • Optimization: Cache results or use IntlTransliterator for batch processing.

Debugging Tips

  1. Unexpected Outputs:

    • Test edge cases like:
      • Non-Latin scripts (e.g., こんにちは??????).
      • Special characters (e.g., ©, ®, ).
    • Tool: Use var_dump(Transliterator::transliterate($testString)) to inspect mappings.
  2. PHP Warnings:

    • If you see Undefined class errors, ensure the package is autoloaded (run composer dump-autoload).
    • For PHP 8.1+ deprecations, check for static method calls in older PHP versions.
  3. CI/CD Failures:

    • If tests fail on newer PHP versions, add a composer.json platform check:
      "config": {
          "platform": {
              "php": "8.1"
          }
      }
      

Configuration Quirks

  1. No Runtime Configuration:

    • The package has no settings or options; it uses a static dataset.
  2. Alternative Configurations:

    • To customize transliteration, you’d need to fork the package and modify the dataset (not recommended due to maintenance risks).

Extension Points

  1. Custom Mappings:

    • Override transliteration for specific characters by extending the class (not recommended for production):
      class CustomTransliterator extends \Behat\Transliterator\Transliterator {
          public static function transliterate($string) {
              $string = parent::transliterate($string);
              return str_replace('ss', 'ß', $string); // Reverse German mapping
          }
      }
      
  2. Pre/Post-Processing:

    • Chain with Laravel’s Str helper for additional transformations:
      $processed = Str::of(Transliterator::transliterate($title))
          ->lower()
          ->replaceMatches('/[^a-z0-9-]+/', '-')
          ->trim('-')
          ->toString();
      
  3. Forking for Maintenance:

    • If you must extend the package, fork it and update the Unicode dataset (e.g., from Unicode CLDR).
    • Warning: This introduces long-term maintenance overhead.

Laravel-Specific Tips

  1. Service Provider Binding: Bind the transliterator to Laravel’s container for dependency injection:

    $app->bind('transliterator', function () {
        return new class {
            public function __invoke(string $text) {
                return Transliterator::transliterate($text);
            }
        };
    });
    

    Usage:

    $transliterated = app('transliterator')($title);
    
  2. Artisan Command: Create a command to test transliteration outputs:

    use Illuminate\Console\Command;
    
    class TransliterateCommand extends Command {
        protected $signature = 'transliterate {text}';
        protected $description = 'Transliterate a string';
    
        public function handle() {
            $this->info(Transliterator::transliterate($this->argument('text')));
        }
    }
    
  3. Model Observers: Automatically transliterate attributes on save:

    class PostObserver {
        public function saving(Post $post) {
            $post->slug = Str::slug(Transliterator::transliterate($post->title));
        }
    }
    

When to Avoid

  • New Projects: Use Str::slug() or `symfony
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi