{"id":21204021,"url":"https://github.com/s-shiryaev/laravel-translatable","last_synced_at":"2025-07-10T07:30:36.084Z","repository":{"id":42972172,"uuid":"386039503","full_name":"s-shiryaev/laravel-translatable","owner":"s-shiryaev","description":"Trait for implementing model localization","archived":false,"fork":false,"pushed_at":"2023-06-30T16:29:17.000Z","size":42,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-29T11:13:47.628Z","etag":null,"topics":["eloquent","laravel","laravel-package","localization","lumen","model","php","translate"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/s-shiryaev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"s-shiryaev"}},"created_at":"2021-07-14T18:30:04.000Z","updated_at":"2024-02-16T13:49:27.000Z","dependencies_parsed_at":"2024-11-20T20:30:35.949Z","dependency_job_id":"7dd4c962-9468-4f08-840c-89b649c79066","html_url":"https://github.com/s-shiryaev/laravel-translatable","commit_stats":{"total_commits":11,"total_committers":4,"mean_commits":2.75,"dds":0.4545454545454546,"last_synced_commit":"1c5d668878124a31b4a51d57a2729cf365cd7f27"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/s-shiryaev/laravel-translatable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-shiryaev%2Flaravel-translatable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-shiryaev%2Flaravel-translatable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-shiryaev%2Flaravel-translatable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-shiryaev%2Flaravel-translatable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/s-shiryaev","download_url":"https://codeload.github.com/s-shiryaev/laravel-translatable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/s-shiryaev%2Flaravel-translatable/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264545157,"owners_count":23625403,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eloquent","laravel","laravel-package","localization","lumen","model","php","translate"],"created_at":"2024-11-20T20:28:39.214Z","updated_at":"2025-07-10T07:30:35.819Z","avatar_url":"https://github.com/s-shiryaev.png","language":"PHP","funding_links":["https://github.com/sponsors/s-shiryaev"],"categories":[],"sub_categories":[],"readme":"# Trait for implementing model localization\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/s-shiryaev/laravel-translatable.svg?style=flat-square)](https://packagist.org/packages/s-shiryaev/laravel-translatable)\n[![Tests](https://github.com/s-shiryaev/laravel-translatable/actions/workflows/run-tests.yml/badge.svg?branch=master)](https://github.com/s-shiryaev/laravel-translatable/actions/workflows/run-tests.yml)\n[![Check \u0026 fix styling](https://github.com/s-shiryaev/laravel-translatable/actions/workflows/php-cs-fixer.yml/badge.svg)](https://github.com/s-shiryaev/laravel-translatable/actions/workflows/php-cs-fixer.yml)\n[![Total Downloads](https://img.shields.io/packagist/dt/s-shiryaev/laravel-translatable.svg?style=flat-square)](https://packagist.org/packages/s-shiryaev/laravel-translatable)\n\n\nThis package contains a trait to make it easier to work with translating Eloquent models.\n\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require s-shiryaev/laravel-translatable\n```\n\n## Version Compatibility\n\n| Laravel/Lumen | Package |\n|:-------------:|:-------:|\n|      7.x      |   1.x   |\n|      8.x      |   1.x   |\n|      9.x      |   1.x   |\n|     10.x      |   2.x   | \n\n## Usage\n\nJust add the `SShiryaev\\LaravelTranslatable\\Translatable` trait to the model and create a property `translatable`, which holds an array with all the names of attributes you wish to make translatable:\n```php\n\u003c?php\n\nnamespace App\\Models;\n\nuse Illuminate\\Database\\Eloquent\\Model;\nuse SShiryaev\\LaravelTranslatable\\Translatable;\n\nclass Currency extends Model\n{\n    use Translatable;\n\n    protected $translatable = ['name', 'code'];\n\n    protected $fillable = [\n        'id',\n        'name_ru',\n        'name_en',\n        'code_ru',\n        'code_en',\n        'code_de',\n        'active',\n        'sort',\n    ];\n}\n```\n\nNow, when accessing the properties of the model, the value will be returned in accordance with the application locale:\n```php\nApp::setLocale('ru');\n$currency = new Currency(['name_ru' =\u003e 'Доллар', 'name_en' =\u003e 'Dollar']);\necho $currency-\u003ename; //Доллар\n```\n\nAlso, when converting a model and their eloquent collection to an array (for example, in presenters), field values will be returned according to the application locale:\n```php\nApp::setLocale('ru');\n\n$currency = Currency::find(1);\n$currency-\u003etoArray(); //['name' =\u003e 'Доллар', 'name_en' =\u003e 'Dollar']\n\n$currencies = Currency::all();\n$currencies-\u003etoArray(); //[0 =\u003e ['name' =\u003e 'Доллар', 'name_en' =\u003e 'Dollar']]\n```\n\nSometimes, when converting to an array, you need to get the original fields of the model without translation. This can be done by passing an optional parameter in the `toArray()` method:\n```php\n$currency = Currency::find(1);\n$currency-\u003etoArray(false); //['name_ru' =\u003e 'Доллар', 'name_en' =\u003e 'Dollar']\n\n$currencies = Currency::all();\n$currencies-\u003etoArray(false); //[0 =\u003e ['name_ru' =\u003e 'Доллар', 'name_en' =\u003e 'Dollar']]\n```\nTranslation also works with relationships.\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [Sergey Shiryaev](https://github.com/s-shiryaev)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs-shiryaev%2Flaravel-translatable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fs-shiryaev%2Flaravel-translatable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fs-shiryaev%2Flaravel-translatable/lists"}