Multilanguage

Introduction

Thanks to the spatie/laravel-translatable package you can translate your models into multiple languages. HotCoffee's own Article and InfoPage models take advantage of this. Additionally there is a helper method language_fields() that will generate the HTML input fields in your forms in separate tabs on the page for each language that you declare in hotcoffee.php config file.

Usage

Define all the locales for the translatable models in the languages array in config/hotcoffee.php. Each language should be an associative array with this structure'acronym' => 'full name'. The first language is going to be the default one.
1
'languages' => [
2
'en' => 'English',
3
'bg' => 'Български',
4
'de' => 'Deutsche',
5
],
Copied!
If you have defined more than one language in the array you will be able to see the flag icons above the title and content fields when creating or editing info pages and articles. Clicking an icon will switch to the tab that contains the fields for that particular locale.
These fields and flag icons are generated, because we called the language_fields() helper method in the template. Flag images are loaded from the public/vendor/hotcoffee/img/flags directory. If the acronym of a locale is 'de', then the loaded image will be de.svg from that same directory. Simply put any missing flag svg files that you need there.
To make your own models translatable they should use the Spatie\Translatable\HasTranslations trait. You need to also define all translatable fields:
1
<?php
2
3
namespace App;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\Translatable\HasTranslations;
7
8
class Product extends Model
9
{
10
11
use HasTranslation;
12
13
/**
14
* Translatable attributes.
15
*/
16
public $translatable = ['name', 'description'];
17
18
/**
19
* The attributes that are mass assignable.
20
*/
21
protected $fillable = [
22
'name', 'description', 'price'
23
];
24
}
Copied!
Now in your template:
1
<form action="{{ route('products.store') }}" method="post">
2
<h6 class="heading-small text-muted mb-4">New Product</h6>
3
4
{{ csrf_field() }}
5
6
<!-- TRANSLATABLE FIELDS -->
7
{!! language_fields([
8
'name' => ['type' => 'text', 'title' => 'Product Name'],
9
'description' => ['type' => 'textarea', 'title' => 'Content', 'class' => 'tinymce'],
10
], $product ?? null) !!}
11
<!-- END TRANSLATABLE FIELDS -->
12
13
<!-- NON TRANSLATABLE FIELDS -->
14
<input type="text" name="price" />
15
<!-- END NON TRANSLATABLE FIELDS -->
16
17
<input type="submit" />
18
</form>
Copied!
The HTML output of language_fields() method in this case would look something like this (given, that we have set 3 locales - en, bg and de):
1
<!-- Flag Icons -->
2
<ul class="nav nav-pills nav-pills-circle" role="tablist" id="tabs_2">
3
4
<li class="nav-item">
5
<a class="nav-link rounded-circle active " id="en" data-toggle="tab" href="#tab-en" role="tab" aria-controls="en" aria-selected="true">
6
<img src="https://yourapp.com/vendor/hotcoffee/img/flags/en.svg" alt="English" class="flag-img">
7
</a>
8
</li>
9
10
<li class="nav-item">
11
<a class="nav-link rounded-circle " id="bg" data-toggle="tab" href="#tab-bg" role="tab" aria-controls="bg" aria-selected="true">
12
<img src="https://yourapp.com/vendor/hotcoffee/img/flags/bg.svg" alt="Български" class="flag-img">
13
</a>
14
</li>
15
16
<li class="nav-item">
17
<a class="nav-link rounded-circle " id="de" data-toggle="tab" href="#tab-de" role="tab" aria-controls="bg" aria-selected="true">
18
<img src="https://yourapp.com/vendor/hotcoffee/img/flags/de.svg" alt="Deutsche" class="flag-img">
19
</a>
20
</li>
21
22
</ul>
23
24
<!-- Tabs with language fields -->
25
<div class="tab-content" id="lang-tabs">
26
27
<!-- English Fields -->
28
<div class="tab-pane fade" id="tab-en" role="tabpanel" aria-labelledby="tab-en">
29
<label class="form-control-label" for="input-name">Name</label>
30
<input type="text" name="name[en]" class="form-control" />
31
32
<label class="form-control-label" for="input-name">Content</label>
33
<textarea name="description[en]" class="tinymce form-control"><textarea>
34
</div>
35
36
<!-- Bulgarian Fields -->
37
<div class="tab-pane fade" id="tab-bg" role="tabpanel" aria-labelledby="tab-bg">
38
<label class="form-control-label" for="input-name">Name</label>
39
<input type="text" name="name[bg]" class="form-control" />
40
41
<label class="form-control-label" for="input-name">Content</label>
42
<textarea name="description[bg]" class="tinymce form-control"><textarea>
43
</div>
44
45
<!-- German Fields -->
46
<div class="tab-pane fade" id="tab-de" role="tabpanel" aria-labelledby="tab-de">
47
<label class="form-control-label" for="input-name">Name</label>
48
<input type="text" name="name[de]" class="form-control" />
49
50
<label class="form-control-label" for="input-name">Content</label>
51
<textarea name="description[de]" class="tinymce form-control"><textarea>
52
</div>
53
</div>
Copied!
The function will also add "has-danger" class if a validation of a field has failed.
It will also populate the value="" attribute (or content of a <textarea> field) automatically when editing a model. Hence why you need to specify the variable name of the model as a second parameter:
1
{!! language_fields($fields = array(), $product ?? null !!}
Copied!
Say you have a method called edit() in your ProductController.php. In this method we get the product from the database, pass the $product variable to the template and return the product.blade.php view. However you may also have a create() method, that will return the same view. But this time we don't have to get a product from the database. We will create a new one. Hence the $product variable is not defined. To not get an error "unidentified variable name $product" when using calling language_fields() you can add$product ?? null as the second parameter.
When the form is submitted, the content of the request will be this:
Now in your store() method you can do this:
1
Product::create($request->all());
Copied!
Of course when using Eloquent's create() or update() methods you will need to specify the fillable or guarded attribute on the model.
1
public $translatable = ['name', 'description'];
2
protected $fillable = ['name', 'description', 'price'];
Copied!
Whenever you need to display the content of the product for example you can just do this:
1
$product->content; // Returns value of content field in the current app locale.
Copied!
If you want to get the content field in a specific locale:
1
$product->getTranslation('content', 'bg'); // Returns value of content field in Bulgarian.
Copied!
To learn more about translating models visit https://github.com/spatie/laravel-translatable
Last modified 1yr ago
Copy link