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

Google Translate Php Laravel Package

stichoza/google-translate-php

Free Google Translate API wrapper for PHP (8.0+). Translate text between languages, detect languages, preserve parameters, and customize HTTP clients or token generators—no API key required. Installed via Composer.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer: composer require stichoza/google-translate-php (pin ^5.3.5 or later—see Gotchas for the emoji/token fix). The package provides a simple, static-based translator class with no configuration needed for basic usage. Start by instantiating the translator: $translator = new Stichoza\GoogleTranslate\TranslateClient();, then translate text: $translated = $translator->translate('Hello world', 'es');. The first argument is the text, the second is the target language code (ISO 639-1). For detecting language, use $translator->detect('Hola mundo');. Check the README.md in the repo for full list of supported languages and examples.

Implementation Patterns

  • Use caching (e.g., with Laravel’s cache facade) around translate() calls to avoid repeated network requests for identical text.
  • Wrap the translator in a service class to abstract language handling and improve testability (e.g., TranslationService::translateText($text, $to, $from = null)).
  • In Laravel, bind the translator in a service provider and inject it where needed instead of static calls.
  • Use batch translation via looping or async handling (e.g., Horizon jobs) for large volumes—note the API has rate limits and may throttle.
  • Leverage environment variables or config files to manage fallback languages, default locales, and API override strategies (though the package itself uses no configurable API key—it's true free usage).
  • Add a regression test that translates a short string containing an emoji (e.g. 'Hello 👋') if your content pipeline accepts user-generated text; token generation for such input is exactly what v5.3.5 repaired, so a test locks in the behaviour across future upgrades.

Gotchas and Tips

  • Emoji / non-BMP characters required v5.3.5+: token generation previously broke for text containing emoji, causing failed or garbled translations. Upgrade and pin at least 5.3.5 if you translate user-generated content, social copy, or chat messages.
  • Unreliable under load: This package uses the unofficial Google Translate frontend, so it's subject to change, blocks, or rate-limiting without warning. Monitor for 403/503 responses and implement retry logic.
  • Language detection may return auto or be inaccurate for short text—always validate detected results in sensitive workflows.
  • The package doesn’t support translating HTML directly—strip or escape tags before translation to avoid corruption; consider using a post-process to reinsert HTML safely.
  • No official support for Google Translate’s advanced options (e.g., formality, glossaries). For production-grade translation, consider migrating to the official Google Cloud Translation API when scalability is needed.
  • To debug, inspect $translator->getLastResponse() or catch GuzzleHttp\Exception\RequestException to capture HTTP-level issues. When a translation fails only for certain inputs, log the raw source string (with encoding) before assuming an API outage—malformed tokens are an input-dependent failure mode, not a transient one.
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.
htinlynn/genius-helper
maco-studios/hephaestus
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