Skip to content
This repository was archived by the owner on Jan 27, 2020. It is now read-only.

Commit fcebd1e

Browse files
committed
fix tests and update readme
1 parent c975a70 commit fcebd1e

File tree

4 files changed

+13
-22
lines changed

4 files changed

+13
-22
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ That's all, you are now good to go.
3232

3333
# Usage
3434

35-
First you need to make sure that the translatable attributes has a mysql field type of TEXT, if you are building the database from a migration file you may do this:
35+
First you need to make sure that the translatable attributes has a mysql field type of text or json, if you are building the database from a migration file you may do this:
3636

3737
```php
3838
<?php
@@ -55,11 +55,13 @@ class Country extends Model
5555

5656
protected $table = 'countries';
5757
public $translatable = ['name'];
58+
public $casts = ['name' => 'array'];
5859
}
5960
```
6061

6162
- Add the `Translatable` trait to your model class
6263
- Add a public class property `$translatable` as an array that holds the names of the translatable fields in your model.
64+
- Remember to cast the translatable attribute as 'array' in the `$casts` property of the model.
6365

6466
Now our model has the `name` attribute translatable, so on creating a new Model you may specify the name field as follow:
6567

src/MultilingualServiceProvider.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Themsaid\Multilingual;
34

45
use Illuminate\Support\ServiceProvider;
@@ -17,22 +18,24 @@ public function boot()
1718
$systemLocales = config('multilingual.locales');
1819

1920
$this->publishes([
20-
__DIR__ . '/config/multilingual.php' => config_path('multilingual.php'),
21+
__DIR__.'/config/multilingual.php' => config_path('multilingual.php'),
2122
]);
2223

2324

2425
$this->app['validator']->extendImplicit('translatable_required', function ($attribute, $value, $parameters) use ($systemLocales) {
25-
if ( ! is_array($value)) return false;
26+
if (! is_array($value)) {
27+
return false;
28+
}
2629

27-
// Get only the locales that has a value and exists in
28-
// the system locales array
30+
// Get only the locales that has a value and exists in the system locales array
2931
$locales = array_filter(array_keys($value), function ($locale) use ($value, $systemLocales) {
3032
return @$value[$locale] && in_array($locale, $systemLocales);
3133
});
3234

3335
foreach ($systemLocales as $systemLocale) {
34-
if ( ! in_array($systemLocale, $locales))
36+
if (! in_array($systemLocale, $locales)) {
3537
return false;
38+
}
3639
}
3740

3841
return true;
@@ -47,7 +50,7 @@ public function boot()
4750
public function register()
4851
{
4952
$this->mergeConfigFrom(
50-
__DIR__ . '/config/multilingual.php', 'multilingual'
53+
__DIR__.'/config/multilingual.php', 'multilingual'
5154
);
5255
}
5356
}

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function test_database_setup()
3636
*/
3737
protected function getEnvironmentSetUp($app)
3838
{
39-
$app['config']->set('multilingual.localed', ['en', 'sp']);
39+
$app['config']->set('multilingual.locales', ['en', 'sp']);
4040
$app['config']->set('multilingual.fallback_locale', 'en');
4141

4242
$app['config']->set('database.default', 'mysql');

tests/ValidationTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ class ValidationTest extends TestCase
77
{
88
public function test_validation_fails_if_required_but_not_provided()
99
{
10-
config(['multilingual.locales' => ['en','sp']]);
11-
1210
$validator = Validator::make(
1311
['name' => ''],
1412
['name' => 'translatable_required']
@@ -19,8 +17,6 @@ public function test_validation_fails_if_required_but_not_provided()
1917

2018
public function test_validation_fails_if_required_but_empty_array()
2119
{
22-
config(['multilingual.locales' => ['en','sp']]);
23-
2420
$validator = Validator::make(
2521
['name' => []],
2622
['name' => 'translatable_required']
@@ -31,8 +27,6 @@ public function test_validation_fails_if_required_but_empty_array()
3127

3228
public function test_validation_fails_if_required_but_string()
3329
{
34-
config(['multilingual.locales' => ['en','sp']]);
35-
3630
$validator = Validator::make(
3731
['name' => 'This is not cool'],
3832
['name' => 'translatable_required']
@@ -43,8 +37,6 @@ public function test_validation_fails_if_required_but_string()
4337

4438
public function test_validation_fails_if_required_and_has_correct_keys_but_empty_values()
4539
{
46-
config(['multilingual.locales' => ['en','sp']]);
47-
4840
$validator = Validator::make(
4941
['name' => ['en' => '']],
5042
['name' => 'translatable_required']
@@ -55,8 +47,6 @@ public function test_validation_fails_if_required_and_has_correct_keys_but_empty
5547

5648
public function test_validation_fails_if_required_and_has_missing_translations()
5749
{
58-
config(['multilingual.locales' => ['en','sp']]);
59-
6050
$validator = Validator::make(
6151
['name' => ['en' => 'One']],
6252
['name' => 'translatable_required']
@@ -67,8 +57,6 @@ public function test_validation_fails_if_required_and_has_missing_translations()
6757

6858
public function test_validation_fails_if_required_and_has_empty_translations()
6959
{
70-
config(['multilingual.locales' => ['en','sp']]);
71-
7260
$validator = Validator::make(
7361
['name' => ['en' => 'One', 'sp' => '']],
7462
['name' => 'translatable_required']
@@ -79,8 +67,6 @@ public function test_validation_fails_if_required_and_has_empty_translations()
7967

8068
public function test_validation_succeed_if_required_and_OK()
8169
{
82-
config(['multilingual.locales' => ['en','sp']]);
83-
8470
$validator = Validator::make(
8571
['name' => ['en' => 'One', 'sp' => 'Uno']],
8672
['name' => 'translatable_required']

0 commit comments

Comments
 (0)