Download the PHP package wazza/dom-translate without Composer
On this page you can find all versions of the php package wazza/dom-translate. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download wazza/dom-translate
More information about wazza/dom-translate
Files in wazza/dom-translate
Package dom-translate
Short Description A Laravel Package that will use the build-in Blade Directive to define specific phrases for auto-translation before being rendered to the screen.
License MIT
Homepage https://www.wazzac.dev
Informations about the package dom-translate
Laravel Translate Package
A Laravel package that uses Blade directives to provide real-time translations for any phrase in your views — backed by a smart caching layer (session → database → cloud API) to minimise API calls and costs.
Write your Blade templates in English, serve every visitor in their own language — automatically.
Table of Contents
- How it Works
- Requirements
- Installation
- Configuration
- Translation Providers
- Google Cloud Translation
- Bing / Azure Cognitive Translator
- Blade Directives
- Auto-Translation (
@transl8auto) - Language Preference API Routes
- SetLocale Middleware
- Using the Helper Directly
- Upgrading to v2.5
- Troubleshooting
- Contributing
How it Works
Three database tables (domt_languages, domt_phrases, domt_translations) power a three-level cache:
- Session (fastest) — if
DOM_TRANSLATE_USE_SESSION=true, translations are stored in the user's session after the first retrieval. - Database (fast) — if
DOM_TRANSLATE_USE_DATABASE=true(default), translations are stored and retrieved from the DB. Each phrase is hashed (SHA-256 HMAC) and stored in an indexed column for sub-millisecond lookups. - Cloud API (fallback) — only when no cached translation exists. The result is immediately stored in the DB (and session) to prevent future API calls.
Laravel caches compiled Blade views, so unchanged pages make zero database or API calls after the first render.
Requirements
| Requirement | Version |
|---|---|
| PHP | ^8.2 |
| Laravel | ^12.0 |
| GuzzleHTTP | ^7.8 (auto-installed) |
| Cloud API key | Google or Bing (at least one) |
Installation
Publish the config and migrations, then run the migrations:
Add your API key(s) to .env:
Clear config cache:
Auto-discovery: The service provider is auto-discovered. No manual registration is needed in
bootstrap/providers.phpunless you have disabled package discovery.
Configuration
After publishing, the config file is at config/dom_translate.php. All settings are also controllable via .env:
Security note: Change
DOM_TRANSLATE_HASH_SALTto a unique value per application. Do not reuse the default across projects.
Translation Providers
Google Cloud Translation Setup
- Visit Google Cloud Console and create a project.
- Enable Cloud Translation API under APIs & Services → Library.
- Create an API key under APIs & Services → Credentials.
- Restrict the key to Cloud Translation API to limit exposure.
- Add to
.env:
Free tier: 500,000 characters/month. Billing account required even for free tier.
Test with curl:
Bing / Azure Cognitive Translator Setup
- Sign in to the Azure Portal.
- Create a Cognitive Services resource (or a dedicated Translator resource).
- Copy one of the subscription keys from Keys and Endpoint.
- Add to
.env:
Free tier: 2,000,000 characters/month (F0 tier).
Blade Directives
@transl8(phrase[, dest[, src]])
The primary directive. Translates a phrase to the destination language.
Language-Specific Shortcuts
Pre-registered directives for common languages:
Adding more languages in your AppServiceProvider::register():
@transl8auto(phrase)
Translates to whatever language is stored in the user's session or cookie. See Auto-Translation below.
Auto-Translation (@transl8auto)
@transl8auto is the most powerful directive. It reads the user's language preference (set via a language switcher) and translates accordingly — no per-phrase language specification needed.
Language priority order
- Session —
session('app_language_code') - Cookie —
cookie('app_language_code') - Config default —
dom_translate.language.dest - App locale —
config('app.locale') - English — final fallback
Building a language switcher
1. Add a selector to your layout:
2. Wire it up with JavaScript:
Vue 3 Component Example
Language Preference API Routes
Enabled by default. Can be disabled with DOM_TRANSLATE_ROUTES_ENABLED=false.
POST /api/translate/set-language
Response:
Also sets a 1-year, httpOnly cookie with the preference.
GET /api/translate/get-language
Response:
Disable routes and roll your own
SetLocale Middleware
SetLocaleMiddleware automatically syncs Laravel's application locale — and Carbon's locale — to the user's language preference on every request.
This ensures __(), trans(), @lang, validation messages, and Carbon date formatting all use the same language as the translate directives.
Enabled by default. To disable:
To keep the middleware but not auto-apply it to the web group:
Then apply it manually where needed:
Using the Helper Directly
TranslateHelper and PhraseHelper are also available for use in controllers, jobs, or anywhere outside Blade:
Upgrading to v2.5
If you are upgrading from v2.4.x:
-
Re-publish migrations — the migration files have been updated to anonymous class syntax (required by Laravel 12). If you have already run the old migrations, no action is needed for existing databases. For fresh installs, re-publish:
-
No breaking changes — all Blade directives, config keys, and
.envvariables remain identical. -
BingTranslate is now implemented — if you set
DOM_TRANSLATE_PROVIDER=bingin your.env, it will now work with the Azure Cognitive Translator v3 API. -
Cookie security — the language preference cookie is now
httpOnly=true(XSS protection). This is transparent to your application. DOM_TRANSLATE_HASH_SALT— if you are using a custom salt, ensure it remains set in your.envto preserve existing hash lookups in the database.
Troubleshooting
E_STRICT deprecation warnings during composer install
This is not caused by this package. It is caused by an outdated system-level Composer installation (typically /usr/share/php/Composer/). Update your server's Composer:
Translations not showing / returning original phrase
- Check your API key is set:
DOM_TRANSLATE_GOOGLE_KEYorDOM_TRANSLATE_BING_KEY. - Check
DOM_TRANSLATE_LOG_LEVEL=3and review Laravel's log for API errors. - Ensure the destination language code (
DOM_TRANSLATE_LANG_DEST) exists in thedomt_languagestable. - Clear config and view cache:
MissingAppKeyException in tests
Ensure APP_KEY is set in phpunit.xml or your .env.testing. The package's test suite sets a dummy key automatically for its own tests.
Language not persisting between requests
Ensure DOM_TRANSLATE_USE_SESSION=true or that cookies are being sent with requests. For AJAX requests, verify the X-CSRF-TOKEN header is included in POST calls.
Contributing
Contributions are welcome. Please read SECURITY.md.
Made with ☕ by Warren Coetzee
All versions of dom-translate with dependencies
guzzlehttp/guzzle Version ^7.8
illuminate/support Version ^12.0
php Version ^8.2