spiral/translator
Spiral Translator provides i18n translation tooling with static analysis support and auto-indexation. Designed for Spiral Framework apps, it helps manage locales, translation keys, and validation via PHPUnit/Psalm-friendly architecture.
Start by installing the package via Composer (composer require spiral/translator) and registering its service provider in your Spiral application. The core entry point is the Spiral\Translator\TranslatorInterface, which you can inject wherever translation is needed—typically in controllers, services, or view helpers. Begin with a basic usage: resolve a message by key and locale using $translator->translate('welcome.message', 'en_US'). For initial setup, examine the config/translator.php file (auto-generated if using Spiral skeleton), where you define message sources (e.g., arrays, YAML files per domain/ locale). A minimal first catalog might define messages.php per locale like messages/en.php.
Note (v1.3.1): The translator no longer throws an exception if a locale directory is missing—it silently falls back to the next available catalog. While this improves resilience, always validate the requested locale before calling
translate()(e.g., viaLocaleCatalog::hasLocale('en_US')) to avoid unintended fallback behavior in production.
auth, validation, errors) to isolate translation concerns. Use $translator->translate('login.submit', 'domain=auth') for clarity.default_locale and fallback_locale in config to gracefully handle missing translations. Implement a fallback chain (e.g., en_US → en) by defining locales as an array in config.TranslatorInterface into services (not controllers directly) to avoid tight coupling—e.g., build dynamic emails or notifications with localized body and subject.__() wrapper) that delegates to the translator—this keeps templates clean and testable.TranslatorInterface in unit tests. Preload test catalogs in memory using ArrayCatalog for fast, deterministic tests. Since v1.3.1, your test setup should explicitly create expected locale directories or use ArrayCatalog to avoid silent fallbacks during test execution.spiral/framework’s cache bridge or a custom CachedCatalogLoader) in production—especially for large message sets.en_US, not en-us). Always sanitize user-provided locale values before passing to translate() to avoid silent fallbacks or errors. v1.3.1 exacerbates this: missing directories now go unreported—so normalize and validate locales explicitly in middleware or validation layers.on_missing callback in config to log or replace with a placeholder (e.g., ?key?).title or error. Prefer namespaced keys (e.g., product.details.title) or explicit domains to prevent collisions across modules.dump() method (if available in your version) to extract keys from codebase into YAML/PHP catalogs incrementally—start with a single domain (e.g., deprecated.php) and refactor over time.Filesystem::exists() or glob() in tests/) to catch setup regressions early.How can I help you explore Laravel packages today?