Skip to content

Commit 98b96aa

Browse files
committed
updated docs
1 parent 8f2ac32 commit 98b96aa

File tree

5 files changed

+159
-71
lines changed

5 files changed

+159
-71
lines changed

docs/contributing-to-docs.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
---
2-
title: Contributing to the docs
2+
title: Contributing to the Docs
33
sidebar_position: 99
44
---
55

6-
Should you come across any glitches in our documentation, your discerning eye is much appreciated. If you've got a solution itching to be implemented, feel free to submit a PR – we welcome your contribution.
6+
# Contributing to the Docs
7+
8+
We believe that documentation is just as important as code, and we're always looking for ways to improve it. If you find any issues with our documentation, or if you have any suggestions for how we can make it better, we'd love to hear from you.
9+
10+
If you're interested in contributing to the documentation, you can do so by submitting a pull request to the [Translatable repository on GitHub](https://github.com/Javaabu/translatable). The documentation is located in the `/docs` directory, and it's written in Markdown.
11+
12+
We use [Docusaurus](https://docusaurus.io) to generate our documentation website, so you can use all of the features that Docusaurus provides, such as:
713

8-
For your reference, each project's documentation resides snugly in the /docs folder. That's the hub where everything documentation-related unfolds. Just so you're in the loop, we employ [Docusaurus](https://docusaurus.io) to transform those markdown files into the HTML served to the public. And since we are using Docusaurus, there's a lot of special magic that you can use to make the docs even better. Here are a few resources that will be useful:
914
- [Markdown Front Matter](https://docusaurus.io/docs/markdown-features#front-matter)
1015
- [Admonitions](https://docusaurus.io/docs/markdown-features/admonitions)
11-
- [Sidebar Metadata](https://docusaurus.io/docs/sidebar/autogenerated#autogenerated-sidebar-metadata)
16+
- [Sidebar Metadata](https://docusaurus.io/docs/sidebar/autogenerated#autogenerated-sidebar-metadata)
17+
18+
We appreciate your help in making our documentation the best it can be!

docs/installation-and-setup.md

Lines changed: 113 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,54 @@ title: Installation & Setup
33
sidebar_position: 1.2
44
---
55

6-
# Installation
6+
# Installation & Setup
77

8-
You can install the package via composer:
8+
Getting started with Translatable is a straightforward process. This guide will walk you through installing the package, configuring it, and setting up your models to be translatable.
9+
10+
## 1. Installation
11+
12+
First, pull the package into your project using Composer:
913

1014
```bash
1115
composer require javaabu/translatable
1216
```
1317

14-
# Publishing the config file
18+
Laravel's package auto-discovery feature will automatically register the necessary service provider.
19+
20+
## 2. Publishing Assets
21+
22+
Next, you'll need to publish the package's assets. This includes migrations, configuration files, and optional views.
23+
24+
### Migrations
25+
26+
Publish the migrations file, which will add a `languages` table to your database:
27+
28+
```bash
29+
php artisan vendor:publish --provider="Javaabu\Translatable\TranslatableServiceProvider" --tag="translatable-migrations"
30+
```
31+
32+
After publishing, run the migration to create the table:
33+
34+
```bash
35+
php artisan migrate
36+
```
37+
38+
### Configuration File (Optional)
1539

16-
Publishing the config file is optional:
40+
If you need to customize the package's behavior, you can publish the configuration file:
1741

1842
```bash
1943
php artisan vendor:publish --provider="Javaabu\Translatable\TranslatableServiceProvider" --tag="translatable-config"
2044
```
2145

22-
This is the default content of the config file:
46+
This will create a `config/translatable.php` file. Here's an overview of the available options:
2347

24-
```php
48+
```php title="config/translatable.php"
2549
return [
26-
// Allows for custom models
27-
'language_model' => \Javaabu\Translatable\Models\Language::class,
50+
// Specify a custom model for languages if you need to extend the default one.
51+
'language_model' => Javaabu\Translatable\Models\Language::class,
2852

29-
// Standard fields to be ignored for translation on all models
53+
// Define fields that should never be translated on any model.
3054
'fields_ignored_for_translation' => [
3155
'id',
3256
'lang',
@@ -35,42 +59,63 @@ return [
3559
'deleted_at',
3660
],
3761

38-
// Whether attr_dv should fall back to app locale if translations do not exist
39-
'lang_suffix_should_fallback' => false,
62+
// Set to `true` if you want suffixed attributes (e.g., `title_dv`) to fall back
63+
// to the default application locale if a translation doesn't exist.
64+
'lang_suffix_should_fallback' => false,
4065

41-
// Default locale before translations are added
42-
'default_locale' => 'en',
43-
];
66+
// The default locale to use before any translations are added.
67+
'default_locale' => 'en',
4468

69+
// Cache configuration for the Language Registrar, which stores language lists.
70+
'cache' => [
71+
'expiration_time' => DateInterval::createFromDateString('24 hours'),
72+
'key' => 'translation.languages.cache',
73+
'driver' => 'default',
74+
],
4575

46-
```
76+
// CSS classes for styling the Blade components.
77+
'styles' => [
78+
'table-cell-wrapper' => 'd-flex justify-content-center align-items-center',
4779

48-
# Setup
80+
'icons' => [
81+
'add' => 'fas fa-plus',
82+
'edit' => 'fas fa-edit',
83+
],
84+
],
85+
];
86+
```
4987

88+
## 3. Making Your Models Translatable
5089

51-
Translatables currently provides **two** different types of translatables, `Db` and `Json`. Check out [Difference between DB and JSON translatable](./basic-usage/difference-isdbtranslatable-isjsontranslatable.md) to learn the differences and design considerations for both
90+
Now for the fun part! To make an Eloquent model translatable, you need to perform two steps: update its migration and add a trait to the model itself.
5291

53-
## Setting up your migrations
92+
Translatable offers two strategies for storing translations:
5493

55-
If you are setting up a new model, you can simply add either `$table->dbTranslatable();` or `$table->jsonTranslatable();` into your migration schema create function.
94+
- **Database-based (`DbTranslatable`):** Stores each language as a new row in the model's table. Better for complex queries and indexing.
95+
- **JSON-based (`JsonTranslatable`):** Stores all translations in a single JSON column. Simpler and requires fewer database queries.
5696

57-
:::warning
97+
:::tip
98+
To understand the pros and cons of each approach, check out the [Difference between DB and JSON translatable](./basic-usage/difference-isdbtranslatable-isjsontranslatable.md) page.
99+
:::
58100

59-
Use one or the other, **DON'T use both at the same time**.
101+
### Step 3.1: Update the Migration
60102

61-
:::
103+
You need to add the necessary columns to your model's table. Choose **one** of the following methods.
62104

63-
```php
64-
use Javaabu\Translatable\DbTranslatable\DbTranslatableSchema;
105+
```php title="database/migrations/your_create_model_table.php"
106+
use Illuminate\Database\Migrations\Migration;
107+
use Illuminate\Database\Schema\Blueprint;
108+
use Illuminate\Support\Facades\Schema;
65109

66110
return new class extends Migration {
67111
public function up(): void
68112
{
69113
Schema::create('posts', function (Blueprint $table) {
70114
$table->id();
115+
$table->string('author');
116+
// ... other columns
71117

72-
// ...
73-
118+
// Choose one of the following:
74119
$table->dbTranslatable();
75120
// OR
76121
$table->jsonTranslatable();
@@ -84,48 +129,59 @@ return new class extends Migration {
84129
};
85130
```
86131

87-
Or if you have already made the model, you can write a migration to add the columns to the existing table.
132+
If you have an existing table, you can create a new migration to add the columns:
88133

89134
```php
90-
use Javaabu\Translatable\DbTranslatable\DbTranslatableSchema;
91-
92-
return new class extends Migration {
93-
public function up(): void
94-
{
95-
Schema::create('posts', function (Blueprint $table) {
96-
$table->dbTranslatable();
97-
// OR
98-
$table->jsonTranslatable();
99-
});
100-
}
101-
102-
public function down(): void
103-
{
104-
Schema::table('posts', function (Blueprint $table) {
105-
$table->dropDbTranslatable();
106-
// OR
107-
$table->dropJsonTranslatable();
108-
});
109-
}
110-
};
135+
Schema::table('posts', function (Blueprint $table) {
136+
// Choose one
137+
$table->dbTranslatable();
138+
// OR
139+
$table->jsonTranslatable();
140+
});
111141
```
112142

113-
## Setting up your models
143+
### Step 3.2: Update the Model
114144

145+
Finally, implement the correct contract and add the corresponding trait to your model. Again, choose the setup that matches the migration you created.
115146

116-
All you need to do is add the `Translatable` implementation using the `IsDbTranslatable` or `IsJsonTranslatable` trait.
147+
#### For DB-based Translations
117148

118-
```php
119-
...
120-
use Javaabu\Translatable\Contracts\Translatable;use Javaabu\Translatable\DbTranslatable\IsDbTranslatable;
149+
If you used `$table->dbTranslatable()`, your model should implement the `DbTranslatable` contract and use the `IsDbTranslatable` trait.
121150

122-
class Post extends Model implements Translatable
151+
```php title="app/Models/Post.php"
152+
use Illuminate\Database\Eloquent\Model;
153+
use Javaabu\Translatable\Contracts\DbTranslatable as DbTranslatableContract;
154+
use Javaabu\Translatable\DbTranslatable\IsDbTranslatable;
155+
156+
class Post extends Model implements DbTranslatableContract
123157
{
124158
use IsDbTranslatable;
125-
// OR
159+
160+
// ... your model properties
161+
162+
// IMPORTANT: Add the fields you want to translate here
163+
public array $translatable = ['title', 'content'];
164+
}
165+
```
166+
167+
#### For JSON-based Translations
168+
169+
If you used `$table->jsonTranslatable()`, your model should implement the `JsonTranslatable` contract and use the `IsJsonTranslatable` trait.
170+
171+
```php title="app/Models/Post.php"
172+
use Illuminate\Database\Eloquent\Model;
173+
use Javaabu\Translatable\Contracts\JsonTranslatable as JsonTranslatableContract;
174+
use Javaabu\Translatable\JsonTranslatable\IsJsonTranslatable;
175+
176+
class Post extends Model implements JsonTranslatableContract
177+
{
126178
use IsJsonTranslatable;
127179

128-
...
180+
// ... your model properties
181+
182+
// IMPORTANT: Add the fields you want to translate here
183+
public array $translatable = ['title', 'content'];
184+
}
129185
```
130186

131-
Once this is setup, you are good to go!
187+
And that's it! Your model is now set up for translations. You can start adding and fetching translated attributes right away.

docs/intro.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ title: Introduction
33
sidebar_position: 1.0
44
---
55

6-
# Translatable
6+
# Introduction
77

8-
[Translatable](https://github.com/Javaabu/translatable) adds multi-lingual to Laravel models.
8+
Welcome to the documentation for Translatable, a Laravel package that adds multi-lingual support to your models.
99

10-
This package allows Laravel model attributes to be translated automatically according to the current `app()->getLocale()`.
10+
This package allows your Laravel model attributes to be translated automatically according to the current `app()->getLocale()`. This means you can easily create applications that cater to a global audience, without having to worry about the complexities of localization.
11+
12+
Here's a quick example of how it works:
1113

1214
```php
1315
app()->setLocale('en');
@@ -20,9 +22,11 @@ $post->title_en // This is an English title
2022
$post->title_dv // Mee dhivehi title eh
2123
```
2224

23-
Adding a translation is made easier as well using the language code suffix.
25+
As you can see, it's incredibly simple to get and set translations for your model attributes. You can even add new translations on the fly:
2426

2527
```php
2628
// to add title for dv language
2729
$post->title_dv = "Mee Dhivehi title eh";
2830
```
31+
32+
Throughout this documentation, we'll explore all the features of Translatable in detail, from basic usage to advanced customization. We'll also cover how to contribute to the project and where to find help if you need it.

docs/questions-and-security.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
---
2-
title: Questions and Security
2+
title: Questions & Security
33
sidebar_position: 1.5
44
---
55

6-
Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving this package? Feel free to create an [issue](../../issues) on GitHub, we'll try to address it as soon as possible.
6+
# Questions & Security
77

8-
If you've found a bug regarding security please mail [info@javaabu.com](mailto:info@javaabu.com) instead of using the issue tracker.
8+
Do you have a question? Is something not working as expected? Have you found a bug? Or maybe you have a suggestion for a new feature? We'd love to hear from you! Please feel free to create an [issue on GitHub](https://github.com/Javaabu/translatable/issues), and we'll do our best to get back to you as soon as possible.
9+
10+
## Security Vulnerabilities
11+
12+
If you discover a security vulnerability within this package, please send an e-mail to [info@javaabu.com](mailto:info@javaabu.com). All security vulnerabilities will be promptly addressed.
13+
14+
We take security very seriously, and we appreciate your help in keeping this package secure.

docs/requirements.md

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,22 @@ title: Requirements
33
sidebar_position: 1.1
44
---
55

6-
This package requires the following:
6+
# Requirements
77

8-
- Laravel 10.0 or higher
9-
- PHP 8.2 or higher
8+
Before you can start using Translatable, you'll need to make sure your development environment meets the following requirements:
9+
10+
- **Laravel:** Version 9.0 or higher
11+
- **PHP:** Version 8.2 or higher
12+
13+
Additionally, this package relies on the following dependencies:
14+
15+
- `illuminate/support`: ^9.0 || ^10.0 || ^11.0 || ^12.0
16+
- `javaabu/helpers`: ^1.61
17+
18+
And for development, you'll need:
19+
20+
- `laravel/pint`: ^1.14
21+
- `orchestra/testbench`: ^7.0 || ^8.0 || ^9.0 || ^10.0
22+
- `phpunit/phpunit`: ^9.5 || ^10.5 || ^11.5
23+
24+
We also suggest using `javaabu/forms` for the little buttons and table cells that are provided.

0 commit comments

Comments
 (0)