Skip to content

Commit

Permalink
The old multilingual approach didn't work...this one does
Browse files Browse the repository at this point in the history
  • Loading branch information
Torann committed Mar 14, 2017
1 parent e55da77 commit 2aa6e77
Show file tree
Hide file tree
Showing 9 changed files with 226 additions and 88 deletions.
52 changes: 38 additions & 14 deletions config/hunt.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@

'index' => 'default',

/*
|--------------------------------------------------------------------------
| Multilingual Support
|--------------------------------------------------------------------------
|
| Use this to set the global locale field used in your models. When set
| Laravel Hunt will only perform searches in the current system locale.
|
| 'locale_field' => 'locale',
|
*/

'locale_field' => null,

/*
|--------------------------------------------------------------------------
| Unified Types
Expand Down Expand Up @@ -106,4 +92,42 @@
*/

'settings' => null,

/*
|--------------------------------------------------------------------------
| Model Namespace
|--------------------------------------------------------------------------
|
| Change this if you use a different model namespace for Laravel.
|
*/

'model_namespace' => '\\App\\',

/*
|--------------------------------------------------------------------------
| Multilingual Support
|--------------------------------------------------------------------------
|
| Use this to set support for multiple languages. Basically it suffixes
| the type with the locale code.
|
| For this to work, the model will need to use the `Localized` trait. Or
| something similar.
|
*/

'multilingual' => false,

/*
|--------------------------------------------------------------------------
| Support Locales
|--------------------------------------------------------------------------
|
| This is used in the command line to import and map models. If using the
| package ``
|
*/

'support_locales' => [],
];
15 changes: 8 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Unified search for Laravel models using Elasticsearch. Laravel Hunt uses the [of

# Elasticsearch Requirements

You must be running Elasticsearch 1.0 or higher.
You must be running Elasticsearch 5.0 or higher.

## Installation

Expand Down Expand Up @@ -107,14 +107,13 @@ Create the Elasticsearch index.

Remove the Elasticsearch index.

#### `hunt:map <action> <model>`
#### `hunt:map <model>`

Initialize an Eloquent model.
Initialize an Eloquent model map.

Arguments:

```
action Mapping action to perform (add or remove)
model Name or comma separated names of the model(s) to initialize
```

Expand Down Expand Up @@ -216,8 +215,10 @@ Once you have retrieved the results, you may display the results and render the
{{ $posts->links() }}
```

## Change Log
## Multilingual

#### v0.1.0
> This feature is experimental
- First release
Laravel Hunt can support multiple languages by appending the language code to the index type, so when the system performs a search it will only look for data that is on in the current system locale suffixed index type. For this to work the model needs to use the `LaravelHunt\Localized` trait or something similar to it and model needs to have the filed `locale`.

For more information see the config file for more details.
2 changes: 0 additions & 2 deletions src/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace LaravelHunt;

use Illuminate\Database\Eloquent\Collection;

class Builder
{
/**
Expand Down
62 changes: 55 additions & 7 deletions src/Console/AbstractCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use LaravelHunt\Hunter;
use Illuminate\Support\Str;
use Illuminate\Console\Command;
use Torann\Localization\LocaleManager;

abstract class AbstractCommand extends Command
{
Expand All @@ -13,6 +14,13 @@ abstract class AbstractCommand extends Command
*/
protected $hunter;

/**
* Namespace for models.
*
* @var string
*/
protected $models;

/**
* Create a new console command instance.
*
Expand All @@ -23,6 +31,21 @@ public function __construct(Hunter $hunter)
parent::__construct();

$this->hunter = $hunter;
$this->models = config('hunt.model_namespace', '\\App\\');
}

/**
* Perform action model mapping.
*
* @param string $action
*/
protected function processModels($action)
{
foreach ($this->getModelArgument() as $model) {
if ($model = $this->validateModel("{$this->models}{$model}")) {
$this->$action($model);
}
}
}

/**
Expand Down Expand Up @@ -61,13 +84,41 @@ protected function getModelArgument()
}

/**
* Get locale option.
* Get an array of supported locales.
*
* @return array
* @return array|null
*/
protected function getLocales()
{
// Get user specified locales
if ($locales = $this->option('locales')) {
return array_filter(explode(',', preg_replace('/\s+/', '', $locales)));
}

// Check for package
if (class_exists('Torann\\Localization\\LocaleManager')) {
return app(LocaleManager::class)->getSupportedLanguagesKeys();
}

return config('hunt.support_locales');
}

/**
* Get an array of supported locales.
*
* @param string $locale
*/
protected function getLocaleOption()
protected function setSystemLocale($locale)
{
return array_filter(explode(',', preg_replace('/\s+/', '', $this->option('locales'))));
$this->line('');
$this->line("System local set to: <info>{$locale}</info>");

if (class_exists('Torann\\Localization\\LocaleManager')) {
app(LocaleManager::class)->setLocale($locale);
}
else {
app()->setLocale($locale);
}
}

/**
Expand All @@ -78,9 +129,6 @@ protected function getLocaleOption()
*/
protected function validateModel($model)
{
// Determine the namespace
$model = ($model[0] !== '\\') ? "\\App\\{$model}" : $model;

// Verify model existence
if (class_exists($model) === false) {
$this->error("Model [{$model}] not found");
Expand Down
14 changes: 10 additions & 4 deletions src/Console/FlushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class FlushCommand extends AbstractCommand
* @var string
*/
protected $signature = 'hunt:flush
{model : Name or comma separated names of the model(s) to index}';
{model : Name or comma separated names of the model(s) to index}
{--l|locales= : Single or comma separated locales to index}';

/**
* The console command description.
Expand All @@ -28,11 +29,16 @@ class FlushCommand extends AbstractCommand
*/
public function handle()
{
foreach ($this->getModelArgument() as $model) {
if ($model = $this->validateModel($model)) {
$this->flush($model);
if (empty($locales = $this->getLocales()) === false) {
foreach ($locales as $locale) {
$this->setSystemLocale($locale);

$this->processModels('flush');
}
}
else {
$this->processModels('flush');
}
}

/**
Expand Down
26 changes: 8 additions & 18 deletions src/Console/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,31 +27,26 @@ class ImportCommand extends AbstractCommand
*/
public function handle()
{
$locales = $this->getLocaleOption();
if (empty($locales = $this->getLocales()) === false) {
foreach ($locales as $locale) {
$this->setSystemLocale($locale);

foreach ($this->getModelArgument() as $model) {
if ($model = $this->validateModel($model)) {
if (empty($locales) === false) {
foreach ($locales as $locale) {
$this->index($model, $locale);
}
}
else {
$this->index($model);
}
$this->processModels('index');
}
}
else {
$this->processModels('index');
}
}

/**
* Index all model entries to ElasticSearch.
*
* @param string $model
* @param string $locale
*
* @return bool
*/
protected function index($model, $locale = '')
protected function index($model)
{
$this->comment("Processing [{$model}]");

Expand All @@ -64,11 +59,6 @@ protected function index($model, $locale = '')
$this->hunter->putMapping($instance);
}

// Get entries by a specific locale
if ($locale && ($field = $this->hunter->config('locale_field'))) {
$instance->where($field, $locale);
}

// Index model
$this->line(' - Importing');

Expand Down
Loading

0 comments on commit 2aa6e77

Please sign in to comment.