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

Commit

Permalink
fix tests and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
themsaid committed Feb 18, 2016
1 parent c975a70 commit fcebd1e
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 22 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ That's all, you are now good to go.

# Usage

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:
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:

```php
<?php
Expand All @@ -55,11 +55,13 @@ class Country extends Model

protected $table = 'countries';
public $translatable = ['name'];
public $casts = ['name' => 'array'];
}
```

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

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

Expand Down
15 changes: 9 additions & 6 deletions src/MultilingualServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Themsaid\Multilingual;

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

$this->publishes([
__DIR__ . '/config/multilingual.php' => config_path('multilingual.php'),
__DIR__.'/config/multilingual.php' => config_path('multilingual.php'),
]);


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

// Get only the locales that has a value and exists in
// the system locales array
// Get only the locales that has a value and exists in the system locales array
$locales = array_filter(array_keys($value), function ($locale) use ($value, $systemLocales) {
return @$value[$locale] && in_array($locale, $systemLocales);
});

foreach ($systemLocales as $systemLocale) {
if ( ! in_array($systemLocale, $locales))
if (! in_array($systemLocale, $locales)) {
return false;
}
}

return true;
Expand All @@ -47,7 +50,7 @@ public function boot()
public function register()
{
$this->mergeConfigFrom(
__DIR__ . '/config/multilingual.php', 'multilingual'
__DIR__.'/config/multilingual.php', 'multilingual'
);
}
}
2 changes: 1 addition & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function test_database_setup()
*/
protected function getEnvironmentSetUp($app)
{
$app['config']->set('multilingual.localed', ['en', 'sp']);
$app['config']->set('multilingual.locales', ['en', 'sp']);
$app['config']->set('multilingual.fallback_locale', 'en');

$app['config']->set('database.default', 'mysql');
Expand Down
14 changes: 0 additions & 14 deletions tests/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ class ValidationTest extends TestCase
{
public function test_validation_fails_if_required_but_not_provided()
{
config(['multilingual.locales' => ['en','sp']]);

$validator = Validator::make(
['name' => ''],
['name' => 'translatable_required']
Expand All @@ -19,8 +17,6 @@ public function test_validation_fails_if_required_but_not_provided()

public function test_validation_fails_if_required_but_empty_array()
{
config(['multilingual.locales' => ['en','sp']]);

$validator = Validator::make(
['name' => []],
['name' => 'translatable_required']
Expand All @@ -31,8 +27,6 @@ public function test_validation_fails_if_required_but_empty_array()

public function test_validation_fails_if_required_but_string()
{
config(['multilingual.locales' => ['en','sp']]);

$validator = Validator::make(
['name' => 'This is not cool'],
['name' => 'translatable_required']
Expand All @@ -43,8 +37,6 @@ public function test_validation_fails_if_required_but_string()

public function test_validation_fails_if_required_and_has_correct_keys_but_empty_values()
{
config(['multilingual.locales' => ['en','sp']]);

$validator = Validator::make(
['name' => ['en' => '']],
['name' => 'translatable_required']
Expand All @@ -55,8 +47,6 @@ public function test_validation_fails_if_required_and_has_correct_keys_but_empty

public function test_validation_fails_if_required_and_has_missing_translations()
{
config(['multilingual.locales' => ['en','sp']]);

$validator = Validator::make(
['name' => ['en' => 'One']],
['name' => 'translatable_required']
Expand All @@ -67,8 +57,6 @@ public function test_validation_fails_if_required_and_has_missing_translations()

public function test_validation_fails_if_required_and_has_empty_translations()
{
config(['multilingual.locales' => ['en','sp']]);

$validator = Validator::make(
['name' => ['en' => 'One', 'sp' => '']],
['name' => 'translatable_required']
Expand All @@ -79,8 +67,6 @@ public function test_validation_fails_if_required_and_has_empty_translations()

public function test_validation_succeed_if_required_and_OK()
{
config(['multilingual.locales' => ['en','sp']]);

$validator = Validator::make(
['name' => ['en' => 'One', 'sp' => 'Uno']],
['name' => 'translatable_required']
Expand Down

0 comments on commit fcebd1e

Please sign in to comment.