PHPackages                             nabila/laravel-translation-sync - 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. [Localization &amp; i18n](/categories/localization)
4. /
5. nabila/laravel-translation-sync

ActiveLibrary[Localization &amp; i18n](/categories/localization)

nabila/laravel-translation-sync
===============================

A Laravel package to detect untranslated strings and automatically translate them using various translation services like Google Translate, DeepL, and more.

2.0.1(7mo ago)03PHPPHP &gt;=8.0

Since Oct 12Pushed 6mo agoCompare

[ Source](https://github.com/Nabilaahmed2000/laravel-translation-sync)[ Packagist](https://packagist.org/packages/nabila/laravel-translation-sync)[ Docs](https://github.com/Nabilaahmed2000/laravel-translation-sync)[ RSS](/packages/nabila-laravel-translation-sync/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (5)Versions (3)Used By (0)

Laravel Translation Sync
========================

[](#laravel-translation-sync)

A powerful Laravel package that automatically detects untranslated strings in your application and provides automatic translation capabilities using **free** translation services.

✨ Features
----------

[](#-features)

- 🔍 **Smart Detection**: Scans your entire Laravel application for untranslated strings
- 🌐 **Free Auto Translation**: Uses LibreTranslate and MyMemory (completely free, no API keys needed!)
- 📊 **Translation Statistics**: Get detailed insights about your translation coverage
- 🔧 **Flexible Configuration**: Customizable scan paths, file formats, and translation patterns
- 🎯 **Multiple File Formats**: Support for both JSON and PHP translation files
- 📱 **Rich Console Interface**: Beautiful command-line interface with progress indicators
- 🛡️ **Safe Operation**: Dry-run mode to preview changes before applying them

🚀 Quick Start (5 minutes!)
--------------------------

[](#-quick-start-5-minutes)

### 1. Install the Package

[](#1-install-the-package)

```
composer require nabila/laravel-translation-sync

```

### 2. Publish Configuration

[](#2-publish-configuration)

```
php artisan vendor:publish --tag=translation-sync-config

```

### 3. Start Translating! (No API keys needed!)

[](#3-start-translating-no-api-keys-needed)

```
# Scan and auto-translate using FREE services
php artisan translations:sync --translate --auto

# Check your translation coverage
php artisan translations:sync --stats

```

That's it! 🎉 Your Laravel app now has automatic translation capabilities using completely free services.

Requirements
------------

[](#requirements)

- **PHP**: &gt;= 8.0
- **Laravel**: 9.x, 10.x, 11.x, 12.x
- **Composer**: Latest stable version

🚀 Quick Start (5 minutes!)
--------------------------

[](#-quick-start-5-minutes-1)

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

[](#installation)

Install the package via Composer:

```
composer require nabila/laravel-translation-sync

```

Publish the configuration file:

```
php artisan vendor:publish --tag=translation-sync-config

```

⚡ Zero-Configuration Setup
--------------------------

[](#-zero-configuration-setup)

The package comes pre-configured to work with **free translation services**. No API keys, no billing setup required!

```
# Your .env file (these are the defaults)
TRANSLATION_SERVICE=libretranslate  # Free service!
TRANSLATION_AUTO_TRANSLATE=true

```

Start using it immediately:

```
php artisan translations:sync --translate --auto

```

🧪 Testing Your Installation
---------------------------

[](#-testing-your-installation)

After installation, test that everything works:

```
# Run the test script
php test-package.php

# Or test with Laravel commands
php artisan translations:sync --stats
php artisan translations:sync --translate --dry-run

```

Configuration
-------------

[](#configuration)

The package comes with a comprehensive configuration file. Here are the key settings:

```
// config/translation-sync.php

return [
    // Translation service: 'libretranslate', 'mymemory', 'google', 'dummy'
    'service' => env('TRANSLATION_SERVICE', 'libretranslate'),

    // Source language (language of your translation keys)
    'source_language' => env('TRANSLATION_SOURCE_LANG', 'en'),

    // Target languages (defaults to Arabic - change as needed!)
    'target_languages' => env('TRANSLATION_TARGET_LANGS', ['ar']),

    // File format: 'php' (Laravel default) or 'json'
    'file_format' => env('TRANSLATION_FILE_FORMAT', 'php'),

    // Enable automatic translation
    'auto_translate' => env('TRANSLATION_AUTO_TRANSLATE', false),

    // Translation service configurations
    'services' => [
        'libretranslate' => [], // No config needed - FREE!
        'mymemory' => [], // No config needed - FREE!
        'google' => [
            'api_key' => env('GOOGLE_TRANSLATE_API_KEY', null),
        ],
        'freetranslateapi' => [
            'url' => env('FREETRANSLATEAPI_URL', 'http://localhost:5000'),
        ],
        // ... other services
    ],
];

```

### Environment Variables

[](#environment-variables)

Add these to your `.env` file:

```
TRANSLATION_SERVICE=libretranslate  # FREE - No API key needed!
TRANSLATION_SOURCE_LANG=en
TRANSLATION_AUTO_TRANSLATE=true
# No API key needed for free services!

```

> 🎉 **No billing, no API keys, no setup required!** The package uses free translation services by default.

> 📋 **Need help getting a Google Translate API key?** Check out our [Quick Setup Guide](GOOGLE_TRANSLATE_SETUP.md) for step-by-step instructions!

Usage
-----

[](#usage)

### Basic Scanning

[](#basic-scanning)

Scan for missing translations:

```
php artisan translations:sync

```

### Automatic Translation

[](#automatic-translation)

Enable automatic translation with the `--translate` flag:

```
php artisan translations:sync --translate

```

### Advanced Options

[](#advanced-options)

```
# Auto-approve all translations without confirmation
php artisan translations:sync --auto --translate

# Use a specific translation service
php artisan translations:sync --translate --service=google

# Set source and target languages
php artisan translations:sync --translate --source=en --targets=ar,en

# Or use the --locales alias
php artisan translations:sync --translate --locales=ar,en

# Dry run (preview changes without making them)
php artisan translations:sync --dry-run

# Show translation statistics
php artisan translations:sync --stats

# Use PHP files instead of JSON
php artisan translations:sync --format=php

```

Translation Services
--------------------

[](#translation-services)

### 🚀 **Free Services (No API Keys Required!)**

[](#-free-services-no-api-keys-required)

#### LibreTranslate (Default - Recommended)

[](#libretranslate-default---recommended)

**Completely free, open-source translation service**

```
TRANSLATION_SERVICE=libretranslate

```

- ✅ **No API key required**
- ✅ **No billing needed**
- ✅ **Open source and self-hosted**
- ✅ **Supports 50+ languages**
- ✅ **Fast and reliable**

#### MyMemory

[](#mymemory)

**Community-driven translation service**

```
TRANSLATION_SERVICE=mymemory

```

- ✅ **No API key required**
- ✅ **No billing needed**
- ✅ **Community translations**
- ✅ **Supports 80+ languages**

### 💰 **Paid Services**

[](#-paid-services)

#### Google Translate

[](#google-translate)

**Professional translation service**

```
TRANSLATION_SERVICE=google
GOOGLE_TRANSLATE_API_KEY=your-api-key

```

- ✅ **High quality translations**
- ✅ **Supports all major languages**
- ⚠️ **Requires billing setup**
- ⚠️ **API key required**

> 📋 **Need help with Google Translate API?** Check out our [Quick Setup Guide](GOOGLE_TRANSLATE_SETUP.md)

Follow these steps to obtain your Google Translate API key:

1. **Go to Google Cloud Console**

    - Visit [Google Cloud Console](https://console.cloud.google.com/)
    - Sign in with your Google account
2. **Create or Select a Project**

    - Click on the project dropdown at the top
    - Either select an existing project or click "New Project"
    - If creating new: Enter project name and click "Create"
3. **Enable the Cloud Translation API**

    - In the left sidebar, go to "APIs &amp; Services" &gt; "Library"
    - Search for "Cloud Translation API"
    - Click on "Cloud Translation API" from the results
    - Click the "Enable" button
4. **Create API Credentials**

    - Go to "APIs &amp; Services" &gt; "Credentials"
    - Click "Create Credentials" &gt; "API key"
    - Your API key will be generated and displayed
    - **Important**: Copy and save this key securely
5. **Secure Your API Key (Recommended)**

    - Click on the API key name to edit it
    - Under "API restrictions", select "Restrict key"
    - Choose "Cloud Translation API" from the list
    - Under "Application restrictions", you can:
        - Set HTTP referrers for web apps
        - Set IP addresses for server apps
        - Or leave unrestricted for development
6. **Set Up Billing (Required)**

    - Google Translate API requires a billing account
    - Go to "Billing" in the left sidebar
    - Link a billing account or create a new one
    - **Note**: Google provides free credits for new accounts
7. **Add to Your Environment**

    ```
    GOOGLE_TRANSLATE_API_KEY=AIzaSyD...your-actual-key-here

    ```

#### Pricing Information

[](#pricing-information)

- **Free Tier**: $300 in free credits for new Google Cloud accounts
- **Cost**: $20 per 1 million characters translated
- **Monthly Free Usage**: First 500,000 characters per month are free
- Check current pricing at [Google Cloud Pricing](https://cloud.google.com/translate/pricing)

#### API Limits

[](#api-limits)

- **Default Quota**: 1,000,000 characters per 100 seconds
- **Character Limit**: 30,000 characters per request
- **Rate Limit**: Can be increased by requesting quota increases

### 🧪 **Testing Service**

[](#-testing-service)

#### Dummy Service

[](#dummy-service)

**For development and testing**

```
TRANSLATION_SERVICE=dummy

```

The dummy service is useful for testing and development. It simply appends the target language code to the original text.

🆓 Using FreeTranslateApi (Self-hosted, Free)
--------------------------------------------

[](#-using-freetranslateapi-self-hosted-free)

### 1. Host the API

[](#1-host-the-api)

- Clone
- Follow its README to run locally (`npm install && npm start`) or deploy to a server.

### 2. Configure the Package

[](#2-configure-the-package)

- In your `.env` file, set:

    ```
    TRANSLATION_SERVICE=freetranslateapi
    FREETRANSLATEAPI_URL=http://localhost:5000

    ```

    (Change the URL if hosted elsewhere.)
- In `config/translation-sync.php`, ensure:

    ```
    'service' => env('TRANSLATION_SERVICE', 'freetranslateapi'),

    'services' => [
        // ...existing code...
        'freetranslateapi' => [
            'url' => env('FREETRANSLATEAPI_URL', 'http://localhost:5000'),
        ],
    ],

    ```

### 3. Usage

[](#3-usage)

- Run your translation sync commands as usual.
- The package will use your FreeTranslateApi instance for translations.

### 4. Notes

[](#4-notes)

- FreeTranslateApi is free and self-hosted, so you control usage and limits.
- Make sure your API server is running and accessible from your Laravel app.

Supported Translation Patterns
------------------------------

[](#supported-translation-patterns)

The package automatically detects these translation patterns in your code:

- `__('key')` - Laravel's `__()` helper
- `@lang('key')` - Blade directive
- `trans('key')` - Laravel's `trans()` helper
- `Lang::get('key')` - Facade usage

File Formats
------------

[](#file-formats)

### JSON Format (default)

[](#json-format-default)

```
{
    "Welcome": "Bienvenido",
    "Hello World": "Hola Mundo"
}

```

### PHP Format

[](#php-format)

```
