Support multiple language resources for Laravel

Overview

Total Downloads Latest Stable Version License

Laratrans

Support multiple language resources for Laravel.

Docs

Installation

composer require lechihuy/laratrans

After you install the package successfully, open your terminal at your project and run the following command:

php artisan laratrans:install

Configuration

You can configure the package in app/config/translatable.php, please read it to detail more.

Database

...

Model

Imaginary, you need to apply translation for Post model.

First, use the Laratrans\Translatable trait in Post model file as bellow:



use Laratrans\Translatable;

class Post extends Model
{
    use Translatable;
}

Next, open your terminal and run the following command to create translation model:

php artisan make:model Translation/PostTranslation

The Translation namespace must be corresponding with translation_namespace in the configuration file. Besides, the suffix of translation model name must be Translation.

Post model need to translate title, description columns. You simple declare the translatedAttributes property in this model.

class Post extends Model
{
    /**
     * The columns are translatable.
     * 
     * @var string[]
    */
    public array $translatedAttributes = ['title', 'description'];
    
    /**
     * The relationships that should always be loaded.
     *
     * @var array
     */
    protected $with = ['translations'];
}

You need to declare eager loading to it work. The package that offers two eager loadings are translations and translation.

If you serialize a translatable model with translations eager loading, the response can be given as bellow:

{
    "id": 1,
    "created_at": "2021-10-07T06:55:46.000000Z",
    "updated_at": "2021-10-07T06:55:46.000000Z",
    "title": "Xin chào thế giới",
    "description": "Đây là bài viết đầu tiên",
    "translations": [
        {
            "post_id": 1,
            "locale": "vi",
            "title": "Xin chào thế giới",
            "description": "Đây là bài viết đầu tiên"
        },
        {
            "post_id": 1,
            "locale": "en",
            "title": "Hello world",
            "description": "This is the first post"
        }
    ]
}

Otherwise, if you use translation eager loading, the response won't contain translations property.

You also hidden a few redundant properties if you want via $hidden property in the model.

class Post extends Model
{
    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = ['post_id'];
}

License

Laratrans is open-sourced software licensed under the MIT license.

You might also like...
Easy localization for Laravel

Laravel Localization Easy i18n localization for Laravel, an useful tool to combine with Laravel localization classes. The package offers the following

[Deprecated] A Laravel package for multilingual models
[Deprecated] A Laravel package for multilingual models

This package has been deprecated. But worry not. You can use Astrotomic/laravel-translatable. Laravel-Translatable If you want to store translations o

Better translation management for Laravel

Better localization management for Laravel Introduction Keeping a project's translations properly updated is cumbersome. Usually translators do not ha

Package to manage Laravel translations locally
Package to manage Laravel translations locally

Translation Manager For Laravel Easy to use package that helps you with the translation of your Laravel application locally. Features ✅ Check all loca

Laravel translation made __('simple').
Laravel translation made __('simple').

Translation.io client for Laravel 5.5+/6/7/8 Add this package to localize your Laravel application. Use the official Laravel syntax (with PHP or JSON

🎌 Laravel Localization Helper :: Easily add translation variables from Blade templates.

LocalizationHelper Package for convenient work with Laravel's localization features and fast language files generation. Take a look at contributing.md

Manage Laravel translation files
Manage Laravel translation files

Laravel 5 Translation Manager For Laravel 4, please use the 0.1 branch! This is a package to manage Laravel translation files. It does not replace the

A GUI for managing JSON translation files in your laravel projects.
A GUI for managing JSON translation files in your laravel projects.

Laravel Language Manager Langman is a GUI for managing your JSON language files in a Laravel project. Installation Begin by installing the package thr

A Gui To Manage Laravel Translation Files
A Gui To Manage Laravel Translation Files

Lingo A file based translation manager, which unlike other Lang managers don't need a database connection to handle the translation. Installation comp

Releases(v1.0.0)
Owner
Lê Chí Huy
Lê Chí Huy
Language files manager in your artisan console.

Laravel Langman Langman is a language files manager in your artisan console, it helps you search, update, add, and remove translation lines with ease.

Mohamed Said 867 Nov 30, 2022
[virion] Language management library for automatic translation

libtranslator :: library for automatic translation ✔️ Multilingual support for plugin messages ✔️ Translation language is set according to the player

PocketMine-MP projects of PresentKim 5 Jul 29, 2022
A morphological solution for Russian and English language written completely in PHP.

Morphos A morphological solution for Russian and English language written completely in PHP. Tests & Quality: Features [✓] Inflection of Personal name

Sergey 723 Jan 4, 2023
Geographer is a PHP library that knows how any country, state or city is called in any language

Geographer Geographer is a PHP library that knows how any country, state or city is called in any language. Documentation on the official website Incl

Menara Solutions 757 Nov 24, 2022
Official PHP library for the DeepL language translation API.

deepl-php Official PHP client library for the DeepL API. The DeepL API is a language translation API that allows other computer programs to send texts

DeepL 78 Dec 23, 2022
75 languages support for Laravel 5 application based on Laravel-Lang/lang.

Laravel-lang 75 languages support for Laravel 5 application based on Laravel-Lang/lang. Features Laravel 5+ && Lumen support. Translations Publisher.

安正超 1.3k Jan 4, 2023
Easy multilingual urls and redirection support for the Laravel framework

Linguist - Multilingual urls and redirects for Laravel This package provides an easy multilingual urls and redirection support for the Laravel framewo

Tanel Tammik 189 Jul 18, 2022
Trait for multilingual resource file support

⚡ Usage This library supports MultilingualResourceTrait which can be used in PluginBase. Multilingual support of resource files is possible using this

PocketMine-MP projects of PresentKim 1 Jun 7, 2022
Provides support for message translation and localization for dates and numbers.

The I18n library provides a I18n service locator that can be used for setting the current locale, building translation bundles and translating messages. Additionally, it provides the Time and Number classes which can be used to output dates, currencies and any numbers in the right format for the specified locale.

CakePHP 26 Oct 22, 2022
List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Cashier and Laravel Nova.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

Laravel Lang 6.9k Dec 29, 2022