Blog Image

Method: Real-Time Translation Using Google Translate API

Get Google Translate API Key

Sign up at Google Cloud Console
Enable the Google Cloud Translation API
Get an API key from Credentials โ†’ API Key

                                            <?php  ?>
                                            

Create a Translation Helper in CodeIgniter 4

Create a helper file:
๐Ÿ“‚ app/Helpers/translation_helper.php

                                            function translate($text, $targetLang = 'en')
{
    $apiKey = 'YOUR_GOOGLE_TRANSLATE_API_KEY';
    $url = "https://translation.googleapis.com/language/translate/v2?key={$apiKey}&q=" . urlencode($text) . "&target={$targetLang}";

    $response = file_get_contents($url);
    $json = json_decode($response, true);

    return $json['data']['translations'][0]['translatedText'] ?? $text;
}
                                            

Autoload the Helper

In app/Config/Autoload.php, add 'translation' to helpers:

                                            public $helpers = ['url', 'form', 'translation'];
                                            

Modify Your Controller to Use Auto-Translation

๐Ÿ“‚ app/Controllers/MockTestController.php

                                            namespace App\Controllers;

use App\Models\MockTestModel;
use CodeIgniter\Controller;

class MockTestController extends Controller
{
    public function show($id)
    {
        $model = new MockTestModel();
        $mockTest = $model->find($id);

        $lang = session()->get('lang') ?? 'hi'; // Default language is Hindi
        $data['title'] = ($lang === 'hi') ? $mockTest->title : translate($mockTest->title, $lang);
        $data['description'] = ($lang === 'hi') ? $mockTest->description : translate($mockTest->description, $lang);

        return view('mock_test_view', $data);
    }

    public function switchLanguage($lang)
    {
        session()->set('lang', $lang);
        return redirect()->back();
    }
}
                                            

Modify View to Display Translated Content

๐Ÿ“‚ app/Views/mock_test_view.php

                                            <h1><?= esc($title); ?></h1>
<p><?= esc($description); ?></p>

<!-- Language Switch Links -->
<a href="<?= site_url('switch-language/en'); ?>">English</a> |
<a href="<?= site_url('switch-language/hi'); ?>">เคนเคฟเคจเฅเคฆเฅ€</a>
                                            

Define Routes

๐Ÿ“‚ app/Config/Routes.php

                                            $routes->get('switch-language/(:segment)', 'MockTestController::switchLanguage/$1');
                                            

Final Output

Now when you click "English", it will:
1๏ธโƒฃ Store 'en' in the session
2๏ธโƒฃ Automatically translate all content using Google Translate API
3๏ธโƒฃ Show the translated content without modifying your database

Advantages of This Approach

โœ… No need to store translations manually
โœ… Works for all dynamic content: blogs, mock tests, current affairs, etc.
โœ… Supports multiple languages
โœ… Easy to implement, no database changes

๐Ÿš€ Let me know if you want help setting up the API key! ๐Ÿš€

Share:
Related Post

1 Comments

Profile Picture
rsrqmwuxrk
02 Jun 2025, 02:09 AM

rqtuyeewppzngxrgnghntezfjspmve

Leave A Comment