PHPackages                             wazza/dom-translate - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. wazza/dom-translate

ActiveLibrary[Templating &amp; Views](/categories/templating)

wazza/dom-translate
===================

A Laravel Package that will use the build-in Blade Directive to define specific phrases for auto-translation before being rendered to the screen.

v2.4.1(8mo ago)4867↓50%1MITPHPPHP ^8.2 || ^8.3

Since Jul 26Pushed 8mo ago2 watchersCompare

[ Source](https://github.com/wazzac/laravel-translate)[ Packagist](https://packagist.org/packages/wazza/dom-translate)[ Docs](https://www.wazzac.dev)[ RSS](/packages/wazza-dom-translate/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (19)Used By (0)

 [![GitHub issues](https://camo.githubusercontent.com/6e8d7db995eb501d5f5b4868a3085e01d527b9cdd880b16824d0b29f5857dafb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f77617a7a61632f646f6d5472616e736c617465)](https://github.com/wazzac/domTranslate/issues) [![GitHub stars](https://camo.githubusercontent.com/88512aa3ce3109df71ff44ea8d4ff3676dbf409f6f580147382e636cd94442bb/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f77617a7a61632f646f6d5472616e736c617465)](https://github.com/WarrenGIT/domTranslate/stargazers) [![GitHub license](https://camo.githubusercontent.com/8a4196d2e8ca457882001b19a6e200d248156c50b85b55ff02eba18a3211d73e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f57617272656e4749542f646f6d5472616e736c617465)](https://github.com/WarrenGIT/domTranslate/blob/main/LICENSE) [![GitHub version](https://camo.githubusercontent.com/cee963fb969e531594871fe340232bc897603153156fd890d70b053d6d0e9111/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f7461672f57617272656e4749542f646f6d5472616e736c6174653f6c6162656c3d76657273696f6e26736f72743d73656d766572)](https://github.com/WarrenGIT/domTranslate) [![Buy me a coffee](https://camo.githubusercontent.com/24e8d6edab584bb208ad9b37c3c918ab581f5ff86646baba835d9b1663d0802a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4275792532306d6525323061253230636f666665652de298952d79656c6c6f773f7374796c653d666c6174266c6f676f3d6275792d6d652d612d636f66666565266c6f676f436f6c6f723d7768697465)](https://coff.ee/wazzac)

Laravel Translate Package
=========================

[](#laravel-translate-package)

A library that leverages Laravel Directives to provide automated translations for all your Blade phrases or words.

*Example: Write HTML static data in English and display it in a different language in real-time.*

Overview
--------

[](#overview)

The library uses three database tables (*domt\_phrases*, *domt\_translations*, and *domt\_languages*) to manage translations efficiently.

1. On page load, the system searches for a specific translation using the provided phrase in the `@transl8()` directive from the *domt\_translations* table. > Laravel generally cache views, so if the content of the entire page didn't change, steps 1 - 4 will not fire as the cached view will simply load.
2. If the translation is found, it is returned and displayed on the page without making an API call.
3. If the translation is not found *(not translated yet)*, the Google Translate API (or another defined provider) is called to retrieve the new translation.
4. The newly translated text is then inserted into the database to avoid future API calls for the same phrase.

> Note: To ensure quick retrieval of translations, each phrase is hashed and stored in an indexed table column. All searches are performed against this indexed column for optimal performance.

Installation
------------

[](#installation)

> PHP 8.0 is the minimum requirement for this project.

Follow these steps to install the package:

```
composer require wazza/dom-translate
php artisan vendor:publish --tag="dom-translate-config"
php artisan vendor:publish --tag="dom-translate-migrations"
php artisan migrate
```

Register the Service Provider (if not auto-discovered): Add to `bootstrap/providers.php`:

```
return [
        App\Providers\AppServiceProvider::class,
        Wazza\DomTranslate\Providers\DomTranslateServiceProvider::class,
];
```

> *If your package supports Laravel auto-discovery, this step may be optional.*

Add `DOM_TRANSLATE_GOOGLE_KEY={your_google_api_key}` to your `.env` file and run:

```
php artisan config:clear
php artisan config:cache
```

Below are all the supported `.env` keys with their default values if not provided. The `KEY` (i.e., `DOM_TRANSLATE_GOOGLE_KEY`) is required.

```
DOM_TRANSLATE_USE_SESSION=true
DOM_TRANSLATE_USE_DATABASE=true
DOM_TRANSLATE_LOG_LEVEL=3
DOM_TRANSLATE_LOG_INDICATOR=dom-translate
DOM_TRANSLATE_PROVIDER=google
DOM_TRANSLATE_GOOGLE_KEY=
DOM_TRANSLATE_BING_KEY=
DOM_TRANSLATE_HASH_SALT=DzBQ2DxKhNaF
DOM_TRANSLATE_HASH_ALGO=sha256
DOM_TRANSLATE_LANG_SRC=en
DOM_TRANSLATE_LANG_DEST=af
DOM_TRANSLATE_ROUTES_ENABLED=true
DOM_TRANSLATE_ROUTES_PREFIX=api/translate
DOM_TRANSLATE_ROUTES_MIDDLEWARE=web
DOM_TRANSLATE_SESSION_KEY=app_language_code
DOM_TRANSLATE_MIDDLEWARE_ENABLED=true
DOM_TRANSLATE_MIDDLEWARE_AUTO_APPLY=true

```

- If `DOM_TRANSLATE_USE_SESSION` is `true`, translations will be saved in the session and used as the first point of retrieval.
- If no translations are found in the session, or if `DOM_TRANSLATE_USE_SESSION` is `false`, translations will be retrieved from the database, provided they have been previously stored there.
- If translations are still not found, or if both `DOM_TRANSLATE_USE_SESSION` and `DOM_TRANSLATE_USE_DATABASE` are `false`, translations will be sourced from a third-party translation service (e.g., Google Translate).
- Depending on whether `DOM_TRANSLATE_USE_SESSION` and `DOM_TRANSLATE_USE_DATABASE` are `true`, the retrieved translation will be saved to either the session or the database.
- We strongly recommend setting `DOM_TRANSLATE_USE_DATABASE` to `true` *(default is `true` if not specified in your .env)* to ensure we don't make repeated API calls *(also it's slower calling the API verses db/session lookup)*.

> **Note:** If you don't have a [Google Cloud Platform](https://cloud.google.com/gcp) account, sign up and create a new project. Add the *Cloud Translation API* to it. You can use [Insomnia](https://insomnia.rest/download) to test your API key.

[ ![insomnia](https://camo.githubusercontent.com/0230213fdf8d92f73b60370aaa04d0407ebc695ef9c7b0489adb4b2cad02a906/68747470733a2f2f692e6962622e636f2f77576a6d3259742f696e736f6d6e69612e706e67) ](https://ibb.co/R0dwJ78)

### 📋 Step-by-Step Guide: Getting Your Google Translate API Key

[](#-step-by-step-guide-getting-your-google-translate-api-key)

Follow these detailed steps to obtain your Google Translate API key:

#### Step 1: Create a Google Cloud Account &amp; Project

[](#step-1-create-a-google-cloud-account--project)

1. **Visit Google Cloud Console**: Go to
2. **Sign In/Register**: Sign in with your Google account or create a new one
3. **Accept Terms**: Accept the Google Cloud Platform Terms of Service
4. **Create New Project**:
    - Click on the project dropdown (top-left, next to "Google Cloud Platform")
    - Click "New Project"
    - Enter a project name (e.g., "My Translation App")
    - Click "Create" and wait for the project to be created
    - **Select your new project** from the project dropdown

#### Step 2: Enable Billing (Required)

[](#step-2-enable-billing-required)

1. **Go to Billing**: In the left sidebar, click "Billing" or visit
2. **Link Billing Account**:
    - If you don't have a billing account, click "Create Account" and follow the setup
    - Add a valid credit card (Google offers $300 free credits for new accounts)
    - Link the billing account to your project

#### Step 3: Enable the Cloud Translation API

[](#step-3-enable-the-cloud-translation-api)

1. **Go to APIs &amp; Services**: In the left sidebar, click "APIs &amp; Services" &gt; "Library"
2. **Search for Translation API**: Search for "Cloud Translation API"
3. **Enable the API**:
    - Click on "Cloud Translation API"
    - Click the "Enable" button
    - Wait for the API to be enabled (may take a few moments)

#### Step 4: Create API Credentials

[](#step-4-create-api-credentials)

1. **Go to Credentials**: In the left sidebar, click "APIs &amp; Services" &gt; "Credentials"
2. **Create API Key**:
    - Click "Create Credentials" dropdown
    - Select "API key"
    - Your new API key will be generated and displayed
    - **Copy the API key** and store it securely

#### Step 5: Restrict Your API Key (Recommended for Security)

[](#step-5-restrict-your-api-key-recommended-for-security)

1. **Edit API Key**: Click on the API key you just created to edit it
2. **Add API Restrictions**:
    - In the "API restrictions" section, select "Restrict key"
    - Check "Cloud Translation API" from the list
    - Click "Save"
3. **Add Application Restrictions** (Optional but recommended):
    - Choose "HTTP referrers" for web applications
    - Add your domain(s), e.g., `*.yourdomain.com/*`
    - Or choose "IP addresses" for server applications
    - Click "Save"

#### Step 6: Add API Key to Your Laravel Project

[](#step-6-add-api-key-to-your-laravel-project)

1. **Add to .env file**: Copy your API key to your Laravel `.env` file: ```
    DOM_TRANSLATE_GOOGLE_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    ```
2. **Clear config cache**: ```
    php artisan config:clear
    php artisan config:cache
    ```

#### Step 7: Test Your Setup

[](#step-7-test-your-setup)

You can test your API key using a simple curl command:

```
curl -X POST \
  -H "Content-Type: application/json; charset=utf-8" \
  -d @- \
  "https://www.googleapis.com/language/translate/v2?key=YOUR_API_KEY"
