paper-leaf-tech / laravel-translation
Manage laravel translation strings using Google Sheets
Package info
github.com/paper-leaf-tech/laravel-translation
pkg:composer/paper-leaf-tech/laravel-translation
Requires
- php: ^8.2
- google/apiclient: ^2.19
- nikic/php-parser: ^5.0
- spatie/laravel-package-tools: ^1.92
Requires (Dev)
- laravel/pint: ^1.24
- mockery/mockery: ^1.6
- orchestra/testbench: ^9.0|^10.0|^11.0
- phpunit/phpunit: ^11.5|^12.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Manage Laravel translation strings using Google Sheets. This package syncs your Laravel translation files with a Google Spreadsheet, making it easy for non-technical team members to manage translations.
Upgrading
From 0.4.x to 0.5.0
- One
Translationstab replaces theTranslations - {locale}tabs. Before upgrading, runphp artisan translations:pullon 0.4 so code holds the latest translations. After upgrading, runphp artisan translations:pushto build the new tab, then delete the old tabs. translations:pushno longer takes a locale; it always writes every locale. New options:--dry-runand--fresh(replaces--clearand--force-initial).translations:pull {locale?}reads the locale's column from the single tab.- Config:
key_column,original_value_column,updated_value_columnandheader_roware gone;sheet,source_localeandformatare new. Update a published config to matchconfig/laravel-translation.php.
From 0.2.x to 0.3.0
translations:pullnow updates existing translation files in place rather than regenerating them. Comments, blank lines, indentation, and quote styles are preserved.- Pull will not create files or directories. New keys in the sheet are surfaced as warnings; add them to the appropriate file in code first, then re-pull to populate their values. To bootstrap a new locale, create starter files in
lang/{locale}/before pulling. nikic/php-parser ^5.0is now a runtime dependency.
From 0.1.x to 0.2.x
- All env vars renamed:
GOOGLE_SHEETS_*→TRANSLATION_*. GOOGLE_SHEETS_SHEET_NAMEremoved; each locale now uses a sheet tab namedTranslations - {locale}(auto-created).- Default credentials filename:
laravel-translation-credentials.json(waslaravel-translations-account.json). Either rename your file or setTRANSLATION_CREDENTIALS_PATH. - Backups are now local JSON files under
storage/app/translation-backups/, not duplicate sheets in your spreadsheet. - For non-English sheets, Column B is now the English source string and Column C is the translation.
Features
- 🔄 Bi-directional sync — Push Laravel translations to Google Sheets and pull updates back
- ✏️ Surgical pull — Updates land in place via AST manipulation; comments, blank lines, indentation, and quote styles are preserved
- 🌍 One sheet, every locale —
translations:pushwrites every locale underlang/to a singleTranslationstab, one column per locale - 🔤 Editor-friendly sheets — English is editable in the sheet, conflicts with code are listed rather than overwritten, and your own columns (
status,notes, …) are kept - 🔐 Service Account authentication — Simple, secure auth using Google service accounts
- 📝 Nested translations — Automatically handles nested translation arrays using dot notation
- 🗂️ Local JSON backups — Sheet snapshots saved to a gitignored folder before every push
- 🔍 Dry-run mode —
--dry-runpreviews what push or pull would change without writing anything - ✅ Drift check —
translations:checkfails CI when a locale's keys or:placeholdersdrift from the source
Installation
Add the repo to your composer.json:
"repositories": [ { "type": "github", "url": "git@github.com:paper-leaf-tech/laravel-translation.git" } ]
Install via Composer:
composer require paper-leaf-tech/laravel-translation --dev
Optionally publish the configuration file:
php artisan vendor:publish --tag=laravel-translation-config
Setup
1. Download Service Account Credentials
- A service account with credentials has been created under the tech@paper-leaf.com account.
- Check 1Password for "Laravel Translations Service Account" and save the note's content to
storage/app/laravel-translation-credentials.json.
Important: Make sure this file is gitignored:
# .gitignore storage/app/laravel-translation-credentials.json
2. Create and Share Your Google Sheet
- Create a new Google Spreadsheet or use an existing one.
- Share the sheet with
laravel-translation-manager@laravel-translations-sheets.iam.gserviceaccount.com, granting edit access.
3. Get Your Spreadsheet ID
The spreadsheet ID is in the URL:
https://docs.google.com/spreadsheets/d/SPREADSHEET_ID_HERE/edit
Configuration
Add the following to your .env file:
TRANSLATION_SPREADSHEET_ID="your-spreadsheet-id-here" # Optional: override the credentials path (default: storage_path('app/laravel-translation-credentials.json')) # TRANSLATION_CREDENTIALS_PATH="storage/app/laravel-translation-credentials.json" # Optional: backup behaviour. Set TRANSLATION_BACKUP_PATH to false (or null) to disable backups entirely. # TRANSLATION_BACKUP_PATH="app/translation-backups" # TRANSLATION_BACKUP_KEEP=5 # TRANSLATION_BACKUP_AUTO_PRUNE=true
You can customize the tab name, source locale, sheet formatting and backups by publishing config/laravel-translation.php. The matching env vars are TRANSLATION_SHEET (default Translations) and TRANSLATION_FORMAT_SHEET (default true); the source locale is the source_locale config key (default en).
Sheet layout
Every locale lives in one tab, Translations (configurable via TRANSLATION_SHEET):
| key | group | default | en | fr | status | notes |
|---|---|---|---|---|---|---|
failed |
auth |
These credentials do not match our records. | These credentials do not match our records. | Identifiants invalides. | reviewed |
- key / group — the line's key and its file (
auth,resources/schools). Pull writes tolang/{locale}/{group}.php. - default — the English in code as of the last push. Push compares it with code and the
encolumn to tell a developer's change from an editor's. It is protected (with a warning) and can be hidden. - en — editable English. Edit the wording here and pull it into code.
- one column per locale — columns are found by header, and a header counts as a locale when
lang/has a directory with that name. Push adds a column when a new locale appears. - anything else (
status,notes, …) — kept with its row on every push.
When code and the sheet both change a line's English, push keeps the sheet's text and lists the key, so an editor's work is never overwritten silently. A blank cell never erases anything on pull.
Push re-sorts and rewrites every row, so Google cell comments, cell notes and manual cell colours do not stay with their translation. Keep review state in the status and notes columns instead; those move with their row.
Usage
Push translations to Google Sheets
# Push every locale under lang/ to the Translations tab php artisan translations:push # See what would change without writing php artisan translations:push --dry-run # Rebuild the tab from code, ignoring what is in it (a backup is still taken) php artisan translations:push --fresh # Skip the backup for this run php artisan translations:push --no-backup
Push reports new and removed keys, English changed in code (and which translations need review), conflicts, and empty cells it filled from code. Rows whose key is gone from code are dropped; their notes survive in the backup.
Pull translations from Google Sheets
# Pull every locale column php artisan translations:pull # Pull one locale php artisan translations:pull fr # Preview without writing files php artisan translations:pull --dry-run
Pull rejects a value whose :placeholders differ from the English line in code, lists it, and exits with a failure after writing the rest. Rows for JSON (*) and package (package::file) groups are ignored; edit those files directly.
Surgical updates
Pull modifies your translation files in place using PHP AST manipulation. This means:
- Preserved: comments (line, block, doc), blank lines, indentation style, original quote styles (single, double, heredoc, nowdoc).
- Touched: only the string values for keys that already exist in your file.
Pull will not:
- Create new files. If the sheet has
messages.greetingand there's nolang/{locale}/messages.php, the row is skipped with a warning telling you to add the file in code first. - Append new keys. If the sheet has
auth.captcha.invalidand yourlang/{locale}/auth.phpdoesn't have acaptcha.invalidentry, the row is skipped with a warning. Add the key to the file in your editor (with whatever default value you want), then re-pull to populate it. - Touch keys whose current value isn't a simple string literal (e.g., function calls, concatenation, integers). Those are skipped with a warning.
- Remove keys. Anything in your file that isn't on the sheet is left alone.
- Apply a value whose
:placeholdersdiffer from the source-locale line in code. A translation that drops or renames:namewould silently lose that value on the page, so the row is skipped with a warning and pull exits with a failure code once the rest are written; fix it in the sheet and re-pull. Placeholders are compared case-insensitively, since Laravel treats:name,:Nameand:NAMEas the same replacement.
Check translations for drift
# Check every locale against the source locale (en) php artisan translations:check # Check a single locale php artisan translations:check fr # Report missing keys without failing (placeholder mismatches still fail) php artisan translations:check --allow-missing
For each locale, the check reports keys missing from it, keys it has that the source lacks, and lines whose :placeholders differ from the source. Extra keys are not reported for package lines, since packages sometimes ship stale keys the app cannot remove. It reads lang/{locale}/**/*.php (subdirectories included) and lang/{locale}.json. For any package with an override published under lang/vendor/{package}/{locale}, it also checks that package's lines, with the override merged over the package's own files the way Laravel loads them.
It exits non-zero on any problem, so it can gate CI:
- php artisan translations:check
The package is usually a dev dependency, so run the check in a CI step that installs dev dependencies.
Customizing the sheet
Push formats the tab after writing it (turn this off with TRANSLATION_FORMAT_SHEET=false) and then fires PaperleafTech\LaravelTranslation\Events\TranslationsPushed. Listen for it to add your own tabs, dropdowns or colours:
use Illuminate\Support\Facades\Event; use PaperleafTech\LaravelTranslation\Events\TranslationsPushed; use PaperleafTech\LaravelTranslation\Services\GoogleSheetsService; Event::listen(function (TranslationsPushed $event) { app(GoogleSheetsService::class)->batchUpdate([ ['setDataValidation' => [ 'range' => ['sheetId' => $event->sheetId, 'startRowIndex' => 1, 'startColumnIndex' => array_search('status', $event->headers), 'endColumnIndex' => array_search('status', $event->headers) + 1], 'rule' => ['condition' => ['type' => 'ONE_OF_LIST', 'values' => [['userEnteredValue' => 'draft'], ['userEnteredValue' => 'approved']]], 'showCustomUi' => true], ]], ]); });
The event carries sheetName, sheetId, headers and the written rows. The package's own conditional rules contain N("laravel-translation")=0 in their formula and are replaced on every push; rules you add are left alone.
Backups
Before each push, the tab's cells are saved as JSON:
storage/app/translation-backups/
├── .gitignore ← written on first run ("*\n!.gitignore")
├── 2026-10-05_143012.json
└── 2026-10-05_152244.json
TRANSLATION_BACKUP_KEEP controls how many are kept (default 5). TRANSLATION_BACKUP_AUTO_PRUNE toggles pruning. Set TRANSLATION_BACKUP_PATH=false to disable backups; --no-backup skips one run.
Workflow
- Push —
php artisan translations:pushbuilds or updates theTranslationstab from every locale underlang/. - Review in the sheet — editors adjust English in the
encolumn; translators fill the locale columns. - Pull —
php artisan translations:pullwrites non-blank cells back into existinglang/{locale}/*.phpkeys. - Check —
php artisan translations:checkin CI catches keys or placeholders that drifted. - Re-push after adding keys in code. Sheet edits and translations are kept.
Adding a new key (translator-driven)
If a translator notices a missing key and adds it to the sheet, pull will skip it with a warning until the developer adds it to code:
⚠ Skipped 1 new key(s) in lang/en/auth.php (add to code first, then re-pull):
- auth.captcha.invalid
Workflow: developer adds 'captcha' => ['invalid' => ''] (or any default) to lang/en/auth.php, re-runs translations:pull, and the value from the sheet lands in place.
Bootstrapping a new locale
Pull will not create the locale directory. To add a new locale:
mkdir lang/frand add starter files (e.g.,lang/fr/auth.phpreturning[]or a copy of the English file).php artisan translations:push— adds anfrcolumn to theTranslationstab, filled from code.- Translator fills the
frcolumn in the sheet. php artisan translations:pull fr— surgically applies translations to your starter files.
Troubleshooting
Permission Denied
Error: Permission denied accessing Google Sheet
Solution: Share the spreadsheet with the service account email (client_email in your credentials JSON).
Credentials File Not Found
Error: Google Sheets credentials file not found
Solution: Confirm the path in TRANSLATION_CREDENTIALS_PATH (or the default storage/app/laravel-translation-credentials.json) and that the file is readable.
Invalid Credentials JSON
Error: Google Sheets credentials file contains invalid JSON
Solution: Re-download the service account JSON from Google Cloud Console / 1Password. The file must contain "type": "service_account".
Sheet Not Found
Error: Google Sheet not found
Solution: Verify TRANSLATION_SPREADSHEET_ID and that the service account has editor access.
Security
- Never commit your service account JSON file to version control.
- Store credentials under
storage/app/and ensure your.gitignoreexcludes them.
Requirements
- PHP 8.2+
- Laravel 11.x+
License
MIT — see LICENSE.md.
Credits
- Brendan Angerman
- Built with Spatie Laravel Package Tools
- Uses Google API PHP Client