Skip to content

Commit 4f7a713

Browse files
author
Ubuntu
committed
Allow for fallback to locale files, backwards compatibility with Laravel 5.1 LTS, Clear cache after Fetch. #2
1 parent ee88a4f commit 4f7a713

File tree

5 files changed

+70
-18
lines changed

5 files changed

+70
-18
lines changed

config/translation-db.php

+8
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,12 @@
3434
* happening.
3535
*/
3636
'minimal' => false,
37+
38+
/**
39+
* Use locales from files as a fallback option. Be aware that
40+
* locales are loaded as groups. When just one locale of a group
41+
* exists in the database, a file will never be used.
42+
* To use some files, keep these groups fully out of your database.
43+
*/
44+
'file_fallback' => false,
3745
];

src/Controllers/TranslationsController.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Hpolthof\Translation\Controllers;
22

3+
use Hpolthof\Translation\ServiceProvider;
34
use Hpolthof\Translation\TranslationException;
45
use Illuminate\Http\Request;
56
use Illuminate\Routing\Controller;
@@ -20,19 +21,21 @@ public function getIndex() {
2021
}
2122

2223
public function getGroups() {
23-
return \DB::table('translations')
24+
$query = \DB::table('translations')
2425
->select('group')
2526
->distinct()
26-
->orderBy('group')
27-
->pluck('group');
27+
->orderBy('group');
28+
29+
return ServiceProvider::pluckOrLists($query, 'group');
2830
}
2931

3032
public function getLocales() {
31-
return \DB::table('translations')
33+
$query = \DB::table('translations')
3234
->select('locale')
3335
->distinct()
34-
->orderBy('locale')
35-
->pluck('locale');
36+
->orderBy('locale');
37+
38+
return ServiceProvider::pluckOrLists($query, 'locale');
3639
}
3740

3841
public function postItems(Request $request) {
@@ -48,8 +51,8 @@ public function postItems(Request $request) {
4851
->select('name', 'value')
4952
->where('locale', strtolower($request->get('translate')))
5053
->where('group', $request->get('group'))
51-
->orderBy('name')
52-
->pluck('value', 'name');
54+
->orderBy('name');
55+
$new = ServiceProvider::pluckOrLists($new, 'value', 'name');
5356

5457
foreach($base as &$item) {
5558
$translate = null;

src/DatabaseLoader.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ public function __construct(Application $app)
2222
*/
2323
public function load($locale, $group, $namespace = null)
2424
{
25-
return \DB::table('translations')
25+
$query = \DB::table('translations')
2626
->where('locale', $locale)
27-
->where('group', $group)
28-
->pluck('value', 'name');
27+
->where('group', $group);
28+
29+
return ServiceProvider::pluckOrLists($query, 'value', 'name');
2930
}
3031

3132
/**

src/ServiceProvider.php

+19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace Hpolthof\Translation;
22

3+
use Illuminate\Database\Query\Builder;
34
use Illuminate\Translation\FileLoader;
45

56
class ServiceProvider extends \Illuminate\Translation\TranslationServiceProvider {
@@ -135,4 +136,22 @@ public function provides()
135136
return array('translator', 'translation.loader', 'translation.database');
136137
}
137138

139+
/**
140+
* Alternative pluck to stay backwards compatible with Laravel 5.1 LTS.
141+
*
142+
* @param Builder $query
143+
* @param $column
144+
* @param null $key
145+
* @return array|mixed
146+
*/
147+
public static function pluckOrLists(Builder $query, $column, $key = null)
148+
{
149+
if(\Illuminate\Foundation\Application::VERSION < '5.2') {
150+
$result = $query->lists($column, $key);
151+
} else {
152+
$result = $query->pluck($column, $key);
153+
}
154+
155+
return $result;
156+
}
138157
}

src/Translator.php

+28-7
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,20 @@ public function get($key, array $replace = array(), $locale = null, $fallback =
3838
// the translator was instantiated. Then, we can load the lines and return.
3939
foreach ($this->parseLocale($locale) as $locale)
4040
{
41-
if(!self::isNamespaced($namespace)) {
42-
// Database stuff
43-
$this->database->addTranslation($locale, $group, $key);
44-
}
45-
4641
$this->load($namespace, $group, $locale);
4742

4843
$line = $this->getLine(
4944
$namespace, $group, $locale, $item, $replace
5045
);
5146

47+
// If we cannot find the translation group in the database nor as a file
48+
// an entry in the database will be added to the translations.
49+
// Keep in mind that a file cannot be used from that point.
50+
if(!self::isNamespaced($namespace) && is_null($line)) {
51+
// Database stuff
52+
$this->database->addTranslation($locale, $group, $key);
53+
}
54+
5255
if ( ! is_null($line)) break;
5356
}
5457

@@ -72,14 +75,32 @@ public function load($namespace, $group, $locale)
7275
if(!\Config::get('app.debug') || \Config::get('translation-db.minimal')) {
7376
$that = $this;
7477
$lines = \Cache::rememberForever('__translations.'.$locale.'.'.$group, function() use ($that, $locale, $group, $namespace) {
75-
return $this->database->load($locale, $group, $namespace);
78+
return $that->loadFromDatabase($namespace, $group, $locale);
7679
});
7780
} else {
78-
$lines = $this->database->load($locale, $group, $namespace);
81+
$lines = $this->loadFromDatabase($namespace, $group, $locale);
7982
}
8083
} else {
8184
$lines = $this->loader->load($locale, $group, $namespace);
8285
}
8386
$this->loaded[$namespace][$group][$locale] = $lines;
8487
}
88+
89+
/**
90+
* @param $namespace
91+
* @param $group
92+
* @param $locale
93+
* @return array
94+
*/
95+
protected function loadFromDatabase($namespace, $group, $locale)
96+
{
97+
$lines = $this->database->load($locale, $group, $namespace);
98+
99+
if (count($lines) == 0 && \Config::get('translation-db.file_fallback', false)) {
100+
$lines = $this->loader->load($locale, $group, $namespace);
101+
return $lines;
102+
}
103+
104+
return $lines;
105+
}
85106
}

0 commit comments

Comments
 (0)