<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Laravel Development]]></title><description><![CDATA[Advanced Laravel &amp; PHP tutorials, tips, and guides for developers to build scalable, secure, and high-performance web applications with creative solutions.]]></description><link>https://laraveldev.hashnode.dev</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1757666933623/fe38b384-49eb-42cd-b37c-35d991b76785.png</url><title>Laravel Development</title><link>https://laraveldev.hashnode.dev</link></image><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 00:31:17 GMT</lastBuildDate><atom:link href="https://laraveldev.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to Translate Your Entire Laravel Website Using Google Cloud Translation API with Language Dropdown and Caching]]></title><description><![CDATA[Want to make your Laravel website multilingual easily? In this tutorial, we’ll learn how to translate all website content dynamically using Google Cloud Translation API, implement a language dropdown, and cache translations to reduce API costs. This ...]]></description><link>https://laraveldev.hashnode.dev/how-to-translate-your-entire-laravel-website-using-google-cloud-translation-api-with-language-dropdown-and-caching</link><guid isPermaLink="true">https://laraveldev.hashnode.dev/how-to-translate-your-entire-laravel-website-using-google-cloud-translation-api-with-language-dropdown-and-caching</guid><category><![CDATA[Multilingual Website]]></category><category><![CDATA[Language Dropdown]]></category><category><![CDATA[Laravel]]></category><category><![CDATA[PHP]]></category><category><![CDATA[google cloud]]></category><category><![CDATA[translation api]]></category><category><![CDATA[Web Development]]></category><category><![CDATA[LaravelTutorials]]></category><category><![CDATA[caching]]></category><category><![CDATA[api integration]]></category><dc:creator><![CDATA[Noni Gopal Chandro]]></dc:creator><pubDate>Fri, 12 Sep 2025 09:23:57 GMT</pubDate><content:encoded><![CDATA[<p>Want to make your <strong>Laravel website multilingual</strong> easily? In this tutorial, we’ll learn how to <strong>translate all website content dynamically using Google Cloud Translation API</strong>, implement a <strong>language dropdown</strong>, and <strong>cache translations</strong> to reduce API costs. This is perfect for Laravel developers who want <strong>safe, fast, and scalable translations</strong>.</p>
<h2 id="heading-prerequisites">Prerequisites</h2>
<p>Before we start, make sure you have:</p>
<ul>
<li><p>A Laravel project (Laravel 9+ recommended).</p>
</li>
<li><p>Google Cloud account and <strong>Translation API enabled</strong>.</p>
</li>
<li><p>Composer installed.</p>
</li>
<li><p>Basic understanding of <strong>Laravel routes, controllers, and views</strong>.</p>
</li>
</ul>
<hr />
<h2 id="heading-step-1-install-google-cloud-translation-sdk">Step 1: Install Google Cloud Translation SDK</h2>
<p>Run the following command in your Laravel project:</p>
<pre><code class="lang-bash">composer require google/cloud-translate
</code></pre>
<h2 id="heading-step-2-set-up-google-cloud-credentials">Step 2: Set Up Google Cloud Credentials</h2>
<ol>
<li><p>Go to your <strong>Google Cloud Console → APIs &amp; Services → Credentials</strong>.</p>
</li>
<li><p>Create a <strong>Service Account Key (JSON)</strong>.</p>
</li>
<li><p>Save the JSON file in your project (e.g., <code>storage/app/google-credentials.json</code>).</p>
</li>
<li><p>Set the environment variable in <code>.env</code>:</p>
</li>
</ol>
<pre><code class="lang-bash">GOOGLE_APPLICATION_CREDENTIALS=/full/path/to/google-credentials.json
</code></pre>
<h2 id="heading-step-3-create-a-translation-service">Step 3: Create a Translation Service</h2>
<p>Create a service to handle translation logic.</p>
<pre><code class="lang-php"><span class="hljs-comment">// app/Services/TranslationService.php</span>

<span class="hljs-keyword">namespace</span> <span class="hljs-title">App</span>\<span class="hljs-title">Services</span>;

