Coder Social home page Coder Social logo

laravel-translatable's Introduction

A Trait to handle Translations in Laravel

This is a Laravel package containing a trait for translatable Eloquent models. This package follows the approach to have only a single table to maintain all the translations.

This approach may not be perfect for every use case as the table can grow really big. But compared to all the other packages this approach is the most flexible as it lets you make models and its attributes translatable without extra configuration.

Alternatives to this package are following packages:

  1. Spatie/laravel-translatable saves the translatable attributes as jsons
  2. dimsav/laravel-translatable expects a new table for every new model that has translatable attributes

Requirements

This package require a minimum Laravel version of 5.7 and PHP in version 7.2

Installation

You can install the package via composer:

composer require aheenam/laravel-translatable

Now you can use this Trait on any Eloquent Model of your project.

Usage

To make your Eloquent Model translatable just add the Aheenam\Translatable\Translatable Trait to your model. Then add a public attribute $translatable as an array containing all the attributes that should be translatable.

<?php

use Illuminate\Database\Eloquent\Model;
use Aheenam\Translatable\Translatable;

class MyModel extends Model {
    
    use Translatable;
    
    public $translatable = ['place'];
    
}

Methods

The simplest version of getting an translation is to simply get the property. This will return the value of the property in the current language

// assuming $myModel is an instace of MyModel class defined above
// and the translations are set

echo $myModel->place; // returns 'Germany'

App::setLocale('de');

echo $myModel->place; // returns 'Deutschland'

You can also use

$myModel->translate('place', 'de'); // returns 'Deutschland'

Getting the translated model

Sometime you want to keep the model properties untouched but get a translated version of your model. You can reach this by using

$translatedModel = $myModel->in('de');

echo $translatedModel->place; // returns 'Deutschland'

// shorter
echo $myModel->in('de')->place; // returns 'Deutschland'

Translating an Model

You can translate a model using

$myModel->translate('no', [
    'place' => 'Tyskland'
]);

Note: The method translate() can be used to get a translation and to set a translation. The difference is what type of parameters are used. If you pass an array as the second argument, then translate() will work as a setter otherwise as a getter

Changelog

Check CHANGELOG for the changelog

Testing

To run tests use

$ composer test

Contributing

soon

Security

If you discover any security related issues, please email [email protected] or use the issue tracker of GitHub.

About Aheenam

Aheenam is a small company from NRW, Germany creating custom digital solutions. Visit our website to find out more about us.

License

The MIT License (MIT). Please see License File for more information.

laravel-translatable's People

Contributors

rathesdot avatar

Stargazers

Oktay Kopcak avatar Raido avatar Nam Nguyen avatar  avatar

Watchers

James Cloos avatar  avatar

laravel-translatable's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.