Download the PHP package alaaeta/translation without Composer

On this page you can find all versions of the php package alaaeta/translation. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package translation

Translation

Description

Translation is a developer friendly, database driven, automatic translator for Laravel 9. Wouldn't it be nice to just write text regularly on your application and have it automatically translated, added to the database, and cached at runtime? Take this for example:

The recommended way to install through Composer:

composer require alaaeta/translation

Controller:

public function index()
{
    return view('home.index');
}

View:

@extends('layout.default')

{{ _t('Welcome to our home page') }}

Seen:

Welcome to our home page

When you visit the page, you won't notice anything different, but if you take a look at your database, your default application locale has already been created, and the translation attached to that locale.

Now if we set locale to something different, such as Arabic (ar), it'll automatically translate it for you.

Controller:

public function index()
{
    app()->setLocale('ar');

    return view('home.index');
}

View:

@extends('layout.default')

{{ _t('Welcome to our home page') }}

Seen:

Welcome to our home page

We can even use placeholders for dynamic content:

View:

{{ _t('Welcome :name, to our home page', ['name' => 'John']) }}

Seen:

Welcome John, to our home page

Installation

Publish the config

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

Run the migrations

php artisan migrate

Your good to go!

Usage

Anywhere in your application, either use the the shorthand function (can be disabled in config file)

_t('Translate me!')

Or

Translation::translate('Translate me!')

This is typically most useful in blade views:

{{ _t('Translate me!') }}

And you can even translate models easily by just plugging in your content:

{{ _t($post->title) }}

Or use placeholders:

{{ _t('Post :title', ['title' => $post->title]) }}

In your translations database table you'll have:

| id | key | value | language_code |
  1        'Translate me!'         'Translate me!'        'en'

To switch languages for the users session, all you need to call is:

app()->setLocale('en') // Setting to Englisg locale

Locales are automatically created when you call the app()->setLocale($code) method, and when the translate function is called, it will automatically create a new translation record for the new locale, with the default locale translation. The default locale is taken from the laravel app.php config file.

this in your translations table:

| id | key | value | language_code |
  1        'Translate me!'         'Translate me!'        'en'
  1        'Translate me!'         'Translate me!'        'ar'

You can now update the translation on the new record and it will be shown wherever it's called:

_t('Translate me!')`
Need to translate a single piece of text without setting the users default locale?

Just pass in the locale into the third argument inside the translation functions show above like so:

View:

{{ _t('Our website also supports english!', [], 'en') }}

<br>

{{ _t('And arabic!', [], 'ar') }}

This is great for showing users that your site supports different languages without changing the entire site language itself. You can also perform replacements like usual:

View:

{{ _t('Hello :name, we also support french!', ['name' => 'John Doe'], 'ar') }}

Seen:

Bonjour John Doe , nous soutenons aussi le français !

Performing this will also create the locale in your database, save the translation, and cache it in case you need it again.

You must provide you're own way of updating translations (controllers/views etc) using the eloquent models provided.

Injecting Translation

As of v1.3.4 you can now inject the Translation contract into your controllers without the use of a facade:

Models

By default, translation model are included and selected inside the configuration file. If you'd like to use your own models you must create them and implement their trait. Here's an example:

The Translation Model:

use Stevebauman\Translation\Traits\TranslationTrait;
use Illuminate\Database\Eloquent\Model;

class Translation extends Model
{
    use TranslationTrait;

    /**
     * The locale translations table.
     *
     * @var string
     */
    protected $table = 'translations';

    /**
     * The fillable locale translation attributes.
     *
     * @var array
     */
    protected $fillable = [
        'key',
        'value',
        'language_code',
    ];

}

Once you've created translation model, insert them into the translation.php configuration file:

|--------------------------------------------------------------------------
| Translation Model
|--------------------------------------------------------------------------
|
|  The translation model is used for storing translations.
|
*/

'translation' => App\Models\Translation::class,

Routes

Translating your site with a locale prefix couldn't be easier. First inside your app/Http/Kernel.php file, insert the locale middleware:

/**
 * The application's route middleware.
 *
 * @var array
 */
protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,

    // Insert Locale Middleware
    'locale' => \Stevebauman\Translation\Middleware\LocaleMiddleware::class
];

Now, in your app/Http/routes.php file, insert the middleware and the following Translation method in the route group prefix like so:

Route::group(['prefix' => Translation::getRoutePrefix(), 'middleware' => ['locale']], function()
{
    Route::get('home', function ()
    {
        return view('home');
    });
});

You should now be able to access routes such as:

http://localhost/home
http://localhost/en/home
http://localhost/fr/home

All versions of translation with dependencies

PHP Build Version
Package Version
No informations.
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package alaaeta/translation contains the following files

Loading the files please wait ....