Skip to content

Commit 2aa6e77

Browse files
committed
The old multilingual approach didn't work...this one does
1 parent e55da77 commit 2aa6e77

9 files changed

+226
-88
lines changed

config/hunt.php

+38-14
Original file line numberDiff line numberDiff line change
@@ -34,20 +34,6 @@
3434

3535
'index' => 'default',
3636

37-
/*
38-
|--------------------------------------------------------------------------
39-
| Multilingual Support
40-
|--------------------------------------------------------------------------
41-
|
42-
| Use this to set the global locale field used in your models. When set
43-
| Laravel Hunt will only perform searches in the current system locale.
44-
|
45-
| 'locale_field' => 'locale',
46-
|
47-
*/
48-
49-
'locale_field' => null,
50-
5137
/*
5238
|--------------------------------------------------------------------------
5339
| Unified Types
@@ -106,4 +92,42 @@
10692
*/
10793

10894
'settings' => null,
95+
96+
/*
97+
|--------------------------------------------------------------------------
98+
| Model Namespace
99+
|--------------------------------------------------------------------------
100+
|
101+
| Change this if you use a different model namespace for Laravel.
102+
|
103+
*/
104+
105+
'model_namespace' => '\\App\\',
106+
107+
/*
108+
|--------------------------------------------------------------------------
109+
| Multilingual Support
110+
|--------------------------------------------------------------------------
111+
|
112+
| Use this to set support for multiple languages. Basically it suffixes
113+
| the type with the locale code.
114+
|
115+
| For this to work, the model will need to use the `Localized` trait. Or
116+
| something similar.
117+
|
118+
*/
119+
120+
'multilingual' => false,
121+
122+
/*
123+
|--------------------------------------------------------------------------
124+
| Support Locales
125+
|--------------------------------------------------------------------------
126+
|
127+
| This is used in the command line to import and map models. If using the
128+
| package ``
129+
|
130+
*/
131+
132+
'support_locales' => [],
109133
];

