Convert Figma logo to code with AI

barryvdh logolaravel-translation-manager

Manage Laravel translation files

1,629
418
1,629
74

Top Related Projects

A Laravel package for multilingual models

Easy localization for Laravel

Quick Overview

Laravel Translation Manager is a package for Laravel that provides a user-friendly interface for managing translations in your application. It allows developers to easily add, edit, and delete translations, as well as import and export translation files. This tool simplifies the process of managing multilingual content in Laravel projects.

Pros

  • Intuitive web interface for managing translations
  • Supports automatic translation using Google Translate API
  • Allows easy import and export of translation files
  • Provides a clear overview of missing translations

Cons

  • May require additional setup and configuration
  • Can potentially slow down the application if used extensively
  • Limited customization options for the interface
  • Requires manual publishing of translations to files

Code Examples

  1. Publishing the package configuration:
php artisan vendor:publish --provider="Barryvdh\TranslationManager\ManagerServiceProvider" --tag=config
  1. Adding the package to your routes:
Route::group(['middleware' => 'web', 'prefix' => 'translations'], function () {
    Route::get('/', '\Barryvdh\TranslationManager\Controller@getIndex');
    Route::get('view/{group}', '\Barryvdh\TranslationManager\Controller@getView');
    Route::post('add/{group}', '\Barryvdh\TranslationManager\Controller@postAdd');
    Route::post('edit/{group}', '\Barryvdh\TranslationManager\Controller@postEdit');
    Route::post('delete/{group}/{key}', '\Barryvdh\TranslationManager\Controller@postDelete');
});
  1. Using the translation manager in your views:
@extends('translation-manager::layout')

@section('content')
    {!! $manager->getIndex() !!}
@endsection

Getting Started

  1. Install the package via Composer:

    composer require barryvdh/laravel-translation-manager
    
  2. Add the service provider to your config/app.php file:

    Barryvdh\TranslationManager\ManagerServiceProvider::class,
    
  3. Publish the configuration file:

    php artisan vendor:publish --provider="Barryvdh\TranslationManager\ManagerServiceProvider" --tag=config
    
  4. Configure the package in config/translation-manager.php as needed.

  5. Add the routes to your routes/web.php file (see code example 2 above).

  6. Access the translation manager at /translations in your browser.

Competitor Comparisons

A Laravel package for multilingual models

Pros of laravel-translatable

  • Offers a more database-centric approach, storing translations in separate tables
  • Provides better performance for large-scale applications with numerous translations
  • Allows for easy addition of new languages without modifying existing database structure

Cons of laravel-translatable

  • Requires more initial setup and configuration compared to laravel-translation-manager
  • May be overkill for smaller projects with limited translation needs
  • Less intuitive for non-technical users to manage translations

Code Comparison

laravel-translatable:

use Astrotomic\Translatable\Translatable;

class Product extends Model
{
    use Translatable;

    public $translatedAttributes = ['name', 'description'];
}

laravel-translation-manager:

// No specific model changes required
// Translations are managed through language files and the web interface

laravel-translatable focuses on database-driven translations, while laravel-translation-manager uses language files and provides a web interface for management. The choice between them depends on project size, complexity, and team preferences.

Easy localization for Laravel

Pros of Laravel Localization

  • Provides route localization, allowing for easy URL-based language switching
  • Offers middleware for automatic language detection and setting
  • Includes helper functions for language-specific redirects and URL generation

Cons of Laravel Localization

  • Focuses primarily on URL-based localization, lacking translation management features
  • Does not provide a user interface for managing translations
  • May require additional setup for complex localization scenarios

Code Comparison

Laravel Localization:

Route::group(['prefix' => LaravelLocalization::setLocale()], function() {
    Route::get('/', function() {
        return view('welcome');
    });
});

Laravel Translation Manager:

Route::get('/', function() {
    return view('welcome');
});

Laravel Translation Manager focuses on managing translations within the application, while Laravel Localization emphasizes URL-based language switching and detection. The code comparison shows how Laravel Localization modifies route definitions to include language prefixes, whereas Laravel Translation Manager doesn't alter routing directly.

Laravel Translation Manager provides a web interface for managing translations, making it easier for non-developers to contribute. It also offers features like auto-translation and import/export capabilities. However, it lacks the URL-based localization features of Laravel Localization.

Choosing between these packages depends on your specific localization needs. Laravel Localization is better suited for projects requiring URL-based language switching, while Laravel Translation Manager excels in translation management and maintenance.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Laravel Translation Manager

Tests Packagist License Latest Stable Version Total Downloads Fruitcake

This is a package to manage Laravel translation files. It does not replace the Translation system, only import/export the php files to a database and make them editable through a webinterface. The workflow would be:

- Import translations: Read all translation files and save them in the database
- Find all translations in php/twig sources
- Optionally: Listen to missing translation with the custom Translator
- Translate all keys through the webinterface
- Export: Write all translations back to the translation files.

This way, translations can be saved in git history and no overhead is introduced in production.

Screenshot

Installation

Require this package in your composer.json and run composer update (or run composer require barryvdh/laravel-translation-manager directly):

composer require barryvdh/laravel-translation-manager

You need to run the migrations for this package.

php artisan vendor:publish --provider="Barryvdh\TranslationManager\ManagerServiceProvider" --tag=migrations
php artisan migrate

You need to publish the config file for this package. This will add the file config/translation-manager.php, where you can configure this package.

php artisan vendor:publish --provider="Barryvdh\TranslationManager\ManagerServiceProvider" --tag=config

In order to edit the default template, the views must be published as well. The views will then be placed in resources/views/vendor/translation-manager.

php artisan vendor:publish --provider="Barryvdh\TranslationManager\ManagerServiceProvider" --tag=views

Routes are added in the ServiceProvider. You can set the group parameters for the routes in the configuration. You can change the prefix or filter/middleware for the routes. If you want full customisation, you can extend the ServiceProvider and override the map() function.

This example will make the translation manager available at http://yourdomain.com/translations

If you would like to use auto translation using Google Translate API, install https://github.com/tanmuhittin/laravel-google-translate

composer require tanmuhittin/laravel-google-translate
php artisan vendor:publish --provider=Tanmuhittin\LaravelGoogleTranslate\LaravelGoogleTranslateServiceProvider

Middleware / Auth

The configuration file by default only includes the auth middleware, but the latests changes in Laravel 5.2 makes it that session variables are only accessible when your route includes the web middleware. In order to make this package work on Laravel 5.2, you will have to change the route/middleware setting from the default

    'route' => [
        'prefix' => 'translations',
        'middleware' => 'auth',
    ],

to

    'route' => [
        'prefix' => 'translations',
        'middleware' => [
	        'web',
	        'auth',
		],
    ],

NOTE: This is only needed in Laravel 5.2 (and up!)

Usage

Web interface

When you have imported your translation (via buttons or command), you can view them in the webinterface (on the url you defined with the controller). You can click on a translation and an edit field will popup. Just click save and it is saved :) When a translation is not yet created in a different locale, you can also just edit it to create it.

Using the buttons on the webinterface, you can import/export the translations. For publishing translations, make sure your application can write to the language directory.

You can also use the commands below.

Import command

The import command will search through app/lang and load all strings in the database, so you can easily manage them.

php artisan translations:import

Translation strings from app/lang/locale.json files will be imported to the _json group.

Note: By default, only new strings are added. Translations already in the DB are kept the same. If you want to replace all values with the ones from the files, add the --replace (or -R) option: php artisan translations:import --replace

Find translations in source

The Find command/button will look search for all php/twig files in the app directory, to see if they contain translation functions, and will try to extract the group/item names. The found keys will be added to the database, so they can be easily translated. This can be done through the webinterface, or via an Artisan command.

php artisan translations:find

If your project uses translation strings as keys, these will be stored into then _json group.

Export command

The export command will write the contents of the database back to app/lang php files. This will overwrite existing translations and remove all comments, so make sure to backup your data before using. Supply the group name to define which groups you want to publish.

php artisan translations:export <group>

For example, php artisan translations:export reminders when you have 2 locales (en/nl), will write to app/lang/en/reminders.php and app/lang/nl/reminders.php

To export translation strings as keys to JSON files , use the --json (or -J) option: php artisan translations:export --json. This will import every entries from the _json group.

Clean command

The clean command will search for all translation that are NULL and delete them, so your interface is a bit cleaner. Note: empty translations are never exported.

php artisan translations:clean

Reset command

The reset command simply clears all translation in the database, so you can start fresh (by a new import). Make sure to export your work if needed before doing this.

php artisan translations:reset

Detect missing translations

Most translations can be found by using the Find command (see above), but in case you have dynamic keys (variables/automatic forms etc), it can be helpful to 'listen' to the missing translations. To detect missing translations, we can swap the Laravel TranslationServiceProvider with a custom provider. In your config/app.php, comment out the original TranslationServiceProvider and add the one from this package:

//'Illuminate\Translation\TranslationServiceProvider',
'Barryvdh\TranslationManager\TranslationServiceProvider',

This will extend the Translator and will create a new database entry, whenever a key is not found, so you have to visit the pages that use them. This way it shows up in the webinterface and can be edited and later exported. You shouldn't use this in production, just in development to translate your views, then just switch back.

TODO

This package is still very alpha. Few things that are on the todo-list:

- Add locales/groups via webinterface
- Improve webinterface (more selection/filtering, behavior of popup after save etc)
- Seed existing languages (https://github.com/caouecs/Laravel-lang)
- Suggestions are welcome :)