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

Php Scanner Laravel Package

gettext/php-scanner

Scan PHP source to extract gettext translations for use with gettext/gettext. Supports multiple domains, default domain selection, and extracting translator/i18n comments. Produces Translations you can export to .po files with generators like PoGenerator.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Laravel Synergy: The package aligns perfectly with Laravel’s i18n architecture, which often relies on gettext or symfony/translation for localization. It complements Laravel’s existing translation mechanisms (e.g., __(), trans() helpers) by automating the extraction of translatable strings from comments and code, reducing manual .po file maintenance.
  • Modular Design: The scanner’s separation of concerns (scanning vs. translation storage) allows it to integrate cleanly into Laravel’s service container or task scheduling (e.g., Artisan commands). For example, it can be wrapped in a custom TranslationScanner facade or service provider.
  • Domain Isolation: Supports Laravel’s need for domain-specific translations (e.g., messages, validation, admin), enabling granular control over where strings are stored and translated. This maps directly to Laravel’s lang/ directory structure or custom domain configurations.
  • Blade Integration: While the scanner doesn’t natively parse Blade templates, Laravel’s compiled Blade output (PHP) can be scanned post-compilation. This requires minimal setup (e.g., scanning storage/framework/views/ after Blade compilation).

Integration Feasibility

  • PHP Parser Dependency: Leverages nikic/php-parser (v5.x), a robust and widely adopted library in PHP’s static analysis ecosystem. Laravel already uses similar tools (e.g., PHPStan, Psalm), so this dependency is low-risk and well-supported.
  • File System Agnostic: Works with glob patterns, allowing it to scan Laravel’s default directories (app/, resources/lang/, routes/) or custom paths. Supports recursive scanning, which is useful for monolithic Laravel applications.
  • Comment-Based Extraction: Extracts strings from i18n:/Translators: comments, which can coexist with Laravel’s existing __() function calls. This dual approach ensures backward compatibility while enabling new workflows (e.g., annotating strings directly in code).
  • Po File Generation: Outputs .po files in a format compatible with Laravel’s translation loaders (e.g., symfony/translation), enabling seamless integration with existing localization pipelines.

Technical Risk

  • PHP Version Constraint: Minimum PHP 7.4 requirement may conflict with older Laravel LTS versions (e.g., 5.8 on PHP 7.2). Mitigation: Use a wrapper script or polyfill, or upgrade PHP to meet the requirement (recommended for long-term support).
  • Blade Template Limitations: Does not natively parse Blade syntax (e.g., @lang, @choice). Mitigation:
    • Pre-process Blade files to PHP using Laravel’s built-in compiler or a custom script.
    • Manually annotate Blade templates with i18n: comments (e.g., @{{-- i18n:Hello --}}).
    • Use laravel-gettext’s Blade directives as a fallback.
  • Dynamic String Handling: May miss strings generated dynamically (e.g., via eval(), create_function(), or complex concatenation). Mitigation: Supplement with manual reviews or custom regex patterns for known edge cases.
  • Performance Overhead: Scanning large codebases (e.g., 50K+ lines) could impact CI/CD pipelines. Mitigation:
    • Cache results (e.g., store scanned strings in a database or JSON file).
    • Run incrementally (e.g., only scan changed files via git diff).
    • Offload to a separate worker process (e.g., Laravel Queues).
  • False Positives/Negatives: May misclassify strings in edge cases (e.g., anonymous functions, static calls). Mitigation: Test thoroughly with a representative subset of the codebase and adjust comment patterns or scanner configurations.