<span class="hljs-keyword">use</span> <span class="hljs-title">Google</span>\<span class="hljs-title">Cloud</span>\<span class="hljs-title">Translate</span>\<span class="hljs-title">V2</span>\<span class="hljs-title">TranslateClient</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">Illuminate</span>\<span class="hljs-title">Support</span>\<span class="hljs-title">Facades</span>\<span class="hljs-title">Cache</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TranslationService</span>
</span>{
    <span class="hljs-keyword">protected</span> $translate;

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span>(<span class="hljs-params"></span>)
    </span>{
        <span class="hljs-keyword">$this</span>-&gt;translate = <span class="hljs-keyword">new</span> TranslateClient([
            <span class="hljs-string">'keyFilePath'</span> =&gt; env(<span class="hljs-string">'GOOGLE_APPLICATION_CREDENTIALS'</span>),
        ]);
    }

    <span class="hljs-comment">/**
     * Translate content to the given language
     *
     * <span class="hljs-doctag">@param</span> string $text
     * <span class="hljs-doctag">@param</span> string $target
     * <span class="hljs-doctag">@return</span> string
     */</span>
    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">translate</span>(<span class="hljs-params"><span class="hljs-keyword">string</span> $text, <span class="hljs-keyword">string</span> $target</span>): <span class="hljs-title">string</span>
    </span>{
        <span class="hljs-comment">// Generate a cache key for this text and language</span>
        $cacheKey = <span class="hljs-string">'translation_'</span> . md5($text . $target);

        <span class="hljs-comment">// Check if translation exists in cache</span>
        <span class="hljs-keyword">return</span> Cache::rememberForever($cacheKey, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) <span class="hljs-title">use</span> (<span class="hljs-params">$text, $target</span>) </span>{
            $result = <span class="hljs-keyword">$this</span>-&gt;translate-&gt;translate($text, [
                <span class="hljs-string">'target'</span> =&gt; $target,
            ]);
            <span class="hljs-keyword">return</span> $result[<span class="hljs-string">'text'</span>] ?? $text;
        });
    }
}
</code></pre>
<p>✅ <strong>Explanation:</strong></p>
<ul>
<li><p>Uses <code>Cache::rememberForever</code> to store translations, reducing Google API calls and cost.</p>
</li>
<li><p>Bulk content translation can be done by looping through all website texts.</p>
</li>
</ul>
<hr />
<h2 id="heading-step-4-create-a-language-dropdown">Step 4: Create a Language Dropdown</h2>
<p>In your <strong>Blade template</strong>, add a dropdown for language selection:</p>
<pre><code class="lang-php">&lt;form method=<span class="hljs-string">"GET"</span> action=<span class="hljs-string">"{{ route('translate') }}"</span>&gt;
    &lt;select name=<span class="hljs-string">"lang"</span> onchange=<span class="hljs-string">"this.form.submit()"</span>&gt;
        &lt;option value=<span class="hljs-string">"en"</span> {{ session(<span class="hljs-string">'lang'</span>)==<span class="hljs-string">'en'</span> ? <span class="hljs-string">'selected'</span> : <span class="hljs-string">''</span> }}&gt;English&lt;/option&gt;
        &lt;option value=<span class="hljs-string">"fr"</span> {{ session(<span class="hljs-string">'lang'</span>)==<span class="hljs-string">'fr'</span> ? <span class="hljs-string">'selected'</span> : <span class="hljs-string">''</span> }}&gt;French&lt;/option&gt;
        &lt;option value=<span class="hljs-string">"es"</span> {{ session(<span class="hljs-string">'lang'</span>)==<span class="hljs-string">'es'</span> ? <span class="hljs-string">'selected'</span> : <span class="hljs-string">''</span> }}&gt;Spanish&lt;/option&gt;
        &lt;option value=<span class="hljs-string">"de"</span> {{ session(<span class="hljs-string">'lang'</span>)==<span class="hljs-string">'de'</span> ? <span class="hljs-string">'selected'</span> : <span class="hljs-string">''</span> }}&gt;German&lt;/option&gt;
    &lt;/select&gt;
&lt;/form&gt;
</code></pre>
<h2 id="heading-step-5-create-a-translation-controller">Step 5: Create a Translation Controller</h2>
<pre><code class="lang-php"><span class="hljs-comment">// app/Http/Controllers/TranslationController.php</span>

<span class="hljs-keyword">namespace</span> <span class="hljs-title">App</span>\<span class="hljs-title">Http</span>\<span class="hljs-title">Controllers</span>;

