A Laravel package for synchronizing Laravel PHP translation arrays with PO (Portable Object) files, with optional POEditor integration.
Perfect for teams using translation management services like POEditor, Crowdin, or Lokalise that work with industry-standard gettext PO files.
- π€ Export Laravel PHP translations to PO files
- π₯ Import PO files back to Laravel PHP arrays
- π POEditor Integration - Download translations directly from POEditor API
- π― Selective Imports - Filter translations by pattern with
--onlyoption - π Merge or Replace - Choose to merge with existing translations or replace them
- π Auto-detection - Automatically detect available languages from your lang directory
- π§ͺ Fully Tested - Comprehensive test suite with 30+ tests
- βοΈ Configurable - All paths and settings can be customized
- PHP 8.2 or higher
- Laravel 11.x or higher
Install the package via Composer:
composer require webhappens/laravel-poThe service provider will be automatically registered via Laravel's package discovery.
Publish the configuration file to customize paths and settings:
php artisan vendor:publish --tag=po-configThis will create a config/po.php file where you can configure:
- Export and import directory paths
- Excluded translation groups
- Language definitions
- POEditor API credentials
- Cache clearing callbacks
// config/po.php
return [
'paths' => [
'export' => lang_path('export'), // Where PO files are exported
'import' => lang_path('import'), // Where PO files are imported from
'lang' => lang_path(), // Root translation directory
],
'excluded_groups' => [
'auth',
'pagination',
'passwords',
'validation',
],
'languages' => [
// Leave empty for auto-detection, or define explicitly:
'en' => ['label' => 'English', 'enabled' => true],
'fr' => ['label' => 'FranΓ§ais', 'enabled' => true],
'de' => ['label' => 'Deutsch', 'enabled' => true],
],
];If you use POEditor, add these to your .env file:
POEDITOR_ENABLED=true
POEDITOR_API_TOKEN=your-api-token-here
POEDITOR_PROJECT_ID=your-project-id-hereIf your application caches compiled translation catalogues, configure a callback to clear the cache after importing:
// config/po.php
'cache' => [
'clear_callback' => function ($locale) {
Cache::forget("catalogue:{$locale}");
File::delete(storage_path("app/cache/catalogue:{$locale}"));
},
],Export all translations for the default language:
php artisan po:exportExport translations for specific languages:
php artisan po:export fr de esThis will create PO files in your configured export directory (default: lang/export/):
lang/export/
βββ fr.po
βββ de.po
βββ es.po
Import all available PO files:
php artisan po:importImport specific languages:
php artisan po:import fr deInclude fuzzy translations:
php artisan po:import --fuzzyFilter to specific translation keys:
php artisan po:import --only=actions.*
php artisan po:import --only=messages.* --only=actions.*Replace existing translations instead of merging:
php artisan po:import --replaceBy default, imports will merge new translations with existing ones. Use --replace to completely replace the translation files.
Download PO files from POEditor (requires POEditor configuration):
# Download all enabled languages
php artisan po:download --all
# Download specific languages
php artisan po:download fr de esDownloaded files will be saved to your import directory. Then run the import command:
php artisan po:import-
Export your current translations:
php artisan po:export
-
Upload PO files to POEditor (manually or via their API)
-
Translators work on translations in POEditor
-
Download updated translations:
php artisan po:download --all
-
Import the translations:
php artisan po:import
-
Commit the updated translation files to your repository
-
Export translations:
php artisan po:export fr de es
-
Send PO files from
lang/export/to your translation agency -
Receive translated PO files and place them in
lang/import/ -
Import the translations:
php artisan po:import
The package automatically converts between Laravel and gettext placeholder formats:
Laravel format (in PHP files):
'welcome' => 'Hello :name, you have :count messages'Gettext format (in PO files):
msgid "Hello {name}, you have {count} messages"
This conversion happens automatically during export and import.
Laravel translations are organized into groups (files), and the package maintains this structure:
PHP files:
lang/en/
βββ actions.php
βββ messages.php
βββ errors.php
PO file structure:
msgctxt "actions.save"
msgid "Save"
msgstr "Sauvegarder"
msgctxt "messages.welcome"
msgid "Welcome"
msgstr "Bienvenue"
msgctxt "errors.not_found"
msgid "Not found"
msgstr "Non trouvΓ©"
The first part before the dot (e.g., actions, messages) determines which PHP file the translation belongs to.
Nested arrays are automatically flattened to dot notation:
PHP:
// lang/en/messages.php
return [
'user' => [
'welcome' => 'Welcome :name',
'goodbye' => 'Goodbye :name',
],
];PO:
msgctxt "messages.user.welcome"
msgid "Welcome {name}"
msgstr "..."
msgctxt "messages.user.goodbye"
msgid "Goodbye {name}"
msgstr "..."
Run the test suite:
cd ~/Sites/packages/laravel-po
composer testThe package includes comprehensive tests covering:
- Export functionality
- Import functionality with merge/replace modes
- Placeholder conversion
- Fuzzy translation handling
- POEditor API integration (with HTTP mocking - no POEditor account required!)
- Pattern filtering
- Language auto-detection
Note: Tests use HTTP mocking for POEditor API calls, so you don't need a POEditor account or project to run the tests.
For detailed testing instructions, see TESTING.md.
your-laravel-app/
βββ lang/
βββ export/ # Generated PO files (add to .gitignore)
βββ import/ # PO files to be imported (add to .gitignore)
βββ en/
β βββ actions.php
β βββ messages.php
β βββ ...
βββ fr/
β βββ actions.php
β βββ messages.php
β βββ ...
βββ de/
βββ actions.php
βββ messages.php
βββ ...
Recommended .gitignore:
/lang/export/*
!/lang/export/.gitignore
/lang/import/*
!/lang/import/.gitignoreThis keeps the directories tracked while ignoring the PO files themselves.
Please see CHANGELOG for more information on what has changed recently.
Contributions are welcome! Please feel free to submit a Pull Request.
If you discover any security-related issues, please email hello@webhappens.co.uk instead of using the issue tracker.
- WebHappens
- Built with gettext/gettext
The MIT License (MIT). Please see License File for more information.