Skip to content

Commit

Permalink
docs: update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
WovenCoast committed Feb 24, 2025
1 parent d4f0c31 commit f1911c4
Show file tree
Hide file tree
Showing 13 changed files with 304 additions and 10 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,55 @@


## Introduction

Adds multi-lingual to Laravel models

To get started with this package, you can simply add `DbTranslatableSchema::columns($table);` or `JsonTranslatableSchema::columns($table);` to your migration `up` function.

```php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
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');
}
};
```

And then all you need to do is add the `Translatable` implementation using the `IsDbTranslatable` or `IsJsonTranslatable` trait.

```php
...

class Post extends Model implements Translatable
{
use IsDbTranslatable;

...
```

Now, your models will automatically be translated according to the current `app()->getLocale()`. To add different translations, all you need to do is

```php
// to add title for dv language
$post->title_dv = "Mee Dhivehi title eh";
```

## Documentation

You'll find the documentation on [https://docs.javaabu.com/docs/translatable](https://docs.javaabu.com/docs/translatable).
Expand Down
2 changes: 1 addition & 1 deletion docs/about-us.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: About Us
sidebar_position: 1.6
sidebar_position: 1.7
---

[Javaabu](https://javaabu.com) is a web design agency based in the Maldives.
Expand Down
11 changes: 11 additions & 0 deletions docs/advanced-usage/_category_.json
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 docs/basic-usage/10-setting-up-your-migration-and-model.md
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!
30 changes: 30 additions & 0 deletions docs/basic-usage/20-fetching-translations.md
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.
22 changes: 22 additions & 0 deletions docs/basic-usage/30-adding-translations.md
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
```

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 |
5 changes: 0 additions & 5 deletions docs/basic-usage/how-to-use-feature.md

This file was deleted.

2 changes: 1 addition & 1 deletion docs/changelog.md
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)
27 changes: 26 additions & 1 deletion docs/installation-and-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,30 @@ php artisan vendor:publish --provider="Javaabu\Translatable\TranslatableServiceP
This is the default content of the config file:

```php
// TODO
return [
/*
|--------------------------------------------------------------------------
| Some config option
|--------------------------------------------------------------------------
|
| Give a description of what each config option is like this
|
*/

'fields_ignored_for_translation' => [
'id',
'lang',
'created_at',
'updated_at',
'deleted_at',
],
'allowed_translation_locales' => [
'en' => 'English',
'dv' => 'Dhivehi',
'jp' => 'Japanese',
],
'lang_suffix_should_fallback' => false,
];


```
60 changes: 59 additions & 1 deletion docs/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,62 @@ This package is currently under development. If anything works, that's a surpris

:::

[Translatable](https://github.com/Javaabu/translatable) Adds multi-lingual to Laravel models.
[Translatable](https://github.com/Javaabu/translatable) adds multi-lingual to Laravel models.

To get started with this package, you can simply add `DbTranslatableSchema::columns($table);` to your migration `up` function.

```php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
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');
}
};
```

:::danger

This function is not currently implemented but is in the plans at this moment.

> We also provide a `DBTranslatableSchema::revert($table)` function to put in the `down` function, this isn't necessary but it's good to have.
:::

And then all you need to do is add the `Translatable` implementation using the `IsDbTranslatable` or `IsJsonTranslatable` trait.

```php
...

class Post extends Model implements Translatable
{
use IsDbTranslatable;

...
```

> Differences between `IsDbTranslatable` and `IsJsonTranslatable` are listed in []
Now, your models will automatically be translated according to the current `app()->getLocale()`. To add different translations, all you need to do is

```php
// to add title for dv language
$post->title_dv = "Mee Dhivehi title eh";
```

> If adding translations give an error, make sure the locale is allowed in `allowed_translation_locales` in `config/translatable.php`. Check out [Installation and Setup > Publishing the config file](./installation-and-setup.md#publishing-the-config-file) for information on how to setup your config file.
2 changes: 1 addition & 1 deletion docs/questions-and-security.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Questions and Security
sidebar_position: 1.4
sidebar_position: 1.5
---

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.
Expand Down
9 changes: 9 additions & 0 deletions src/DbTranslatable/DbTranslatableSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,13 @@ public static function columns(Blueprint $table): void

$table->string('lang')->index();
}

public static function revert(Blueprint $table): void
{
$table->dropForeign('translatable_parent_id');
$table->dropColumn('translatable_parent_id');

$table->dropIndex('lang');
$table->dropColumn('lang');
}
}

0 comments on commit f1911c4

Please sign in to comment.