This commit is contained in:
Kevin Adametz 2024-08-05 12:05:24 +02:00
parent 04d677d37a
commit bfa3bb1df4
1191 changed files with 637397 additions and 10619 deletions

View file

@ -2,180 +2,258 @@
namespace App\Http\Controllers;
use App;
use File;
use App\Models\Category;
use Request;
use App\Models\Product;
use App\Models\Ingredient;
use App\Models\Shipping;
use App\Models\TransCategory;
use App\Models\TransProduct;
use App\Models\TransIngredient;
use App\Models\TransShipping;
use App\Models\TransUserLevel;
use App\Models\UserLevel;
use Illuminate\Support\Collection;
use JoeDixon\Translation\Language;
class TranslationController extends Controller
{
protected $languagesPath;
protected $directory_separator;
protected $from;
protected $sourceLanguage;
protected $selectLanguage;
protected $selectKey;
protected $keys;
protected $model;
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->sourceLanguage = 'de';
$this->selectLanguage = 'en';
$this->keys = [];
$this->model = "";
}
/**
*/
public function index($model, $lang=null)
{
//Request::get('key')
//Request::get('language')
$this->initByModel($model);
$languages = $this->allLanguages();
$languages->forget('de');
$this->directory_separator = DIRECTORY_SEPARATOR;
$this->languagesPath = App::langPath();
$translations = $this->getTranslationsFormModel();
$data = [
'keys' => $this->keys,
'languages' => $languages,
'model' => $this->model,
'select_language' => $this->selectLanguage,
'source_language' => $this->sourceLanguage,
'translations' => $translations,
'select_key' => $this->selectKey
];
return view('translation::languages.translations.custom', $data);
}
/**
* Show the application dashboard.
*/
public function initByModel($model)
{
if($model === 'products'){
$this->model = $model;
$this->keys = [
'name'=>'Produktname',
'copy'=>'Produktbeschreibung',
'description'=>'Beschreibung',
'usage'=>'Anwendung',
'ingredients'=>'Hinweise',
];
$this->selectKey = 'name';
}
if($model === 'ingredients'){
$this->model = $model;
$this->keys = [
'name'=>'Name',
'inci'=>'INCI',
'effect'=>'Wirkung',
];
$this->selectKey = 'name';
}
if($model === 'user_levels'){
$this->model = $model;
$this->keys = [
'name'=>'Name',
];
$this->selectKey = 'name';
}
if($model === 'shippings'){
$this->model = $model;
$this->keys = [
'name'=>'Name',
];
$this->selectKey = 'name';
}
if($model === 'categories'){
$this->model = $model;
$this->keys = [
'name'=>'Name',
'headline'=>'Headline',
];
$this->selectKey = 'name';
}
if(Request::get('key')){
$this->selectKey = Request::get('key');
}
if(Request::get('language')){
$this->selectLanguage = Request::get('language');
}
//Request::get('language')
}
public function getTranslationsFormModel(){
if($this->model === 'products'){
return Product::all()->mapWithKeys(function ($value) {
return [$value->id => [
'master' => $value->name,
'source' => $value->{$this->selectKey},
'trans' => $value->getTrans($this->selectKey, $this->selectLanguage),
]
];
});
}
if($this->model === 'ingredients'){
return Ingredient::all()->mapWithKeys(function ($value) {
return [$value->id => [
'master' => $value->name,
'source' => $value->{$this->selectKey},
'trans' => $value->getTrans($this->selectKey, $this->selectLanguage),
]
];
});
}
if($this->model === 'user_levels'){
return UserLevel::all()->mapWithKeys(function ($value) {
return [$value->id => [
'master' => $value->name,
'source' => $value->{$this->selectKey},
'trans' => $value->getTrans($this->selectKey, $this->selectLanguage),
]
];
});
}
if($this->model === 'shippings'){
return Shipping::all()->mapWithKeys(function ($value) {
return [$value->id => [
'master' => $value->name,
'source' => $value->{$this->selectKey},
'trans' => $value->getTrans($this->selectKey, $this->selectLanguage),
]
];
});
}
if($this->model === 'categories'){
return Category::all()->mapWithKeys(function ($value) {
return [$value->id => [
'master' => $value->name,
'source' => $value->{$this->selectKey},
'trans' => $value->getTrans($this->selectKey, $this->selectLanguage),
]
];
});
}
}
public function update($model)
{
$id = Request::get('key'); //id
$key = Request::get('group'); //key colum
$language = Request::get('language'); //selectLanguage
$value = Request::get('value') ?: ''; //value
if($model === 'products'){
TransProduct::updateOrCreate([
'language' => $language,
'product_id' => $id,
'key' => $key,
], [
'key' => $key,
'value' => $value,
]);
}
if($model === 'ingredients'){
TransIngredient::updateOrCreate([
'language' => $language,
'ingredient_id' => $id,
'key' => $key,
], [
'key' => $key,
'value' => $value,
]);
}
if($model === 'user_levels'){
TransUserLevel::updateOrCreate([
'language' => $language,
'user_level_id' => $id,
'key' => $key,
], [
'key' => $key,
'value' => $value,
]);
}
if($model === 'shippings'){
TransShipping::updateOrCreate([
'language' => $language,
'shipping_id' => $id,
'key' => $key,
], [
'key' => $key,
'value' => $value,
]);
}
if($model === 'categories'){
TransCategory::updateOrCreate([
'language' => $language,
'categorie_id' => $id,
'key' => $key,
], [
'key' => $key,
'value' => $value,
]);
}
return ['success' => true];
}
/**
* Get all languages from the application.
*
* @return \Illuminate\Http\Response
* @return Collection
*/
public function index()
public function allLanguages()
{
return redirect('admin/translate/all/edit/de');
return Language::all()->mapWithKeys(function ($language) {
return [$language->language => $language->name ?: $language->language];
});
}
/**
* @param $language
* @param string $from
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function edit($language, $from = 'en')
{
$localisations = array_keys(config('localization.supportedLocales'));
$files = $this->json_files($localisations);
$translations = $this->translationsJson($language, $from);
$show = "all";
return view('translation.index', compact('files','translations', 'language', 'from', 'show'));
}
/**
* @param $lang
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function update($language)
{
$path = $this->resourcePath($this->languagesPath);
$file = $path.$language.".json";
$data = Request::all();
$this->backup($path, $language.".json");
unset($data['_token']);
$ret = [];
//file make keys
$source = json_decode(File::get($path."de.json"));
foreach ($source as $key => $v){
$skey = $this->sanitize($key);
if(!empty($data[$skey])) {
$ret[$key] = $data[$skey];
}
}
$jsonData = json_encode($ret, TRUE);
file_put_contents($file, $jsonData);
return redirect()
->route('admin_translate_all_edit', [$language])
->with('message', 'Translation added successfully');
}
/**
* @param $langs
* @return array
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function json_files($localisations){
$path = $this->resourcePath($this->languagesPath);
$content = array();
foreach ($localisations as $local){
$file = File::get($path.$local.".json");
if($file){
$content[$local] = array('path'=>$path.$local.".json", 'content'=>json_encode($file));
}
}
return $content;
}
/**
* @param $language
* @param $from
* @return array
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function translationsJson($language, $from)
{
$path = $this->resourcePath($this->languagesPath);
$file = array();
$file['keys'] = (array) json_decode(File::get($path."de.json"));
$file['keys'] = $this->sanitizeKey($file['keys']);
$file['from'] = (array) json_decode(File::get($path.$from.".json"));
$file['from'] = $this->sanitizeKey($file['from']);
$file['dest'] = (array) json_decode(File::get($path.$language.".json"));
$file['dest'] = $this->sanitizeKey($file['dest']);
return $file;
}
private function backup($path, $file)
{
if (!File::exists(storage_path('language/'))) {
File::makeDirectory(storage_path('language/'), 0755, true);
}
return File::copy($path.$file, storage_path('language/'.time()."-".$file));
}
/**
* @param $path
* @return string
*/
protected function resourcePath($path)
{
return "{$path}{$this->directory_separator}";
}
/**
* @param $arr
* @return mixed
*/
protected function sanitizeKey($arr){
foreach ($arr as $key => $val){
unset($arr[$key]);
$arr[$this->sanitize($key)] = $val;
}
return $arr;
}
/**
* @param $string
* @param bool $force_lowercase
* @param bool $anal
* @return mixed|null|string|string[]
*/
protected function sanitize($string, $force_lowercase = true, $anal = false)
{
$strip = array("~", "`", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "=", "+", "[", "{", "]",
"}", "\\", "|", ";", ":", "\"", "'", "‘", "’", "“", "”", "–", "—",
"—", "–", ",", "<", ".", ">", "/", "?");
$clean = trim(str_replace($strip, "", strip_tags($string)));
$clean = preg_replace('/\s+/', "_", $clean);
$clean = ($anal) ? preg_replace("/[^a-zA-Z0-9]/", "", $clean) : $clean ;
return ($force_lowercase) ?
(function_exists('mb_strtolower')) ?
mb_strtolower($clean, 'UTF-8') :
strtolower($clean) :
$clean;
}
}