<span class="hljs-keyword">use</span> <span class="hljs-title">App</span>\<span class="hljs-title">Services</span>\<span class="hljs-title">TranslationService</span>;
<span class="hljs-keyword">use</span> <span class="hljs-title">Illuminate</span>\<span class="hljs-title">Http</span>\<span class="hljs-title">Request</span>;

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">TranslationController</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Controller</span>
</span>{
    <span class="hljs-keyword">protected</span> $translator;

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__construct</span>(<span class="hljs-params">TranslationService $translator</span>)
    </span>{
        <span class="hljs-keyword">$this</span>-&gt;translator = $translator;
    }

    <span class="hljs-keyword">public</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">translate</span>(<span class="hljs-params">Request $request</span>)
    </span>{
        $lang = $request-&gt;get(<span class="hljs-string">'lang'</span>, <span class="hljs-string">'en'</span>);
        session([<span class="hljs-string">'lang'</span> =&gt; $lang]);

        <span class="hljs-comment">// Example: translating all website texts</span>
        $contents = [
            <span class="hljs-string">'welcome'</span> =&gt; <span class="hljs-string">'Welcome to our website!'</span>,
            <span class="hljs-string">'about'</span> =&gt; <span class="hljs-string">'We provide high-quality Laravel tutorials.'</span>,
            <span class="hljs-string">'contact'</span> =&gt; <span class="hljs-string">'Contact us for more information.'</span>
        ];

        $translated = [];
        <span class="hljs-keyword">foreach</span> ($contents <span class="hljs-keyword">as</span> $key =&gt; $text) {
            $translated[$key] = <span class="hljs-keyword">$this</span>-&gt;translator-&gt;translate($text, $lang);
        }

        <span class="hljs-keyword">return</span> view(<span class="hljs-string">'welcome'</span>, [<span class="hljs-string">'translations'</span> =&gt; $translated]);
    }
}
</code></pre>
<h2 id="heading-step-6-update-routes">Step 6: Update Routes</h2>
<pre><code class="lang-php"><span class="hljs-comment">// routes/web.php</span>

<span class="hljs-keyword">use</span> <span class="hljs-title">App</span>\<span class="hljs-title">Http</span>\<span class="hljs-title">Controllers</span>\<span class="hljs-title">TranslationController</span>;

Route::get(<span class="hljs-string">'/translate'</span>, [TranslationController::class, <span class="hljs-string">'translate'</span>])-&gt;name(<span class="hljs-string">'translate'</span>);
</code></pre>
<h2 id="heading-step-7-display-translated-content-in-blade">Step 7: Display Translated Content in Blade</h2>
<pre><code class="lang-php">&lt;h1&gt;{{ $translations[<span class="hljs-string">'welcome'</span>] ?? <span class="hljs-string">'Welcome'</span> }}&lt;/h1&gt;
&lt;p&gt;{{ $translations[<span class="hljs-string">'about'</span>] ?? <span class="hljs-string">'About us'</span> }}&lt;/p&gt;
&lt;p&gt;{{ $translations[<span class="hljs-string">'contact'</span>] ?? <span class="hljs-string">'Contact'</span> }}&lt;/p&gt;
</code></pre>
<p>✅ <strong>Explanation:</strong></p>
<ul>
<li><p><code>translations</code> array contains <strong>cached translated text</strong>.</p>
</li>
<li><p>Once a text is translated, it’s served from <strong>cache</strong>, saving API costs.</p>
</li>
</ul>
<hr />
<h2 id="heading-step-8-bulk-translation-for-full-website">Step 8: Bulk Translation for Full Website</h2>
<p>If you want to translate <strong>dynamic content from database</strong>, loop through your models:</p>
<pre><code class="lang-php">$posts = \App\Models\Post::all();

<span class="hljs-keyword">foreach</span> ($posts <span class="hljs-keyword">as</span> $post) {
    $post-&gt;title_translated = <span class="hljs-keyword">$this</span>-&gt;translator-&gt;translate($post-&gt;title, $lang);
    $post-&gt;body_translated  = <span class="hljs-keyword">$this</span>-&gt;translator-&gt;translate($post-&gt;body, $lang);
}
</code></pre>
<p>This way, all posts or content can be <strong>translated in bulk</strong> safely.</p>
<hr />
<h2 id="heading-step-9-optimize-for-cost-amp-performance">Step 9: Optimize for Cost &amp; Performance</h2>
<ul>
<li><p>Use <code>Cache::rememberForever</code> to store translated strings.</p>
</li>
<li><p>Translate only <strong>unique texts</strong> to reduce repeated API calls.</p>
</li>
<li><p>Consider translating <strong>database content once</strong> and saving it for future visits.</p>
</li>
</ul>
<hr />
<h2 id="heading-conclusion">✅ Conclusion</h2>
<p>In this tutorial, you learned how to:</p>
<ol>
<li><p>Integrate <strong>Google Cloud Translation API</strong> with Laravel.</p>
</li>
<li><p>Add a <strong>language dropdown</strong> for users to switch languages.</p>
</li>
<li><p>Translate <strong>all website content safely in bulk</strong>.</p>
</li>
<li><p>Implement <strong>cache to save API costs</strong> and improve performance.</p>
</li>
</ol>
<p>This setup allows you to make your Laravel website <strong>truly multilingual</strong> while keeping costs low and performance high.</p>
]]></content:encoded></item></channel></rss>