public static function generate($language = false)
 {
     $config = config('db-translator');
     /**
      * First we get the list of languages and iterate for each one of them
      * to get the associated translations
      */
     if ($language) {
         if (is_integer($language)) {
             $languages[] = Languages::find($language);
         } else {
             $languages = Languages::whereIso($language)->get();
         }
     } else {
         $languages = Languages::whereStatus(1)->get();
     }
     /**
      * Get the list of available variables in the database
      */
     $variables = Intl::all();
     $arr = [];
     foreach ($languages as $language) {
         /**
          * Lets generate a list of variables per language and
          * all translations associated
          */
         foreach ($variables as $variable) {
             /**
              * Generate per group
              */
             $trans = Translated::VarLang($variable->id, $language->id)->first()['translation'];
             $translation = $trans ? $trans : $variable->text;
             $arr[$language->iso][$variable->group][] = [md5($variable->text) . sha1($variable->text) => $translation];
         }
     }
     foreach ($arr as $language => $values) {
         \File::deleteDirectory(base_path('resources/lang/vendor/dbtranslator/' . $language), true);
         foreach ($values as $group => $trans) {
             $file = "<?php\nreturn [\r\n";
             foreach ($trans as $hash => $translated) {
                 $save_value = str_replace('"', '\\"', array_values($translated)[0]);
                 $file .= "    \"" . array_keys($translated)[0] . "\" => \"" . $save_value . "\",\r\n";
             }
             $file .= "];";
             \Storage::disk($config['storage_driver'])->put($language . '/' . $group . '.php', $file);
         }
     }
     return true;
 }
Ejemplo n.º 2
0
/**
 * If using the return method, it will return an array of the variable
 */
function lang($text = false, $vars = null, $value = null, $group = null, $locale = null, $force_add = false)
{
    $original_locale = Lang::getLocale();
    $params['value'] = $value ? $value : 0;
    $params['vars'] = $vars ? $vars : [];
    $params['locale'] = $locale ? $locale : $original_locale;
    $params['group'] = $group ? $group : 'general';
    $params['dynamic'] = false;
    if (preg_match('/dynamic_/', $params['group'])) {
        $params['group'] = preg_replace('/dynamic_/', '', $params['group']);
        $params['dynamic'] = true;
    }
    $hash = md5($text) . sha1($text);
    $file_namespace = 'dbtranslator';
    if (preg_match('/\\|/', $text)) {
        $choose = true;
    } else {
        $choose = false;
    }
    $config = config('db-translator');
    /**
     * Before anything lets change the locale to the intl locale
     */
    App::setLocale($params['locale']);
    /**
     * This will check the existence of the translation on the locale given, not the default
     */
    if (!$force_add) {
        if (Lang::has($file_namespace . '::' . $params['group'] . '.' . $hash)) {
            if ($choose) {
                $text = trans_choice($file_namespace . '::' . $params['group'] . '.' . $hash, $params['value'], $params['vars']);
                App::setLocale($original_locale);
                return $text;
            } else {
                $text = trans($file_namespace . '::' . $params['group'] . '.' . $hash, $params['vars']);
                App::setLocale($original_locale);
                return $text;
            }
        } else {
            /**
             * If we are using the database translations
             */
            if ($config['use_database']) {
                if (!Intl::whereText($text)->whereGroup($params['group'])->get()->count()) {
                    $intl = new Intl();
                    $intl->text = $text;
                    $intl->group = $params['group'];
                    $intl->md5sha1 = $hash;
                    $intl->dynamic = $params['dynamic'];
                    $intl->save();
                }
            } else {
                /**
                 * first we will check if the translation exists under the fallback language
                 */
                App::setLocale($config['default_locale']);
                if (!Lang::has($file_namespace . '::' . $params['group'] . '.' . $hash)) {
                    // if we don't find it, we should include the file for the original locale
                    // change it with the new array, and save it.
                    /**
                     * Determine if the file exists
                     */
                    if (Storage::disk($config['storage_driver'])->exists($config['default_locale'] . '/' . $params['group'] . '.php')) {
                        $lang_array = (require base_path('resources/lang/vendor/dbtranslator/' . $config['default_locale'] . '/' . $params['group'] . '.php'));
                        if (!isset($lang_array[$hash])) {
                            $lang_array[$hash] = $text;
                            $file = "<?php\n        return [\r\n";
                            foreach ($lang_array as $k => $v) {
                                $file .= "    \"" . $k . "\" => \"" . str_replace('"', '\\"', $v) . "\",\r\n";
                            }
                            $file .= "];";
                            Storage::disk($config['storage_driver'])->put($config['default_locale'] . '/' . $params['group'] . '.php', $file);
                        }
                    } else {
                        $file = "<?php\n    return [\r\n";
                        $file .= "    \"" . $hash . "\" => \"" . str_replace('"', '\\"', $text) . "\",\r\n";
                        $file .= "];";
                        Storage::disk($config['storage_driver'])->put($config['default_locale'] . '/' . $params['group'] . '.php', $file);
                    }
                }
                App::setLocale($params['locale']);
            }
            // now we will process the string
            if ($choose) {
                $params['vars']['count'] = $params['value'];
                $ms = new MessageSelector();
                $text = $ms->choose($text, $params['value'], $params['locale']);
                $params['vars'] = (new Collection($params['vars']))->sortBy(function ($value, $key) {
                    return mb_strlen($key) * -1;
                });
                foreach ($params['vars'] as $key => $value) {
                    $text = str_replace(':' . $key, $value, $text);
                }
                App::setLocale($original_locale);
                return $text;
            } else {
                if (count($params['vars'])) {
                    $params['vars'] = (new Collection($params['vars']))->sortBy(function ($value, $key) {
                        return mb_strlen($key) * -1;
                    });
                    foreach ($params['vars'] as $key => $value) {
                        $text = str_replace(':' . $key, $value, $text);
                    }
                }
                App::setLocale($original_locale);
                return $text;
            }
        }
    } else {
        if ($config['use_database']) {
            if (!Intl::whereText($text)->whereGroup($params['group'])->get()->count()) {
                $intl = new Intl();
                $intl->text = $text;
                $intl->group = $params['group'];
                $intl->md5sha1 = $hash;
                $intl->dynamic = $params['dynamic'];
                $intl->save();
            }
        }
    }
}
 /**
  * Execute the command.
  *
  * @return void
  */
 public function handle()
 {
     $variables = Intl::whereDynamic(0)->get()->toArray();
     /**
      * Get all files in views
      */
     $paths = [];
     $folders = config('view');
     $paths[] = base_path('app/Http');
     foreach ($folders['paths'] as $folder) {
         $paths[] = $folder;
     }
     $keys = [];
     $bar_path = $this->output->createProgressBar(count($paths));
     foreach ($paths as $key => $path) {
         //$this->info('PATH: "'.$path);
         $finder = new Finder();
         $bar_finder = $this->output->createProgressBar(count($finder->in($path)->name('*.php')->files()));
         foreach ($finder as $file) {
             $f = $file->getContents();
             preg_match_all("/lang(?:\\s*)\\((?:(?:\\s*)(?:\\'|\\\")(.*?)(?:\\'|\\\"))(?:(?:\\s*)(?:\\,(?:\\s*)((?:\\[|array\\().*(?:\\]|\\))|null)(?:\\s*)(?:\\,(?:\\s*)(?:(\\d+?|null))(?:\\s*)(?:\\,(?:\\s*)(?:\\'|\\\")(.*?)(?:\\'|\\\")(?:\\s*)?)?)?)?)\\)/", $f, $matches);
             if (count($matches[2]) > 0) {
                 foreach ($matches[2] as $k) {
                     while (preg_match_all("/lang(?:\\s*)\\((?:(?:\\s*)(?:\\'|\\\")(.*?)(?:\\'|\\\"))(?:(?:\\s*)(?:\\,(?:\\s*)((?:\\[|array\\().*(?:\\]|\\))|null)(?:\\s*)(?:\\,(?:\\s*)(?:(\\d+?|null))(?:\\s*)(?:\\,(?:\\s*)(?:\\'|\\\")(.*?)(?:\\'|\\\")(?:\\s*)?)?)?)?)\\)/", $k, $ka)) {
                         $keys[] = $ka[0][0];
                         $k = str_replace($ka[0][0], "null", $k);
                         $f = str_replace($ka[0][0], "null", $f);
                     }
                     while (preg_match_all("/lang(?:\\s*)\\((?:(?:\\s*)(?:\\'|\\\")(.*?)(?:\\'|\\\"))(?:(?:\\s*)(?:\\,(?:\\s*)((?:\\[|array\\().*(?:\\]|\\))|null)(?:\\s*)(?:\\,(?:\\s*)(?:(\\d+?|null))(?:\\s*)(?:\\,(?:\\s*)(?:\\'|\\\")(.*?)(?:\\'|\\\")(?:\\s*)?)?)?)?)\\)/", $f, $kv)) {
                         foreach ($kv[0] as $fa) {
                             $keys[] = $fa;
                             $f = str_replace($fa, "null", $f);
                         }
                     }
                 }
             }
             $bar_finder->advance();
         }
         $bar_path->advance();
     }
     $bar_finder->finish();
     $bar_path->finish();
     $keys = array_unique($keys);
     $check = [];
     $bar = $this->output->createProgressBar(count($keys));
     foreach ($keys as $key => $match) {
         if (!preg_match("/(lang(?:(\\s?|\\s+))\\()(?:(\\s?|\\s+))(?:(\\\$))/", $match)) {
             // if it is not a dynamic variable
             preg_match_all("/lang(?:\\s*)\\((?:(?:\\s*)(?:\\'|\\\")(.*?)(?:\\'|\\\"))(?:(?:\\s*)(?:\\,(?:\\s*)((?:\\[|array\\().*(?:\\]|\\))|null)(?:\\s*)(?:\\,(?:\\s*)(?:(\\d+?|null))(?:\\s*)(?:\\,(?:\\s*)(?:\\'|\\\")(.*?)(?:\\'|\\\")(?:\\s*)?)?)?)?)\\)/", $match, $d);
             if (!empty($d[1][0]) and (!empty($d[4][0]) and $d[4][0] != 'null')) {
                 $check[$d[4][0]][] = $d[1][0];
                 //$this->info('INSERTING: "'.$d[1][0].'" on "'.$d[4][0].'" group.');
             } elseif (!empty($d[1][0])) {
                 $check['general'][] = $d[1][0];
                 //$this->info('INSERTING: "'.$d[1][0].'" on "general" group.');
             }
         }
         $bar->advance();
     }
     $bar->finish();
     /**
      * lets iterate though all variables in the database and see which ones need to be removed
      */
     $toremove = false;
     if (count($variables) > 0) {
         foreach ($variables as $key => $variable) {
             if (!isset($check[$variable['group']])) {
                 $toremove = true;
                 $remove[] = $variable['id'];
                 $this->info('TO REMOVE: "' . $variable['group'] . ':' . $variable['md5sha1'] . '.' . $variable['text'] . '"');
             } else {
                 if (!in_array($variable['text'], array_values($check[$variable['group']]))) {
                     $toremove = true;
                     $remove[] = $variable['id'];
                     $this->info('TO REMOVE: "' . $variable['group'] . ':' . $variable['md5sha1'] . '.' . $variable['text'] . '"');
                 }
             }
         }
     }
     if ($toremove) {
         if ($this->confirm('Do you wish to continue removing this from database? [yes|no]')) {
             foreach ($remove as $key => $id) {
                 $locale = Intl::find($id);
                 $locale->delete();
                 $this->info('REMOVING: ' . $id);
             }
         }
         $this->info('Completed!');
     } else {
         $this->info('Nothing to remove!');
     }
 }