generated from Javaabu/package-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d4f0c31
commit f1911c4
Showing
13 changed files
with
304 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"position": 1.4, | ||
"label": "Advanced Usage", | ||
"collapsible": true, | ||
"collapsed": false, | ||
"link": { | ||
"type": "generated-index", | ||
"slug": "/translatable/_categories/advanced-usage", | ||
"description": "Additional features available in this package." | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
docs/basic-usage/10-setting-up-your-migration-and-model.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
--- | ||
title: Set up your Migration and Model | ||
sidebar_position: 10 | ||
--- | ||
|
||
:::info | ||
|
||
Translatables currently provides **two** different types of translatables, `Db` and `Json`. Check out [Advanced Usage > Difference between DB and JSON translatable](./70-difference-isdbtranslatable-isjsontranslatable) to learn the differences and design considerations for both | ||
|
||
::: | ||
|
||
## Setting up your migrations | ||
|
||
If you are setting up a new model, you can simply add either `DbTranslatableSchema::columns($table);` or `JsonTranslatableSchema::columns($table);` into your migration schema create function. | ||
|
||
```php | ||
use Javaabu\Translatable\DbTranslatable\DbTranslatableSchema; | ||
|
||
return new class extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::create('posts', function (Blueprint $table) { | ||
$table->id(); | ||
|
||
// ... | ||
|
||
DbTranslatableSchema::columns($table); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('posts'); | ||
} | ||
}; | ||
``` | ||
|
||
Or if you have already made the model, you can write a migration to add the columns to the existing table. | ||
|
||
```php | ||
use Javaabu\Translatable\DbTranslatable\DbTranslatableSchema; | ||
|
||
return new class extends Migration { | ||
public function up(): void | ||
{ | ||
Schema::create('posts', function (Blueprint $table) { | ||
DbTranslatableSchema::columns($table); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::table('posts', function (Blueprint $table) { | ||
DbTranslatableSchema::revert($table); | ||
}); | ||
} | ||
}; | ||
``` | ||
|
||
:::danger | ||
|
||
The revert function is **unstable** as the testing suite for this has not been written yet. Use it with caution. | ||
|
||
::: | ||
|
||
## Setting up your models | ||
|
||
|
||
All you need to do is add the `Translatable` implementation using the `IsDbTranslatable` or `IsJsonTranslatable` trait. | ||
|
||
```php | ||
... | ||
use Javaabu\Translatable\DbTranslatable\IsDbTranslatable; | ||
use Javaabu\Translatable\Translatable; | ||
|
||
class Post extends Model implements Translatable | ||
{ | ||
use IsDbTranslatable; | ||
|
||
... | ||
``` | ||
|
||
Once this is setup, you are good to go! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: Fetching Translations | ||
sidebar_position: 20 | ||
--- | ||
|
||
There are multiple ways to use locale translated attributes within your code. | ||
|
||
## Using application locale | ||
|
||
If you use `app()->setLocale('dv');` then translatables will automatically detect this and give you the translated attribute if available. | ||
|
||
```php | ||
app()->setLocale('dv'); | ||
|
||
// ... | ||
|
||
// assuming you have added the translation | ||
$post->title // Mee dhivehi title eh | ||
``` | ||
|
||
## Using language code suffixes | ||
|
||
If you need a specific locale, you can use the language code suffix to always get that locale. | ||
|
||
```php | ||
$post->title_dv // Mee dhivehi title eh | ||
$post->title_ar // null | ||
``` | ||
|
||
By default, the language code suffix does not fallback to default locale. To change this behaviour, change the `lang_suffix_should_fallback` to true in the [config](../installation-and-setup.md#publishing-the-config-file) file. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
--- | ||
title: Adding Translations | ||
sidebar_position: 30 | ||
--- | ||
|
||
Translations can be added using the `addTranslation` / `addTranslations` methods available on models with Translatables. | ||
|
||
```php | ||
// add translation for one attribute | ||
$post->addTranslation('dv', 'title', 'Mee dhivehi title eh'); | ||
$post->addTranslation('dv', 'body', 'Mee dhivehi liyumeh'); | ||
|
||
// add translations for multiple attributes at once | ||
$post->addTranslations('dv', [ | ||
'title' => 'Mee dhivehi title eh', | ||
'body' => 'Mee dhivehi liyumeh' | ||
]); | ||
|
||
$post->title_dv // Mee dhivehi title eh | ||
$post->body_dv // Mee dhivehi liyumeh | ||
``` | ||
|
14 changes: 14 additions & 0 deletions
14
docs/basic-usage/70-difference-isdbtranslatable-isjsontranslatable.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
--- | ||
title: Difference between DB and JSON translatable | ||
sidebar_position: 70 | ||
--- | ||
|
||
Functionally, both `IsDbTranslatable` and `IsJsonTranslatable` both implement all functions of `Translatable`, there are no differences in syntax when using either of these. | ||
|
||
But there are a few notable differences in how each type implements `Translatables`. Depending on the use case and scale of the application, one may make more sense than the other. | ||
|
||
| IsDbTranslatable | IsJsonTranslatable | | ||
|---------------------------------------------------------|-------------------------------------------------------------| | ||
| - Works by creating additional rows for each language | - Works by adding a `translatables` JSON field to every row | | ||
| - Additional queries required to check if locale exists | - No additional queries needed for translating | | ||
| - Indexable by default | - Needs to search through JSON field | |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
--- | ||
title: Changelog | ||
sidebar_position: 1.5 | ||
sidebar_position: 1.6 | ||
--- | ||
|
||
All notable changes to this package are documented on [GitHub](https://github.com/Javaabu/translatable/blob/main/CHANGELOG.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters