-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #702 from xfudox/LocaleRepository
Locale Repository
- Loading branch information
Showing
8 changed files
with
332 additions
and
8 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
Modules/Translation/Assets/js/components/locale/LocaleCodeSelect.vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<template> | ||
<el-select | ||
v-model="_locale_code" | ||
filterable | ||
remote | ||
value-key="code" | ||
:remote-method="fetchOptions" | ||
:loading="loading" > | ||
<el-option | ||
v-for ="item in _options" | ||
:key ="item.code" | ||
:label ="item.label" | ||
:value ="item.code"> | ||
{{ item.label }} | ||
<span class='code' v-if="item.code">{{ item.code }}</span> | ||
</el-option> | ||
</el-select> | ||
</template> | ||
|
||
<script> | ||
import axios from 'axios'; | ||
import _ from 'lodash'; | ||
export default { | ||
name: 'LocaleCodeSelect', | ||
props: { | ||
value: { | ||
type: String, | ||
default: null, | ||
}, | ||
options: { | ||
type: Array, | ||
default: null, | ||
}, | ||
}, | ||
data() { | ||
return { | ||
loading: false, | ||
locale_code: this.value, | ||
opts: [], | ||
}; | ||
}, | ||
methods: { | ||
fetchOptions(query) { | ||
if (query.length >= 3) { | ||
this.loading = true; | ||
this.queryServer({ | ||
search: query, | ||
}); | ||
} | ||
}, | ||
queryServer(customProperties) { | ||
const properties = { | ||
order_by: 'name', | ||
order: 'asc', | ||
search: this.searchQuery, | ||
}; | ||
axios.get(route('api.translation.translations.list-locales-for-select', _.merge(properties, customProperties))) | ||
.then((response) => { | ||
this.loading = false; | ||
this.opts = response.data.data; | ||
}); | ||
}, | ||
}, | ||
computed: { | ||
_locale_code: { | ||
get: function(){ | ||
return this.value; | ||
}, | ||
set: function(new_val){ | ||
this.locale_code = new_val; | ||
this.$emit('input', this.locale_code); | ||
} | ||
}, | ||
_options: { | ||
get: function(){ | ||
if(this.opts.length>0){ | ||
return this.opts; | ||
} | ||
if(this.options!=null){ | ||
return this.options; | ||
} | ||
return []; | ||
}, | ||
} | ||
}, | ||
}; | ||
</script> | ||
|
||
<style> | ||
.code { | ||
float: right; | ||
color: #8492a6; | ||
font-size: 13px; | ||
} | ||
</style> | ||
|
25 changes: 25 additions & 0 deletions
25
Modules/Translation/Http/Controllers/Api/LocaleController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Modules\Translation\Http\Controllers\Api; | ||
|
||
use Illuminate\Routing\Controller; | ||
use Modules\Translation\Repositories\LocaleRepository; | ||
use Modules\Translation\Transformers\LocaleSelectTransformer; | ||
use Modules\Translation\Http\Requests\LocaleCodeRequest; | ||
|
||
class LocaleController extends Controller | ||
{ | ||
private $locale; | ||
|
||
public function __construct(LocaleRepository $locale) | ||
{ | ||
$this->locale = $locale; | ||
} | ||
|
||
public function listLocalesForSelect(LocaleCodeRequest $request) | ||
{ | ||
return LocaleSelectTransformer::collection( | ||
$this->locale->listLocalesForSelect($request) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Modules\Translation\Http\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class LocaleCodeRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
return [ | ||
'order_by' => ['required', 'string'], | ||
'order' => ['required', 'string', 'in:asc,desc'], | ||
'search' => ['nullable', 'string', 'min:3'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
Modules/Translation/Repositories/Eloquent/EloquentLocaleRepository.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
<?php | ||
|
||
namespace Modules\Translation\Repositories\Eloquent; | ||
|
||
use Illuminate\Support\Collection; | ||
use Modules\Translation\Repositories\LocaleRepository; | ||
use Modules\Translation\Http\Requests\LocaleCodeRequest; | ||
|
||
class EloquentLocaleRepository implements LocaleRepository | ||
{ | ||
private $locales; | ||
private $mapper; | ||
|
||
public function __construct() | ||
{ | ||
$this->mapper = function (array $item, string $key) { | ||
return [ | ||
'code' => $key, | ||
'name' => $item['name'], | ||
'script' => $item['script'], | ||
'native' => $item['native'], | ||
]; | ||
}; | ||
$this->locales = collect(config('asgard.core.available-locales')) | ||
->map($this->mapper) | ||
->values(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function listLocalesForSelect(LocaleCodeRequest $request): Collection | ||
{ | ||
$locales = $this->locales; | ||
|
||
$search = $request->get('search'); | ||
if ($search !== null) { | ||
$locales = $locales->filter(function ($locale) use ($search) { | ||
$name = $locale['name']; | ||
|
||
return stripos($name, $search) !== false; // https://www.php.net/manual/en/function.stripos.php | ||
}); | ||
} | ||
|
||
$order = $request->get('order'); | ||
$order_by = $request->get('order_by'); | ||
switch ($order_by) { | ||
case 'code': | ||
if ($order == 'asc') { | ||
$locales->sortBy('code'); | ||
} else { | ||
$locales->sortByDesc('code'); | ||
} | ||
break; | ||
case 'name': | ||
default: | ||
if ($order == 'asc') { | ||
$locales->sortBy('name'); | ||
} else { | ||
$locales->sortByDesc('name'); | ||
} | ||
break; | ||
} | ||
|
||
return $locales->values(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function availableLocales(): Collection | ||
{ | ||
return $this->locales; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function supportedLocales(): Collection | ||
{ | ||
return collect(app()->config->get('laravellocalization.supportedLocales')) | ||
->map($this->mapper) | ||
->values(); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function translatableLocales(): Collection | ||
{ | ||
$translatable_locales = collect(app()->config->get('translatable.locales')) | ||
->filter(function ($item) { | ||
return is_string($item); | ||
})->toArray(); | ||
|
||
return $this->locales->filter(function ($locale) use ($translatable_locales) { | ||
return in_array($locale['code'], $translatable_locales); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace Modules\Translation\Repositories; | ||
|
||
use Illuminate\Support\Collection; | ||
use Modules\Translation\Http\Requests\LocaleCodeRequest; | ||
|
||
interface LocaleRepository | ||
{ | ||
/** | ||
* Return locales filtered,sorted and paginated according to request data | ||
* | ||
* @param LocaleCodeRequest $request | ||
* @return Collection | ||
*/ | ||
public function listLocalesForSelect(LocaleCodeRequest $request): Collection; | ||
|
||
/** | ||
* Return all available locales, as config('asgard.core.available-locales') | ||
* | ||
* @return Collection | ||
*/ | ||
public function availableLocales(): Collection; | ||
|
||
/** | ||
* Return same locales as app()->config->get('laravellocalization.supportedLocales') | ||
* | ||
* @return Collection | ||
*/ | ||
public function supportedLocales(): Collection; | ||
|
||
/** | ||
* Return same locales as app()->config->get('translatable.locales') | ||
* | ||
* @param array $locales | ||
* @return Collection | ||
*/ | ||
public function translatableLocales(array $locales = null): Collection; | ||
} |
16 changes: 16 additions & 0 deletions
16
Modules/Translation/Transformers/LocaleSelectTransformer.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Modules\Translation\Transformers; | ||
|
||
use Illuminate\Http\Resources\Json\Resource; | ||
|
||
class LocaleSelectTransformer extends Resource | ||
{ | ||
public function toArray($request) | ||
{ | ||
return [ | ||
'label' => $this['name'], | ||
'code' => $this['code'], | ||
]; | ||
} | ||
} |