Key Questions

  1. Translation Workflow Alignment:
    • How will this scanner interact with Laravel’s existing translation extraction methods (e.g., __() calls, trans() helpers)? Will it replace, supplement, or run in parallel?
    • Should scanned strings override or merge with manually defined translations in .po files?
  2. CI/CD Pipeline Integration:
    • Where in the pipeline should scanning occur (e.g., pre-commit, post-merge, or on-demand)?
    • How will generated .po files be merged with existing translations (e.g., Git merge conflicts, automated tools like msgmerge)?
  3. Blade Template Strategy:
    • Will Blade templates be pre-processed to PHP, or will translatable strings be manually annotated?
    • Should the scanner ignore Blade files, or will a custom solution (e.g., Blade-to-PHP compiler) be developed?
  4. Domain and Namespace Mapping:
    • How will Laravel’s translation domains (e.g., messages, validation) map to the scanner’s domain configurations?
    • Should domains align with Laravel’s lang/ directory structure or follow a custom schema?
  5. Maintenance and Ownership:
    • Who will be responsible for maintaining .po files and resolving conflicts between scanned and manual translations?
    • Will this require new tooling (e.g., Poedit, Crowdin integration) or process changes (e.g., dedicated localization team)?
  6. Testing and Validation:
    • How will translations be tested (e.g., unit tests for __() calls, integration tests for scanned strings)?
    • Should the scanner include validation rules (e.g., ensuring all __() calls have corresponding .po entries)?
  7. Edge Cases and Customization:
    • Are there specific string patterns (e.g., sprintf, pluralization) that require custom handling?
    • Should the scanner support additional comment patterns (e.g., // translatable, /* i18n */) beyond the default i18n:/Translators:?

Integration Approach

Stack Fit

  • Laravel Core Integration:
    • Service Provider: Register the scanner as a Laravel service provider (e.g., TranslationScannerServiceProvider) to bind it to the container and boot it during application startup.
    • Artisan Command: Create a custom artisan scan:translations command to trigger scanning and generate .po files on demand or via scheduling (e.g., php artisan scan:translations --watch for development).
    • Facade: Expose a clean API (e.g., TranslationScanner::scanAndGenerate()) for use in controllers, commands, or event listeners.
  • Translation Loader Compatibility:
    • Integrate with Laravel’s translation loader (e.g., Illuminate\Translation\FileLoader) to load .po files generated by the scanner. This may require extending Laravel’s loader to support .po files or using a bridge like symfony/translation.
    • For laravel-gettext, the scanner’s output is natively compatible and can be fed directly into the package’s pipeline.
  • Blade Integration:
    • Option 1 (Recommended): Scan compiled Blade files in storage/framework/views/ after Blade compilation. Requires ensuring Blade files are compiled before scanning (e.g., via php artisan view:clear or a custom event listener).
    • Option 2: Use a pre-compilation hook to inject i18n: comments into Blade templates (e.g., via a custom Blade compiler extension).
  • Tooling Ecosystem:
    • Poedit/Crowdin: Generated .po files can be imported into Poedit for manual editing or pushed to Crowdin/Lokalise for collaborative translation.
    • Laravel Mix/Webpack: Trigger scanning via a custom Webpack plugin or npm script (e.g., npm run scan-translations).
    • Git Hooks: Automate scanning on pre-commit or pre-push to ensure translations are up-to-date (e.g., using husky or Laravel’s git hooks).

Migration Path

  1. Pilot Phase:
    • Start with a single module or feature (e.g., resources/lang/ or a specific domain like validation).
    • Manually verify scanned strings against existing .po files to identify false positives/negatives.
    • Adjust scanner configurations (e.g., comment patterns, file paths) based on findings.
  2. Incremental Rollout:
    • Gradually expand scanning to additional directories (e.g., app/Http/Controllers/, routes/).
    • Integrate with CI/CD pipelines (e.g., scan on PR merge, validate .po file changes).
    • Replace manual translation extraction workflows with automated scanning where feasible.
  3. Full Integration:
    • Migrate all translatable strings to the scanner’s workflow.
    • Deprecate legacy extraction methods (e.g., manual __() calls without comments).
    • Implement a feedback loop (e.g., Slack notifications or GitHub checks) for missing or outdated translations.

Compatibility

  • Laravel Versions:
    • LTS Support: Compatible with Laravel 8+ (PHP 7.4+) and Laravel 9/10 (PHP 8.0+). For older versions (e.g., 5.8), use a compatibility layer or upgrade PHP.
    • Gettext Integration: Works with Laravel’s native gettext support or third-party packages like laravel-gettext/symfony/translation.
  • PHP Extensions:
    • Requires the gettext PHP extension for runtime translation (optional if using symfony/translation).
    • No other extensions are mandatory, but intl may
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.
besmartand-pro/php-quality-config
sentix/ai-chatbot
codifyo/ts-generator-bundle
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor