Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 5e099ec

Browse files
committed
Fixing Translator usage by external Ardent
The Translator was not being properly instantiated, since the translation files were stored in the main laravel boilerplate structure. It was included in Ardent codebase and properly called (now validation errors are shown as text and not i18n keys).
1 parent 670a672 commit 5e099ec

File tree

2 files changed

+103
-4
lines changed

2 files changed

+103
-4
lines changed

src/LaravelBook/Ardent/Ardent.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Illuminate\Database\Eloquent\Model;
2222
use Illuminate\Support\Str;
2323
use Illuminate\Validation\Factory as ValidationFactory;
24+
use Symfony\Component\Translation\Loader\PhpFileLoader;
2425
use Symfony\Component\Translation\Translator;
2526

2627
/**
@@ -129,20 +130,25 @@ public function __construct( array $attributes = array() ) {
129130

130131
/**
131132
* Configures Ardent to be used outside of Laravel - correctly setting Eloquent and Validation modules.
133+
* @todo Should allow for additional language files. Would probably receive a Translator instance as an optional argument, or a list of translation files.
132134
*
133-
* @param string $language A language string as used by {@link \Symfony\Component\Translation\Translator}
134135
* @param array $connection Connection info used by {@link \Illuminate\Database\Capsule\Manager::addConnection}.
135136
* Should contain driver, host, port, database, username, password, charset and collation.
136137
*/
137-
public static function configureAsExternal( $language, array $connection ) {
138+
public static function configureAsExternal( array $connection ) {
138139
$db = new DatabaseCapsule;
139140
$db->addConnection( $connection );
140141
$db->setEventDispatcher( new Dispatcher( new Container ) );
141142
//TODO: configure a cache manager (as an option)
142143
$db->bootEloquent();
143144

145+
$translator = new Translator( 'en' );
146+
$translator->addLoader( 'file_loader', new PhpFileLoader() );
147+
$translator->addResource( 'file_loader', dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'lang'.
148+
DIRECTORY_SEPARATOR.'en'.DIRECTORY_SEPARATOR.'validation.php', 'en' );
149+
144150
self::$externalValidator = true;
145-
self::$validationFactory = new ValidationFactory( new Translator( $language ) );
151+
self::$validationFactory = new ValidationFactory( $translator );
146152
}
147153

148154
protected static function makeValidator( $data, $rules, $customMessages ) {
@@ -199,7 +205,7 @@ public function validate( array $rules = array(), array $customMessages = array(
199205
$this->validationErrors = $validator->messages();
200206

201207
// stash the input to the current session
202-
if ( Input::hasSessionStore() ) {
208+
if ( !self::$externalValidator && Input::hasSessionStore() ) {
203209
Input::flash();
204210
}
205211
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
return array(
4+
'validation' => array(
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Validation Language Lines
8+
|--------------------------------------------------------------------------
9+
|
10+
| The following language lines contain the default error messages used by
11+
| the validator class. Some of these rules have multiple versions such
12+
| such as the size rules. Feel free to tweak each of these messages.
13+
|
14+
*/
15+
16+
"accepted" => "The :attribute must be accepted.",
17+
"active_url" => "The :attribute is not a valid URL.",
18+
"after" => "The :attribute must be a date after :date.",
19+
"alpha" => "The :attribute may only contain letters.",
20+
"alpha_dash" => "The :attribute may only contain letters, numbers, and dashes.",
21+
"alpha_num" => "The :attribute may only contain letters and numbers.",
22+
"before" => "The :attribute must be a date before :date.",
23+
"between" => array(
24+
"numeric" => "The :attribute must be between :min - :max.",
25+
"file" => "The :attribute must be between :min - :max kilobytes.",
26+
"string" => "The :attribute must be between :min - :max characters.",
27+
),
28+
"confirmed" => "The :attribute confirmation does not match.",
29+
"date" => "The :attribute is not a valid date.",
30+
"date_format" => "The :attribute does not match the format :format.",
31+
"different" => "The :attribute and :other must be different.",
32+
"digits" => "The :attribute must be :digits digits.",
33+
"digits_between" => "The :attribute must be between :min and :max digits.",
34+
"email" => "The :attribute format is invalid.",
35+
"exists" => "The selected :attribute is invalid.",
36+
"image" => "The :attribute must be an image.",
37+
"in" => "The selected :attribute is invalid.",
38+
"integer" => "The :attribute must be an integer.",
39+
"ip" => "The :attribute must be a valid IP address.",
40+
"max" => array(
41+
"numeric" => "The :attribute may not be greater than :max.",
42+
"file" => "The :attribute may not be greater than :max kilobytes.",
43+
"string" => "The :attribute may not be greater than :max characters.",
44+
),
45+
"mimes" => "The :attribute must be a file of type: :values.",
46+
"min" => array(
47+
"numeric" => "The :attribute must be at least :min.",
48+
"file" => "The :attribute must be at least :min kilobytes.",
49+
"string" => "The :attribute must be at least :min characters.",
50+
),
51+
"not_in" => "The selected :attribute is invalid.",
52+
"numeric" => "The :attribute must be a number.",
53+
"regex" => "The :attribute format is invalid.",
54+
"required" => "The :attribute field is required.",
55+
"required_if" => "The :attribute field is required when :other is :value.",
56+
"required_with" => "The :attribute field is required when :values is present.",
57+
"required_without" => "The :attribute field is required when :values is not present.",
58+
"same" => "The :attribute and :other must match.",
59+
"size" => array(
60+
"numeric" => "The :attribute must be :size.",
61+
"file" => "The :attribute must be :size kilobytes.",
62+
"string" => "The :attribute must be :size characters.",
63+
),
64+
"unique" => "The :attribute has already been taken.",
65+
"url" => "The :attribute format is invalid.",
66+
67+
/*
68+
|--------------------------------------------------------------------------
69+
| Custom Validation Language Lines
70+
|--------------------------------------------------------------------------
71+
|
72+
| Here you may specify custom validation messages for attributes using the
73+
| convention "attribute.rule" to name the lines. This makes it quick to
74+
| specify a specific custom language line for a given attribute rule.
75+
|
76+
*/
77+
78+
'custom' => array(),
79+
80+
/*
81+
|--------------------------------------------------------------------------
82+
| Custom Validation Attributes
83+
|--------------------------------------------------------------------------
84+
|
85+
| The following language lines are used to swap attribute place-holders
86+
| with something more reader friendly such as E-Mail Address instead
87+
| of "email". This simply helps us make messages a little cleaner.
88+
|
89+
*/
90+
91+
'attributes' => array(),
92+
)
93+
);

0 commit comments

Comments
 (0)