Completed
Push — master ( 5106be...84b949 )
by Chukwudiebube
05:45 queued 03:34
created
src/Middleware/LocaleMiddleware.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -19,7 +19,7 @@
 block discarded – undo
19 19
      */
20 20
     public function handle(Request $request, Closure $next)
21 21
     {
22
-        Translation::setLocale( Translation::getRoutePrefix() ?? '' );
22
+        Translation::setLocale(Translation::getRoutePrefix() ?? '');
23 23
 
24 24
         return $next($request);
25 25
     }
Please login to merge, or discard this patch.
src/TranslationServiceProvider.php 2 patches
Spacing   +5 added lines, -5 removed lines patch added patch discarded remove patch
@@ -20,8 +20,8 @@  discard block
 block discarded – undo
20 20
      */
21 21
     public function boot()
22 22
     {
23
-        Blade::directive('trans', function ( string $text, array $replacements = [], string $to_locale = '' ) {
24
-            $translation = __trans( $text, $replacements, $to_locale );
23
+        Blade::directive('trans', function(string $text, array $replacements = [], string $to_locale = '') {
24
+            $translation = __trans($text, $replacements, $to_locale);
25 25
 
26 26
             return "<?php echo $translation ?>";
27 27
         });
@@ -50,11 +50,11 @@  discard block
 block discarded – undo
50 50
         require_once __DIR__.'/helpers.php';
51 51
 
52 52
         // Bind translation to the IoC.
53
-        $this->app->bind( 'translation', function ( Application $app ) {
54
-            return new Translation( $app );
53
+        $this->app->bind('translation', function(Application $app) {
54
+            return new Translation($app);
55 55
         } );
56 56
 
57
-        register_shutdown_function( [ $this->app->translation, 'shutdown' ] );
57
+        register_shutdown_function([$this->app->translation, 'shutdown']);
58 58
     }
59 59
 
60 60
     /**
Please login to merge, or discard this patch.
Braces   +4 added lines, -1 removed lines patch added patch discarded remove patch
@@ -34,7 +34,10 @@
 block discarded – undo
34 34
      */
35 35
     public function register()
36 36
     {
37
-        if (PHP_SESSION_NONE == session_status()) session_start(); // Start Session
37
+        if (PHP_SESSION_NONE == session_status()) {
38
+            session_start();
39
+        }
40
+        // Start Session
38 41
 
39 42
         // Allow configuration to be publishable.
40 43
         $this->publishes([
Please login to merge, or discard this patch.
src/Migrations/2019_03_23_164730_create_translations_table.php 1 patch
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -13,7 +13,7 @@
 block discarded – undo
13 13
      */
14 14
     public function up()
15 15
     {
16
-        Schema::create('translations', function (Blueprint $table) {
16
+        Schema::create('translations', function(Blueprint $table) {
17 17
             $table->increments('id');
18 18
             $table->text('text');
19 19
             $table->longText('data');
Please login to merge, or discard this patch.
src/helpers.php 1 patch
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -2,7 +2,7 @@  discard block
 block discarded – undo
2 2
 
3 3
 use Illuminate\Support\Facades\App;
4 4
 
5
-if ( ! function_exists( '__trans' ) ) :
5
+if ( ! function_exists('__trans')) :
6 6
     /**
7 7
      * Shorthand function for translating text.
8 8
      *
@@ -12,7 +12,7 @@  discard block
 block discarded – undo
12 12
      *
13 13
      * @return string
14 14
      */
15
-    function __trans( string $text, array $replacements = [], string $to_locale = '' ) {
16
-        return App::make('translation')->translate( $text, $replacements, $to_locale );
15
+    function __trans(string $text, array $replacements = [], string $to_locale = '') {
16
+        return App::make('translation')->translate($text, $replacements, $to_locale);
17 17
     }
18 18
 endif;
19 19
\ No newline at end of file
Please login to merge, or discard this patch.
src/Translation.php 2 patches
Braces   +18 added lines, -6 removed lines patch added patch discarded remove patch
@@ -187,7 +187,9 @@  discard block
 block discarded – undo
187 187
             throw new InvalidArgumentException( $message );
188 188
         }
189 189
 
190
-        if ( empty( $text ) ) return $text;
190
+        if ( empty( $text ) ) {
191
+            return $text;
192
+        }
191 193
 
192 194
         // Get translation data from session or database
193 195
         $translations = (array) ($_SESSION['__translations'] ?? $this->translationModel->pluck( 'data', 'text' )->toArray());
@@ -254,16 +256,22 @@  discard block
 block discarded – undo
254 256
         $new_translations = $_SESSION['__translations'] ?? [];
255 257
         $old_translations = (array) $this->translationModel->pluck( 'data', 'text' )->toArray();
256 258
 
257
-        if ( json_encode( $new_translations ) == json_encode( $old_translations ) ) return;
259
+        if ( json_encode( $new_translations ) == json_encode( $old_translations ) ) {
260
+            return;
261
+        }
258 262
 
259 263
         $now = Carbon::now('utc')->toDateTimeString();
260 264
         $insert = $update = [];
261 265
 
262
-        if ( empty( $new_translations ) ) return;
266
+        if ( empty( $new_translations ) ) {
267
+            return;
268
+        }
263 269
 
264 270
         foreach ($new_translations as $text => $data) {
265 271
             if ( isset( $old_translations[ $text ] ) ) {
266
-                if ( $old_translations[ $text ] == $data ) continue;
272
+                if ( $old_translations[ $text ] == $data ) {
273
+                    continue;
274
+                }
267 275
 
268 276
                 // Update old
269 277
                 $this->translationModel->where( 'text', $text )->update( [ 'data' => $data ] );
@@ -279,9 +287,13 @@  discard block
 block discarded – undo
279 287
             ];
280 288
         }
281 289
 
282
-        if ( ! empty( $insert ) ) $this->translationModel->insert( $insert );
290
+        if ( ! empty( $insert ) ) {
291
+            $this->translationModel->insert( $insert );
292
+        }
283 293
 
284 294
         // Remove the translation
285
-        if ( isset( $_SESSION['__translations'] ) ) unset( $_SESSION['__translations'] );
295
+        if ( isset( $_SESSION['__translations'] ) ) {
296
+            unset( $_SESSION['__translations'] );
297
+        }
286 298
     }
287 299
 }
Please login to merge, or discard this patch.
Spacing   +48 added lines, -48 removed lines patch added patch discarded remove patch
@@ -43,10 +43,10 @@  discard block
 block discarded – undo
43 43
      * @access public
44 44
      * @var Illuminate\Contracts\Foundation\Application
45 45
      */
46
-    public function __construct( Application $app )
46
+    public function __construct(Application $app)
47 47
     {
48 48
         $this->config           = $app->make('config');
49
-        $this->request          = app( \Illuminate\Http\Request::class );
49
+        $this->request          = app(\Illuminate\Http\Request::class);
50 50
         $this->translationModel = $app->make($this->getTranslationModel());
51 51
     }
52 52
 
@@ -57,7 +57,7 @@  discard block
 block discarded – undo
57 57
      */
58 58
     public function getApiKey() : string
59 59
     {
60
-        return (string) $this->config->get( 'translation.key', '' );
60
+        return (string) $this->config->get('translation.key', '');
61 61
     }
62 62
 
63 63
     /**
@@ -69,7 +69,7 @@  discard block
 block discarded – undo
69 69
     {
70 70
         $locale = $_SESSION['locale'] ?? $this->getDefaultLocale();
71 71
 
72
-        return (string) ( empty( $locale ) ? $this->getDefaultLocale() : $locale );
72
+        return (string) (empty($locale) ? $this->getDefaultLocale() : $locale);
73 73
     }
74 74
 
75 75
     /**
@@ -79,7 +79,7 @@  discard block
 block discarded – undo
79 79
      */
80 80
     protected function getLocales() : array
81 81
     {
82
-        return (array) $this->config->get( 'translation.locales' );
82
+        return (array) $this->config->get('translation.locales');
83 83
     }
84 84
 
85 85
     /**
@@ -91,9 +91,9 @@  discard block
 block discarded – undo
91 91
      */
92 92
     protected function getDefaultLocale() : string
93 93
     {
94
-        $locale = $this->config->get( 'app.locale', $this->config->get('app.fallback_locale') );
94
+        $locale = $this->config->get('app.locale', $this->config->get('app.fallback_locale'));
95 95
 
96
-        $locale = $this->localeExist( $locale ) ? $locale : 'en';
96
+        $locale = $this->localeExist($locale) ? $locale : 'en';
97 97
 
98 98
         return (string) $locale;
99 99
     }
@@ -118,9 +118,9 @@  discard block
 block discarded – undo
118 118
      */
119 119
     public function getRoutePrefix() : string
120 120
     {
121
-        $locale = (string) $this->request->segment( $this->getRequestSegment() );
121
+        $locale = (string) $this->request->segment($this->getRequestSegment());
122 122
 
123
-        return $this->localeExist( $locale ) ? $locale : '';
123
+        return $this->localeExist($locale) ? $locale : '';
124 124
     }
125 125
 
126 126
     /**
@@ -130,7 +130,7 @@  discard block
 block discarded – undo
130 130
      */
131 131
     protected function getRequestSegment() : int
132 132
     {
133
-        return $this->config->get( 'translation.request_segment', 1 );
133
+        return $this->config->get('translation.request_segment', 1);
134 134
     }
135 135
 
136 136
     /**
@@ -139,12 +139,12 @@  discard block
 block discarded – undo
139 139
      * @param string $locale    The locale to use. Defaults to 'en'.
140 140
      * @throws InvalidArgumentException|Exception
141 141
      */
142
-    public function setLocale( string $locale = 'en' )
142
+    public function setLocale(string $locale = 'en')
143 143
     {
144
-        if ( ! empty( $locale ) && ! $this->localeExist( $locale ) ) {
144
+        if ( ! empty($locale) && ! $this->localeExist($locale)) {
145 145
             $message = 'Invalid Argument! Locale passed does not exist.';
146 146
 
147
-            throw new InvalidArgumentException( $message );
147
+            throw new InvalidArgumentException($message);
148 148
         }
149 149
 
150 150
         $_SESSION['locale'] = $locale;
@@ -157,9 +157,9 @@  discard block
 block discarded – undo
157 157
      * 
158 158
      * @return bool
159 159
      */
160
-    public function localeExist( string $locale = 'en' ) : bool
160
+    public function localeExist(string $locale = 'en') : bool
161 161
     {
162
-        return array_key_exists( $locale, (array) $this->getLocales() ) ? true : false;
162
+        return array_key_exists($locale, (array) $this->getLocales()) ? true : false;
163 163
     }
164 164
 
165 165
     /**
@@ -175,58 +175,58 @@  discard block
 block discarded – undo
175 175
      * 
176 176
      * @return string   The translated text.
177 177
      */
178
-    public function translate( string $text, array $replacements = [], string $to_locale = '' ) : string
178
+    public function translate(string $text, array $replacements = [], string $to_locale = '') : string
179 179
     {
180 180
         $do_translate   = false;
181 181
         $data           = [];
182 182
         $locale         = 'en';
183
-        $to_locale      = empty( $to_locale ) ? $this->getLocale() : $to_locale;
184
-        $text           = preg_replace( '/:([a-z]+)/i', '__$1__', $text ); // Turn replacements to placeholders
183
+        $to_locale      = empty($to_locale) ? $this->getLocale() : $to_locale;
184
+        $text           = preg_replace('/:([a-z]+)/i', '__$1__', $text); // Turn replacements to placeholders
185 185
         $trans          = $text;
186 186
         $translations   = [];
187 187
 
188
-        if ( ! array_key_exists( $to_locale, $this->getLocales() ) ) {
188
+        if ( ! array_key_exists($to_locale, $this->getLocales())) {
189 189
             $message = 'Invalid Argument. Locale not found for translation.';
190 190
 
191
-            throw new InvalidArgumentException( $message );
191
+            throw new InvalidArgumentException($message);
192 192
         }
193 193
 
194
-        if ( empty( $text ) ) return $text;
194
+        if (empty($text)) return $text;
195 195
 
196 196
         // Get translation data from session or database
197
-        $translations = (array) ($_SESSION['__translations'] ?? $this->translationModel->pluck( 'data', 'text' )->toArray());
197
+        $translations = (array) ($_SESSION['__translations'] ?? $this->translationModel->pluck('data', 'text')->toArray());
198 198
 
199
-        if ( ( empty( $translations ) || ! isset( $translations[ $text ] ) ) && $locale != $to_locale ) {
199
+        if ((empty($translations) || ! isset($translations[$text])) && $locale != $to_locale) {
200 200
             $do_translate = true;
201
-        } else if ( isset( $translations[ $text ] ) ) {
202
-            $data = json_decode( $translations[ $text ], true );
201
+        } else if (isset($translations[$text])) {
202
+            $data = json_decode($translations[$text], true);
203 203
 
204
-            if ( $locale != $to_locale ) {
205
-                $trans = $data[ $to_locale ] ?? '';
204
+            if ($locale != $to_locale) {
205
+                $trans = $data[$to_locale] ?? '';
206 206
 
207
-                $do_translate = ! empty( $trans ) && $trans == $text ? true : ( empty( $trans ) ? true : false );
207
+                $do_translate = ! empty($trans) && $trans == $text ? true : (empty($trans) ? true : false);
208 208
             }
209 209
         }
210 210
 
211
-        if ( $do_translate ) {
211
+        if ($do_translate) {
212 212
             // Let's request for translation
213 213
             try {
214
-                $trans = (new T1( $this->getApiKey() ) )->translate( $text, $to_locale, $locale );
214
+                $trans = (new T1($this->getApiKey()))->translate($text, $to_locale, $locale);
215 215
 
216 216
             } catch (\Exception $e) {
217
-                Log::info( $e->getMessage() );
217
+                Log::info($e->getMessage());
218 218
 
219
-                $trans = T2::trans( $text, $to_locale, $locale );
219
+                $trans = T2::trans($text, $to_locale, $locale);
220 220
             }
221 221
         }
222 222
 
223
-        $data = array_merge( $data, [ $to_locale => $trans ] );
224
-        $translations[ $text ] = json_encode( $data );
223
+        $data = array_merge($data, [$to_locale => $trans]);
224
+        $translations[$text] = json_encode($data);
225 225
 
226 226
         // Save to session
227 227
         $_SESSION['__translations'] = $translations;
228 228
 
229
-        return (string) ( empty( $replacements ) ? $trans : $this->makeReplacements( $trans, $replacements ) );
229
+        return (string) (empty($replacements) ? $trans : $this->makeReplacements($trans, $replacements));
230 230
     }
231 231
 
232 232
     /**
@@ -238,13 +238,13 @@  discard block
 block discarded – undo
238 238
      * 
239 239
      * @return string   The replaced text
240 240
      */
241
-    private function makeReplacements( string $text, array $replacements ) : string
241
+    private function makeReplacements(string $text, array $replacements) : string
242 242
     {
243
-        $keys       = strtolower( '__' . join( '__,__', array_keys( $replacements ) ) . '__' );
244
-        $search     = explode( ',', $keys );
245
-        $replace    = array_values( $replacements );
243
+        $keys       = strtolower('__'.join('__,__', array_keys($replacements)).'__');
244
+        $search     = explode(',', $keys);
245
+        $replace    = array_values($replacements);
246 246
 
247
-        return (string) str_replace( $search, $replace, $text );
247
+        return (string) str_replace($search, $replace, $text);
248 248
     }
249 249
 
250 250
     /**
@@ -256,21 +256,21 @@  discard block
 block discarded – undo
256 256
     {
257 257
         // Get translation data from session
258 258
         $new_translations = $_SESSION['__translations'] ?? [];
259
-        $old_translations = (array) $this->translationModel->pluck( 'data', 'text' )->toArray();
259
+        $old_translations = (array) $this->translationModel->pluck('data', 'text')->toArray();
260 260
 
261
-        if ( json_encode( $new_translations ) == json_encode( $old_translations ) ) return;
261
+        if (json_encode($new_translations) == json_encode($old_translations)) return;
262 262
 
263 263
         $now = Carbon::now('utc')->toDateTimeString();
264 264
         $insert = $update = [];
265 265
 
266
-        if ( empty( $new_translations ) ) return;
266
+        if (empty($new_translations)) return;
267 267
 
268 268
         foreach ($new_translations as $text => $data) {
269
-            if ( isset( $old_translations[ $text ] ) ) {
270
-                if ( $old_translations[ $text ] == $data ) continue;
269
+            if (isset($old_translations[$text])) {
270
+                if ($old_translations[$text] == $data) continue;
271 271
 
272 272
                 // Update old
273
-                $this->translationModel->where( 'text', $text )->update( [ 'data' => $data ] );
273
+                $this->translationModel->where('text', $text)->update(['data' => $data]);
274 274
                 continue;
275 275
             }
276 276
 
@@ -283,9 +283,9 @@  discard block
 block discarded – undo
283 283
             ];
284 284
         }
285 285
 
286
-        if ( ! empty( $insert ) ) $this->translationModel->insert( $insert );
286
+        if ( ! empty($insert)) $this->translationModel->insert($insert);
287 287
 
288 288
         // Remove the translation
289
-        if ( isset( $_SESSION['__translations'] ) ) unset( $_SESSION['__translations'] );
289
+        if (isset($_SESSION['__translations'])) unset($_SESSION['__translations']);
290 290
     }
291 291
 }
Please login to merge, or discard this patch.