Weave 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
Motranslator

Motranslator Laravel Package

phpmyadmin/motranslator

A PHP library for translating strings using Gettext MO files, with fast in-memory lookup, a Gettext-compatible API, and support for plural forms and contexts. No eval required.

View on GitHub
Deep Wiki
Context7

phpmyadmin/motranslator is a lightweight PHP translation library for reading Gettext MO files. It provides both a low-level API for parsing MO files and a convenient object-oriented Translator API, along with a Gettext-compatible emulation layer.

It stores all strings in memory for fast lookups, loads MO files quickly, and evaluates plural equations without eval(), making it a safe, efficient choice for PHP internationalization.

Features:

  • Fast in-memory storage and lookup of translations
  • Object API with setlocale, textdomain, and bindtextdomain
  • gettext, ngettext, pgettext, and npgettext support
  • Low-level MoParser and InMemoryCache classes for direct MO file access
  • Gettext API emulation with no eval() usage for plural equations
Frequently asked questions about Motranslator
Can I use phpmyadmin/motranslator to replace Laravel’s built-in translation system entirely?
Yes, but you’ll need to bridge the two systems. Laravel’s `trans()` helper uses a different syntax and file structure (JSON/PHP arrays), while motranslator relies on Gettext’s `.mo`/`.po` files. You can create a wrapper class or middleware to map Laravel’s language keys to Gettext domains, or use motranslator only for specific parts of your app (e.g., legacy codebases). Start with a service provider to bind motranslator as a singleton and test incrementally.
How do I configure motranslator to work with Laravel’s language files (e.g., `lang/{locale}/messages.php`)?
Motranslator expects `.po`/`.mo` files, not Laravel’s PHP/JSON arrays. You’ll need to convert your existing translations to Gettext format using tools like `xgettext` or `msgfmt`, then place them in a directory like `storage/lang/mo`. Configure motranslator’s path in the service provider, and ensure your Laravel views or controllers use `gettext()` or a custom wrapper instead of `trans()`. For hybrid setups, consider pre-compiling Laravel’s translations into `.po` files.
What Laravel versions does phpmyadmin/motranslator support, and are there breaking changes when upgrading?
Motranslator itself is framework-agnostic but integrates with Laravel via custom logic. It requires PHP 7.4+ and has no strict Laravel version dependency, though Laravel 8+ may need adjustments for newer service container syntax. Breaking changes are rare, but monitor minor releases for Gettext API updates or domain-handling changes. Always test with your Laravel version’s minimum PHP requirement (e.g., Laravel 9.x needs PHP 8.0+).
How do I handle pluralization and gender-specific translations in Laravel using motranslator?
Gettext natively supports plurals (e.g., `%d item|%d items`) and genders via context, but Laravel’s `trans()` helper doesn’t. Use motranslator’s `gettext()` or `dgettext()` functions directly in your Blade templates or controllers, passing the locale and domain. For example: `gettext('There is %d apple', $count, 'fruit')`. Document your plural rules in `.po` files using Gettext’s syntax, and ensure your translation tool (e.g., Crowdin) preserves these during updates.
Is motranslator suitable for production environments with high traffic, or will it cause performance bottlenecks?
Motranslator is performant for production if you pre-compile `.po` files to `.mo` format (binary, fast lookups). Runtime compilation of `.po` files should be avoided in high-traffic apps. Cache compiled `.mo` files aggressively (e.g., with OPcache or Redis) and ensure your server has `ext-gettext` enabled. For extreme scale (millions of translations), consider lazy-loading domains or splitting translations across multiple `.mo` files by feature/module.
How do I integrate motranslator with Laravel’s middleware for locale switching (e.g., based on user preference)?
Use Laravel’s middleware pipeline to set the locale before motranslator processes translations. For example, create middleware that reads the user’s preferred locale from a cookie/session and sets it via `setlocale()` or motranslator’s `bindtextdomain()`. Then, inject the translator into your controllers/views using the resolved locale. Example middleware: `app('translator')->setLocale($request->get('locale', config('app.locale')));`. Combine this with Laravel’s `App::setLocale()` for consistency.
Are there alternatives to phpmyadmin/motranslator for Gettext in Laravel, and when should I choose them?
For Laravel, alternatives include `laravel-gettext` (a Laravel-specific wrapper for Gettext) or sticking with Laravel’s native localization. Use motranslator if you need tight integration with phpMyAdmin’s workflow, support for complex plural/gender rules, or are migrating from a Gettext-based legacy system. Choose `laravel-gettext` if you want a simpler Laravel-centric API. For headless apps, consider caching translations in Redis/JSON instead of `.mo` files to avoid Gettext’s runtime overhead.
How do I test translation logic in PHPUnit/Pest when using motranslator, especially for missing keys or fallbacks?
Mock motranslator’s `gettext()` calls in your tests using PHPUnit’s mocking or Pest’s `fake()`. Test fallbacks by simulating missing keys and verifying the correct fallback locale/domain is used. Example: `$this->app->instance('translator', MockTranslator::expects('gettext')->with('missing.key')->andReturns('fallback'));`. For CI/CD, include tests that validate `.po`/`.mo` file integrity (e.g., no missing plural forms) using tools like `msgfmt --check`.
Can I dynamically switch translation domains at runtime (e.g., per user role or feature flag), and how does motranslator handle this?
Yes, motranslator supports dynamic domains via `bindtextdomain()` and `textdomain()`. In Laravel, bind the translator to the container and resolve domains contextually (e.g., based on Auth or middleware). Example: `app('translator')->setDomain('admin' if Auth::check('admin'));`. For feature flags, use environment variables to enable/disable domains or lazy-load them. Cache domain mappings if performance is critical, but avoid over-caching to allow runtime flexibility.
What’s the best way to deploy translation updates in a Laravel app using motranslator (e.g., `.po`/`.mo` files) without downtime?
Pre-compile `.po` files to `.mo` in your CI/CD pipeline and deploy them alongside your code. Use a file watcher (e.g., Laravel’s `file` cache driver with `php artisan config:clear`) or a cron job to trigger recompilation if `.po` files change. For zero-downtime, deploy to a staging environment first, then swap the `.mo` directory atomically (e.g., via symlink). Monitor for missing translations using Laravel’s `translator` event listeners or a custom middleware that logs untranslated keys.
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.
elmogy/larafast
xeviant/laravel-paystack
gorilladash/laravel-website-sdk
martialbe/flysystem-oss
chondal/3312client
jeroen-g/flickr
opscale-co/nova-genbi
itsjustvita/laravel-retention
tequilarapido/trackit
gregsphotography/site-builder-filament
cabaoctavio/laravel-query-log
gregsphotography/site-builder-components
io-digital/clickatell
snowsoft/laravel-admin
oxygen/auth
relicsoftware/barakah-payment-gateway
tobya/work-with-composer
equipc/laravel-email-validator
equipc/laravel-email-database-log
adedsec/nova-translated-labels