Skip to content

Commit 9a6e535

Browse files
committed
add documentation and license
1 parent 53a9e7c commit 9a6e535

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 jetcod
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Laravel Translation
2+
3+
Laravel Translation is a package that provides a simple and efficient way to manage translations in your Laravel applications. It allows you to store translations of all your models attributes in database, making it easy to manage and update translations without modifying language files.
4+
5+
## Installation
6+
7+
You can install the package via Composer:
8+
9+
```bash
10+
composer require jetcod/laravel-translation
11+
```
12+
13+
## Configuration
14+
After installing the package, you need to publish the configuration file and migration:
15+
16+
```bash
17+
php artisan vendor:publish --tag=translation-config
18+
php artisan vendor:publish --tag=translation-migrations
19+
```
20+
21+
Then, run the migration to create the translations table:
22+
23+
```bash
24+
php artisan migrate
25+
```
26+
27+
In order to avoid conflictig with other packages and database tables, you can customize your database table name in the `config/translation.php` config file:
28+
29+
```php
30+
return [
31+
'database' => [
32+
'prefix' => env('TRANSLATION_TABLE_PREFIX', 'lt_'),
33+
'table_name' => env('TRANSLATION_TABLE_NAME', 'translations'),
34+
],
35+
];
36+
```
37+
38+
## Usage
39+
40+
### Setup model
41+
First, you need to import the `Jetcode\Laravel\Translation\Traits\HasTranslations` trait in your model and specify the fields you want to translate:
42+
43+
```php
44+
use Jetcode\Laravel\Translation\Traits\HasTranslations;
45+
46+
class Post extends Model
47+
{
48+
use HasTranslations;
49+
50+
protected const TRANSLATABLE_ATTRIBUTES = ['title', 'content'];
51+
}
52+
```
53+
Alternatively, you can specify the fields in a method in your model class:
54+
55+
```php
56+
use Jetcode\Laravel\Translation\Traits\HasTranslations;
57+
58+
class Post extends Model
59+
{
60+
use HasTranslations;
61+
62+
protected function getTranslatableAttributes()
63+
{
64+
return ['title', 'content'];
65+
}
66+
}
67+
```
68+
69+
> **NOTE**: The `TRANSLATABLE_ATTRIBUTES` constant or the `getTranslatableAttributes` method return value can be either a string or an array of the model attribute names.
70+
71+
72+
### Create a translation
73+
Now, you can create translations for the model attributes through the defined relations:
74+
75+
```php
76+
// Create a new post with translations
77+
$post = Post::create([
78+
'title' => 'Hello World',
79+
'content' => 'This is a post',
80+
]);
81+
82+
// Create a new translation for the post
83+
$post->translation()->saveMany([
84+
new Translation([
85+
'locale' => 'fr_FR',
86+
'key' => 'title',
87+
'value' => 'Bonjour le monde',
88+
]),
89+
new Translation([
90+
'locale' => 'fr_FR',
91+
'key' => 'content',
92+
'value' => 'Ceci est un article',
93+
]),
94+
]);
95+
```
96+
97+
### Retrieve translated model
98+
This package is compatible with Laravel localization system, so the models are translated according to the current locale. All you need to do is to set the appl locale and your model will be translated automatically:
99+
100+
```php
101+
$post = Post::find(123);
102+
var_dump($post->title); // "Hello World"
103+
104+
app()->setLocale('fr_FR');
105+
var_dump($post->title); // "Bonjour le monde"
106+
```
107+
108+
## Testing
109+
The package includes tests that you can run using `PHPUnit`:
110+
111+
```bash
112+
composer test
113+
```
114+
115+
You can also run static analysis with PHPStan:
116+
117+
```bash
118+
composer phpstan
119+
```
120+
121+
## License
122+
This package is open-source software licensed under the [MIT license](LICENSE.md).

0 commit comments

Comments
 (0)