Skip to content

Commit

Permalink
Merge pull request #702 from xfudox/LocaleRepository
Browse files Browse the repository at this point in the history
Locale Repository
  • Loading branch information
nWidart committed Jun 6, 2019
2 parents 32f2e87 + 683e60e commit 265d5cf
Show file tree
Hide file tree
Showing 8 changed files with 332 additions and 8 deletions.
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 Modules/Translation/Http/Controllers/Api/LocaleController.php
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)
);
}
}
32 changes: 32 additions & 0 deletions Modules/Translation/Http/Requests/LocaleCodeRequest.php
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'],
];
}
}
4 changes: 4 additions & 0 deletions Modules/Translation/Http/apiRoutes.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
'uses' => 'AllTranslationController',
'as' => 'api.translation.translations.all',
]);
$router->get('list-locales-for-select', [
'uses' => 'LocaleController@listLocalesForSelect',
'as' => 'api.translation.translations.list-locales-for-select',
]);
});
25 changes: 17 additions & 8 deletions Modules/Translation/Providers/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,25 @@
namespace Modules\Translation\Providers;

use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
use Modules\Core\Composers\CurrentUserViewComposer;
use Modules\Core\Events\BuildingSidebar;
use Illuminate\Support\Facades\Validator;
use Modules\Translation\Entities\Translation;
use Modules\Core\Traits\CanPublishConfiguration;
use Modules\Core\Composers\CurrentUserViewComposer;
use Modules\Core\Events\LoadingBackendTranslations;
use Modules\Translation\Services\TranslationLoader;
use Modules\Core\Traits\CanGetSidebarClassForModule;
use Modules\Core\Traits\CanPublishConfiguration;
use Modules\Translation\Console\BuildTranslationsCacheCommand;
use Modules\Translation\Entities\Translation;
use Modules\Translation\Repositories\LocaleRepository;
use Modules\Translation\Entities\TranslationTranslation;
use Modules\Translation\Repositories\TranslationRepository;
use Modules\Translation\Console\BuildTranslationsCacheCommand;
use Modules\Translation\Repositories\FileTranslationRepository;
use Modules\Translation\Events\Handlers\RegisterTranslationSidebar;
use Modules\Translation\Repositories\Cache\CacheTranslationDecorator;
use Modules\Translation\Repositories\Eloquent\EloquentLocaleRepository;
use Modules\Translation\Repositories\Eloquent\EloquentTranslationRepository;
use Modules\Translation\Repositories\File\FileTranslationRepository as FileDiskTranslationRepository;
use Modules\Translation\Repositories\FileTranslationRepository;
use Modules\Translation\Repositories\TranslationRepository;
use Modules\Translation\Services\TranslationLoader;

class TranslationServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -116,6 +118,13 @@ private function registerBindings()
$this->app->bind(FileTranslationRepository::class, function ($app) {
return new FileDiskTranslationRepository($app['files'], $app['translation.loader']);
});

$this->app->bind(
LocaleRepository::class,
function () {
return new EloquentLocaleRepository();
}
);
}

private function registerConsoleCommands()
Expand Down
100 changes: 100 additions & 0 deletions Modules/Translation/Repositories/Eloquent/EloquentLocaleRepository.php
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);
});
}
}
39 changes: 39 additions & 0 deletions Modules/Translation/Repositories/LocaleRepository.php
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 Modules/Translation/Transformers/LocaleSelectTransformer.php
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'],
];
}
}

0 comments on commit 265d5cf

Please sign in to comment.