readme.md

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Unified search for Laravel models using Elasticsearch. Laravel Hunt uses the [of
1111

1212
# Elasticsearch Requirements
1313

14-
You must be running Elasticsearch 1.0 or higher.
14+
You must be running Elasticsearch 5.0 or higher.
1515

1616
## Installation
1717

@@ -107,14 +107,13 @@ Create the Elasticsearch index.
107107

108108
Remove the Elasticsearch index.
109109

110-
#### `hunt:map <action> <model>`
110+
#### `hunt:map <model>`
111111

112-
Initialize an Eloquent model.
112+
Initialize an Eloquent model map.
113113

114114
Arguments:
115115

116116
```
117-
action Mapping action to perform (add or remove)
118117
model Name or comma separated names of the model(s) to initialize
119118
```
120119

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

219-
## Change Log
218+
## Multilingual
220219

221-
#### v0.1.0
220+
> This feature is experimental
222221
223-
- First release
222+
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`.
223+
224+
For more information see the config file for more details.

src/Builder.php

-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
namespace LaravelHunt;
44

5-
use Illuminate\Database\Eloquent\Collection;
6-
75
class Builder
86
{
97
/**

src/Console/AbstractCommand.php

+55-7
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use LaravelHunt\Hunter;
66
use Illuminate\Support\Str;
77
use Illuminate\Console\Command;
8+
use Torann\Localization\LocaleManager;
89

910
abstract class AbstractCommand extends Command
1011
{
@@ -13,6 +14,13 @@ abstract class AbstractCommand extends Command
1314
*/
1415
protected $hunter;
1516

17+
/**
18+
* Namespace for models.
19+
*
20+
* @var string
21+
*/
22+
protected $models;
23+
1624
/**
1725
* Create a new console command instance.
1826
*
@@ -23,6 +31,21 @@ public function __construct(Hunter $hunter)
2331
parent::__construct();
2432

2533
$this->hunter = $hunter;
34+
$this->models = config('hunt.model_namespace', '\\App\\');
35+
}
36+
37+
/**
38+
* Perform action model mapping.
39+
*
40+
* @param string $action
41+
*/
42+
protected function processModels($action)
43+
{
44+
foreach ($this->getModelArgument() as $model) {
45+
if ($model = $this->validateModel("{$this->models}{$model}")) {
46+
$this->$action($model);
47+
}
48+
}
2649
}
2750

2851
/**
@@ -61,13 +84,41 @@ protected function getModelArgument()
6184
}
6285

6386
/**
64-
* Get locale option.
87+
* Get an array of supported locales.
6588
*
66-
* @return array
89+
* @return array|null
90+
*/
91+
protected function getLocales()
92+
{
93+
// Get user specified locales
94+
if ($locales = $this->option('locales')) {
95+
return array_filter(explode(',', preg_replace('/\s+/', '', $locales)));
96+
}
97+
98+
// Check for package
99+
if (class_exists('Torann\\Localization\\LocaleManager')) {
100+
return app(LocaleManager::class)->getSupportedLanguagesKeys();
101+
}
102+
103+
return config('hunt.support_locales');
104+
}
105+
106+
/**
107+
* Get an array of supported locales.
108+
*
109+
* @param string $locale
67110
*/
68-
protected function getLocaleOption()
111+
protected function setSystemLocale($locale)
69112
{
70-
return array_filter(explode(',', preg_replace('/\s+/', '', $this->option('locales'))));
113+
$this->line('');
114+
$this->line("System local set to: <info>{$locale}</info>");
115+
116+
if (class_exists('Torann\\Localization\\LocaleManager')) {
117+
app(LocaleManager::class)->setLocale($locale);
118+
}
119+
else {
120+
app()->setLocale($locale);
121+
}
71122
}
72123

73124
/**
@@ -78,9 +129,6 @@ protected function getLocaleOption()
78129
*/
79130
protected function validateModel($model)
80131
{
81-
// Determine the namespace
82-
$model = ($model[0] !== '\\') ? "\\App\\{$model}" : $model;
83-
84132
// Verify model existence
85133
if (class_exists($model) === false) {
86134
$this->error("Model [{$model}] not found");

src/Console/FlushCommand.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class FlushCommand extends AbstractCommand
1212
* @var string
1313
*/
1414
protected $signature = 'hunt:flush
15-
{model : Name or comma separated names of the model(s) to index}';
15+
{model : Name or comma separated names of the model(s) to index}
16+
{--l|locales= : Single or comma separated locales to index}';
1617

1718
/**
1819
* The console command description.
@@ -28,11 +29,16 @@ class FlushCommand extends AbstractCommand
2829
*/
2930
public function handle()
3031
{
31-
foreach ($this->getModelArgument() as $model) {
32-
if ($model = $this->validateModel($model)) {
33-
$this->flush($model);
32+
if (empty($locales = $this->getLocales()) === false) {
33+
foreach ($locales as $locale) {
34+
$this->setSystemLocale($locale);
35+
36+
$this->processModels('flush');
3437
}
3538
}
39+
else {
40+
$this->processModels('flush');
41+
}
3642
}
3743

3844
/**

src/Console/ImportCommand.php

+8-18
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,26 @@ class ImportCommand extends AbstractCommand
2727
*/
2828
public function handle()
2929
{
30-
$locales = $this->getLocaleOption();
30+
if (empty($locales = $this->getLocales()) === false) {
31+
foreach ($locales as $locale) {
32+
$this->setSystemLocale($locale);
3133

32-
foreach ($this->getModelArgument() as $model) {
33-
if ($model = $this->validateModel($model)) {
34-
if (empty($locales) === false) {
35-
foreach ($locales as $locale) {
36-
$this->index($model, $locale);
37-
}
38-
}
39-
else {
40-
$this->index($model);
41-
}
34+
$this->processModels('index');
4235
}
4336
}
37+
else {
38+
$this->processModels('index');
39+
}
4440
}
4541

4642
/**
4743
* Index all model entries to ElasticSearch.
4844
*
4945
* @param string $model
50-
* @param string $locale
5146
*
5247
* @return bool
5348
*/
54-
protected function index($model, $locale = '')
49+
protected function index($model)
5550
{
5651
$this->comment("Processing [{$model}]");
5752

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

67-
// Get entries by a specific locale
68-
if ($locale && ($field = $this->hunter->config('locale_field'))) {
69-
$instance->where($field, $locale);
70-
}
71-
7262
// Index model
7363
$this->line(' - Importing');
7464

0 commit comments

Comments
 (0)