Skip to content

Commit f3e3763

Browse files
Gummibeergitbook-bot
authored andcommitted
GitBook: [master] 4 pages modified
1 parent 5fe3602 commit f3e3763

File tree

4 files changed

+144
-10
lines changed

4 files changed

+144
-10
lines changed

docs/README.md

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@
88

99
This is a Laravel package for translatable models. Its goal is to remove the complexity in retrieving and storing multilingual model instances. With this package you write less code, as the translations are being fetched/saved when you fetch/save your instance.
1010

11-
### Tutorials
11+
## Quick Example
1212

13-
* [How To Add Multilingual Support to Eloquent](https://laravel-news.com/how-to-add-multilingual-support-to-eloquent)
14-
* [How To Build An Efficient and SEO Friendly Multilingual Architecture For Your Laravel Application](https://mydnic.be/post/how-to-build-an-efficient-and-seo-friendly-multilingual-architecture-for-your-laravel-application)
15-
16-
### Quick Example
17-
18-
#### **Getting translated attributes**
13+
### **Getting translated attributes**
1914

2015
```php
2116
$post = Post::first();
@@ -28,7 +23,7 @@ App::setLocale('de');
2823
echo $post->title; // Mein erster Post
2924
```
3025

31-
#### **Saving translated attributes**
26+
### **Saving translated attributes**
3227

3328
```php
3429
$post = Post::first();
@@ -41,7 +36,7 @@ $post = Post::first();
4136
echo $post->translate('en')->title; // My cool post
4237
```
4338

44-
#### **Filling multiple translations**
39+
### **Filling multiple translations**
4540

4641
```php
4742
$data = [
@@ -54,3 +49,17 @@ $post = Post::create($data);
5449
echo $post->translate('fr')->title; // Mon premier post
5550
```
5651

52+
## Tutorials
53+
54+
* [How To Add Multilingual Support to Eloquent](https://laravel-news.com/how-to-add-multilingual-support-to-eloquent)
55+
* [How To Build An Efficient and SEO Friendly Multilingual Architecture For Your Laravel Application](https://mydnic.be/post/how-to-build-an-efficient-and-seo-friendly-multilingual-architecture-for-your-laravel-application)
56+
57+
## Add-Ons
58+
59+
Thanks to the community a few packages have been written to make usage of Translatable easier when working with forms:
60+
61+
* [Propaganistas/Laravel-Translatable-Bootforms](https://github.com/Propaganistas/Laravel-Translatable-Bootforms)
62+
* [TypiCMS/TranslatableBootForms](https://github.com/TypiCMS/TranslatableBootForms)
63+
64+
65+

docs/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
* [Installation](usage/installation.md)
1111
* [Methods](usage/methods.md)
1212
* [Scopes](usage/scopes.md)
13+
* [Fallback locale](usage/fallback-locale.md)
1314

docs/usage/fallback-locale.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Fallback locale
2+
3+
## App wide
4+
5+
If you want to fallback to a default translation when a translation has not been found, enable this in the configuration using the `use_fallback` key. And to select the default locale, use the `fallback_locale` key.
6+
7+
```php
8+
[
9+
// ...
10+
'use_fallback' => true,
11+
'fallback_locale' => 'en',
12+
// ...
13+
]
14+
```
15+
16+
## per Model
17+
18+
You can also define per-model the default for "if fallback should be used", by setting the `$useTranslationFallback`property:
19+
20+
```php
21+
class Post extends Model
22+
{
23+
public $useTranslationFallback = true;
24+
}
25+
```
26+
27+
## for Properties
28+
29+
Even though we try having all models nicely translated, some fields might left empty. What's the result? You end up with missing translations for those fields!
30+
31+
The property fallback feature is here to help. When enabled, translatable will return the value of the fallback language for those empty properties.
32+
33+
The feature is enabled by default on new installations. If your config file was setup before v7.1, make sure to add the following line to enable the feature:
34+
35+
```php
36+
'use_property_fallback' => true,
37+
```
38+
39+
Of course the fallback locales must be enabled to use this feature.
40+
41+
If the property fallback is enabled in the configuration, then translatable will return the translation of the fallback locale for the fields where the translation is empty.
42+
43+
### **customize empty translation property detection**
44+
45+
This package is made to translate strings, but in general it's also able to translate numbers, bools or whatever you want to. By default a simple `empty()` call is used to detect if the translation value is empty or not. If you want to customize this or use different logic per property you can override `isEmptyTranslatableAttribute()` in your main model.
46+
47+
```php
48+
protected function isEmptyTranslatableAttribute(string $key, $value): bool
49+
{
50+
switch($key) {
51+
case 'name':
52+
return empty($value);
53+
case 'price':
54+
return !is_number($value);
55+
default:
56+
return is_null($value);
57+
}
58+
}
59+
```
60+
61+
## **Country based fallback**
62+
63+
Since version v5.3 it is possible to use country based locales. For example, you can have the following locales:
64+
65+
* English: `en`
66+
* Spanish: `es`
67+
* Mexican Spanish: `es-MX`
68+
* Colombian Spanish: `es-CO`
69+
70+
To configuration for these locales looks like this:
71+
72+
```php
73+
'locales' => [
74+
'en',
75+
'es' => [
76+
'MX',
77+
'CO',
78+
],
79+
];
80+
```
81+
82+
We can also configure the "glue" between the language and country. If for instance we prefer the format `es_MX` instead of `es-MX`, the configuration should look like this:
83+
84+
```php
85+
'locale_separator' => '_',
86+
```
87+
88+
What applies for the fallback of the locales using the `en-MX` format?
89+
90+
Let's say our fallback locale is `en`. Now, when we try to fetch from the database the translation for the locale `es-MX`but it doesn't exist, we won't get as fallback the translation for `en`. Translatable will use as a fallback `es` \(the first part of `es-MX`\) and only if nothing is found, the translation for `en` is returned.
91+

docs/usage/methods.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,39 @@ $post->getTranslationsArray();
8989
Creates a clone and clones the translations.
9090

9191
```php
92-
$replicate = $post->replicateWithTranslations();
92+
$replicate = $post->replicateWithTranslations();
9393
```
9494

95+
## getDefaultLocale\(\)
96+
97+
Returns the current default locale for the current model or `null` if no default locale is set.
98+
99+
```php
100+
$post->getDefaultLocale(); // null
101+
```
102+
103+
## setDefaultLocale\(?string $locale\)
104+
105+
Sets the default locale for the current model.
106+
107+
```php
108+
$post->setDefaultLocale('fr');
109+
$post->getDefaultLocale(); // 'fr'
110+
```
111+
112+
## Translation Autoloading
113+
114+
If the `toArray()` method is called it's possible to autoload all translations. To control this feature the package comes with a config value `to_array_always_loads_translations` and three static methods in the trait:
115+
116+
### static enableAutoloadTranslations\(\)
117+
118+
forces to load all translations
119+
120+
### static disableAutoloadTranslations\(\)
121+
122+
disables autoload and returns parent attributes
123+
124+
### static defaultAutoloadTranslations\(\)
125+
126+
does not change the default behavior logic
127+

0 commit comments

Comments
 (0)