@@ -19,7 +19,7 @@ |
||
| 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 | } |
@@ -20,8 +20,8 @@ discard block |
||
| 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 |
||
| 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 | /** |
@@ -34,7 +34,10 @@ |
||
| 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([ |
@@ -13,7 +13,7 @@ |
||
| 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'); |
@@ -2,7 +2,7 @@ discard block |
||
| 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 |
||
| 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 |
@@ -43,10 +43,10 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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,7 +91,7 @@ discard block |
||
| 91 | 91 | */ |
| 92 | 92 | protected function getDefaultLocale() : string |
| 93 | 93 | { |
| 94 | - return (string) $this->config->get( 'app.locale', $this->config->get('app.fallback_locale', 'en') ); |
|
| 94 | + return (string) $this->config->get('app.locale', $this->config->get('app.fallback_locale', 'en')); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | /** |
@@ -114,9 +114,9 @@ discard block |
||
| 114 | 114 | */ |
| 115 | 115 | public function getRoutePrefix() : string |
| 116 | 116 | { |
| 117 | - $locale = (string) $this->request->segment( $this->getRequestSegment() ); |
|
| 117 | + $locale = (string) $this->request->segment($this->getRequestSegment()); |
|
| 118 | 118 | |
| 119 | - return $this->localeExist( $locale ) ? $locale : ''; |
|
| 119 | + return $this->localeExist($locale) ? $locale : ''; |
|
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | /** |
@@ -126,7 +126,7 @@ discard block |
||
| 126 | 126 | */ |
| 127 | 127 | protected function getRequestSegment() : int |
| 128 | 128 | { |
| 129 | - return $this->config->get( 'translation.request_segment', 1 ); |
|
| 129 | + return $this->config->get('translation.request_segment', 1); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
@@ -135,12 +135,12 @@ discard block |
||
| 135 | 135 | * @param string $locale The locale to use. Defaults to 'en'. |
| 136 | 136 | * @throws InvalidArgumentException|Exception |
| 137 | 137 | */ |
| 138 | - public function setLocale( string $locale = 'en' ) |
|
| 138 | + public function setLocale(string $locale = 'en') |
|
| 139 | 139 | { |
| 140 | - if ( ! empty( $locale ) && ! $this->localeExist( $locale ) ) { |
|
| 140 | + if ( ! empty($locale) && ! $this->localeExist($locale)) { |
|
| 141 | 141 | $message = 'Invalid Argument! Locale passed does not exist.'; |
| 142 | 142 | |
| 143 | - throw new InvalidArgumentException( $message ); |
|
| 143 | + throw new InvalidArgumentException($message); |
|
| 144 | 144 | } |
| 145 | 145 | |
| 146 | 146 | $_SESSION['locale'] = $locale; |
@@ -153,9 +153,9 @@ discard block |
||
| 153 | 153 | * |
| 154 | 154 | * @return bool |
| 155 | 155 | */ |
| 156 | - public function localeExist( string $locale = 'en' ) : bool |
|
| 156 | + public function localeExist(string $locale = 'en') : bool |
|
| 157 | 157 | { |
| 158 | - return array_key_exists( $locale, (array) $this->getLocales() ) ? true : false; |
|
| 158 | + return array_key_exists($locale, (array) $this->getLocales()) ? true : false; |
|
| 159 | 159 | } |
| 160 | 160 | |
| 161 | 161 | /** |
@@ -171,58 +171,58 @@ discard block |
||
| 171 | 171 | * |
| 172 | 172 | * @return string The translated text. |
| 173 | 173 | */ |
| 174 | - public function translate( string $text, array $replacements = [], string $to_locale = '' ) : string |
|
| 174 | + public function translate(string $text, array $replacements = [], string $to_locale = '') : string |
|
| 175 | 175 | { |
| 176 | 176 | $do_translate = false; |
| 177 | 177 | $data = []; |
| 178 | 178 | $locale = 'en'; |
| 179 | - $to_locale = empty( $to_locale ) ? $this->getLocale() : $to_locale; |
|
| 180 | - $text = preg_replace( '/:([a-z]+)/i', '__$1__', $text ); // Turn replacements to placeholders |
|
| 179 | + $to_locale = empty($to_locale) ? $this->getLocale() : $to_locale; |
|
| 180 | + $text = preg_replace('/:([a-z]+)/i', '__$1__', $text); // Turn replacements to placeholders |
|
| 181 | 181 | $trans = $text; |
| 182 | 182 | $translations = []; |
| 183 | 183 | |
| 184 | - if ( ! array_key_exists( $to_locale, $this->getLocales() ) ) { |
|
| 184 | + if ( ! array_key_exists($to_locale, $this->getLocales())) { |
|
| 185 | 185 | $message = 'Invalid Argument. Locale not found for translation.'; |
| 186 | 186 | |
| 187 | - throw new InvalidArgumentException( $message ); |
|
| 187 | + throw new InvalidArgumentException($message); |
|
| 188 | 188 | } |
| 189 | 189 | |
| 190 | - if ( empty( $text ) ) return $text; |
|
| 190 | + if (empty($text)) return $text; |
|
| 191 | 191 | |
| 192 | 192 | // Get translation data from session or database |
| 193 | - $translations = (array) ($_SESSION['__translations'] ?? $this->translationModel->pluck( 'data', 'text' )->toArray()); |
|
| 193 | + $translations = (array) ($_SESSION['__translations'] ?? $this->translationModel->pluck('data', 'text')->toArray()); |
|
| 194 | 194 | |
| 195 | - if ( ( empty( $translations ) || ! isset( $translations[ $text ] ) ) && $locale != $to_locale ) { |
|
| 195 | + if ((empty($translations) || ! isset($translations[$text])) && $locale != $to_locale) { |
|
| 196 | 196 | $do_translate = true; |
| 197 | - } else if ( isset( $translations[ $text ] ) ) { |
|
| 198 | - $data = json_decode( $translations[ $text ], true ); |
|
| 197 | + } else if (isset($translations[$text])) { |
|
| 198 | + $data = json_decode($translations[$text], true); |
|
| 199 | 199 | |
| 200 | - if ( $locale != $to_locale ) { |
|
| 201 | - $trans = $data[ $to_locale ] ?? ''; |
|
| 200 | + if ($locale != $to_locale) { |
|
| 201 | + $trans = $data[$to_locale] ?? ''; |
|
| 202 | 202 | |
| 203 | - $do_translate = ! empty( $trans ) && $trans == $text ? true : ( empty( $trans ) ? true : false ); |
|
| 203 | + $do_translate = ! empty($trans) && $trans == $text ? true : (empty($trans) ? true : false); |
|
| 204 | 204 | } |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | - if ( $do_translate ) { |
|
| 207 | + if ($do_translate) { |
|
| 208 | 208 | // Let's request for translation |
| 209 | 209 | try { |
| 210 | - $trans = (new T1( $this->getApiKey() ))->translate( $text, $to_locale, $locale ); |
|
| 210 | + $trans = (new T1($this->getApiKey()))->translate($text, $to_locale, $locale); |
|
| 211 | 211 | |
| 212 | 212 | } catch (\Exception $e) { |
| 213 | - Log::info( $e->getMessage() ); |
|
| 213 | + Log::info($e->getMessage()); |
|
| 214 | 214 | |
| 215 | 215 | // $trans = T2::trans( $text, $to_locale, $locale ); |
| 216 | 216 | } |
| 217 | 217 | } |
| 218 | 218 | |
| 219 | - $data = array_merge( $data, [ $to_locale => $trans ] ); |
|
| 220 | - $translations[ $text ] = json_encode( $data ); |
|
| 219 | + $data = array_merge($data, [$to_locale => $trans]); |
|
| 220 | + $translations[$text] = json_encode($data); |
|
| 221 | 221 | |
| 222 | 222 | // Save to session |
| 223 | 223 | $_SESSION['__translations'] = $translations; |
| 224 | 224 | |
| 225 | - return (string) ( empty( $replacements ) ? $trans : $this->makeReplacements( $trans, $replacements ) ); |
|
| 225 | + return (string) (empty($replacements) ? $trans : $this->makeReplacements($trans, $replacements)); |
|
| 226 | 226 | } |
| 227 | 227 | |
| 228 | 228 | /** |
@@ -234,13 +234,13 @@ discard block |
||
| 234 | 234 | * |
| 235 | 235 | * @return string The replaced text |
| 236 | 236 | */ |
| 237 | - private function makeReplacements( string $text, array $replacements ) : string |
|
| 237 | + private function makeReplacements(string $text, array $replacements) : string |
|
| 238 | 238 | { |
| 239 | - $keys = strtolower( '__' . join( '__,__', array_keys( $replacements ) ) . '__' ); |
|
| 240 | - $search = explode( ',', $keys ); |
|
| 241 | - $replace = array_values( $replacements ); |
|
| 239 | + $keys = strtolower('__'.join('__,__', array_keys($replacements)).'__'); |
|
| 240 | + $search = explode(',', $keys); |
|
| 241 | + $replace = array_values($replacements); |
|
| 242 | 242 | |
| 243 | - return (string) str_replace( $search, $replace, $text ); |
|
| 243 | + return (string) str_replace($search, $replace, $text); |
|
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | /** |
@@ -252,21 +252,21 @@ discard block |
||
| 252 | 252 | { |
| 253 | 253 | // Get translation data from session |
| 254 | 254 | $new_translations = $_SESSION['__translations'] ?? []; |
| 255 | - $old_translations = (array) $this->translationModel->pluck( 'data', 'text' )->toArray(); |
|
| 255 | + $old_translations = (array) $this->translationModel->pluck('data', 'text')->toArray(); |
|
| 256 | 256 | |
| 257 | - if ( json_encode( $new_translations ) == json_encode( $old_translations ) ) return; |
|
| 257 | + if (json_encode($new_translations) == json_encode($old_translations)) return; |
|
| 258 | 258 | |
| 259 | 259 | $now = Carbon::now('utc')->toDateTimeString(); |
| 260 | 260 | $insert = $update = []; |
| 261 | 261 | |
| 262 | - if ( empty( $new_translations ) ) return; |
|
| 262 | + if (empty($new_translations)) return; |
|
| 263 | 263 | |
| 264 | 264 | foreach ($new_translations as $text => $data) { |
| 265 | - if ( isset( $old_translations[ $text ] ) ) { |
|
| 266 | - if ( $old_translations[ $text ] == $data ) continue; |
|
| 265 | + if (isset($old_translations[$text])) { |
|
| 266 | + if ($old_translations[$text] == $data) continue; |
|
| 267 | 267 | |
| 268 | 268 | // Update old |
| 269 | - $this->translationModel->where( 'text', $text )->update( [ 'data' => $data ] ); |
|
| 269 | + $this->translationModel->where('text', $text)->update(['data' => $data]); |
|
| 270 | 270 | continue; |
| 271 | 271 | } |
| 272 | 272 | |
@@ -279,9 +279,9 @@ discard block |
||
| 279 | 279 | ]; |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | - if ( ! empty( $insert ) ) $this->translationModel->insert( $insert ); |
|
| 282 | + if ( ! empty($insert)) $this->translationModel->insert($insert); |
|
| 283 | 283 | |
| 284 | 284 | // Remove the translation |
| 285 | - if ( isset( $_SESSION['__translations'] ) ) unset( $_SESSION['__translations'] ); |
|
| 285 | + if (isset($_SESSION['__translations'])) unset($_SESSION['__translations']); |
|
| 286 | 286 | } |
| 287 | 287 | } |
@@ -187,7 +187,9 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 | } |