Skip to content

Commit

Permalink
Allow for fallback to locale files, backwards compatibility with Lara…
Browse files Browse the repository at this point in the history
…vel 5.1 LTS, Clear cache after Fetch. #2
  • Loading branch information
Ubuntu committed Nov 15, 2016
1 parent ee88a4f commit 4f7a713
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 18 deletions.
8 changes: 8 additions & 0 deletions config/translation-db.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@
* happening.
*/
'minimal' => false,

/**
* Use locales from files as a fallback option. Be aware that
* locales are loaded as groups. When just one locale of a group
* exists in the database, a file will never be used.
* To use some files, keep these groups fully out of your database.
*/
'file_fallback' => false,
];
19 changes: 11 additions & 8 deletions src/Controllers/TranslationsController.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Hpolthof\Translation\Controllers;

use Hpolthof\Translation\ServiceProvider;
use Hpolthof\Translation\TranslationException;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
Expand All @@ -20,19 +21,21 @@ public function getIndex() {
}

public function getGroups() {
return \DB::table('translations')
$query = \DB::table('translations')
->select('group')
->distinct()
->orderBy('group')
->pluck('group');
->orderBy('group');

return ServiceProvider::pluckOrLists($query, 'group');
}

public function getLocales() {
return \DB::table('translations')
$query = \DB::table('translations')
->select('locale')
->distinct()
->orderBy('locale')
->pluck('locale');
->orderBy('locale');

return ServiceProvider::pluckOrLists($query, 'locale');
}

public function postItems(Request $request) {
Expand All @@ -48,8 +51,8 @@ public function postItems(Request $request) {
->select('name', 'value')
->where('locale', strtolower($request->get('translate')))
->where('group', $request->get('group'))
->orderBy('name')
->pluck('value', 'name');
->orderBy('name');
$new = ServiceProvider::pluckOrLists($new, 'value', 'name');

foreach($base as &$item) {
$translate = null;
Expand Down
7 changes: 4 additions & 3 deletions src/DatabaseLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ public function __construct(Application $app)
*/
public function load($locale, $group, $namespace = null)
{
return \DB::table('translations')
$query = \DB::table('translations')
->where('locale', $locale)
->where('group', $group)
->pluck('value', 'name');
->where('group', $group);

return ServiceProvider::pluckOrLists($query, 'value', 'name');
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php namespace Hpolthof\Translation;

use Illuminate\Database\Query\Builder;
use Illuminate\Translation\FileLoader;

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

/**
* Alternative pluck to stay backwards compatible with Laravel 5.1 LTS.
*
* @param Builder $query
* @param $column
* @param null $key
* @return array|mixed
*/
public static function pluckOrLists(Builder $query, $column, $key = null)
{
if(\Illuminate\Foundation\Application::VERSION < '5.2') {
$result = $query->lists($column, $key);
} else {
$result = $query->pluck($column, $key);
}

return $result;
}
}
35 changes: 28 additions & 7 deletions src/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,20 @@ public function get($key, array $replace = array(), $locale = null, $fallback =
// the translator was instantiated. Then, we can load the lines and return.
foreach ($this->parseLocale($locale) as $locale)
{
if(!self::isNamespaced($namespace)) {
// Database stuff
$this->database->addTranslation($locale, $group, $key);
}

$this->load($namespace, $group, $locale);

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

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

if ( ! is_null($line)) break;
}

Expand All @@ -72,14 +75,32 @@ public function load($namespace, $group, $locale)
if(!\Config::get('app.debug') || \Config::get('translation-db.minimal')) {
$that = $this;
$lines = \Cache::rememberForever('__translations.'.$locale.'.'.$group, function() use ($that, $locale, $group, $namespace) {
return $this->database->load($locale, $group, $namespace);
return $that->loadFromDatabase($namespace, $group, $locale);
});
} else {
$lines = $this->database->load($locale, $group, $namespace);
$lines = $this->loadFromDatabase($namespace, $group, $locale);
}
} else {
$lines = $this->loader->load($locale, $group, $namespace);
}
$this->loaded[$namespace][$group][$locale] = $lines;
}

/**
* @param $namespace
* @param $group
* @param $locale
* @return array
*/
protected function loadFromDatabase($namespace, $group, $locale)
{
$lines = $this->database->load($locale, $group, $namespace);

if (count($lines) == 0 && \Config::get('translation-db.file_fallback', false)) {
$lines = $this->loader->load($locale, $group, $namespace);
return $lines;
}

return $lines;
}
}

0 comments on commit 4f7a713

Please sign in to comment.