Skip to content

Commit

Permalink
5.4 support (#12) (#13)
Browse files Browse the repository at this point in the history
* Add str_before if not exists
Doc blocks

* Different database loaders to satisfy Laravel <= 5.4

* Check for value

* Update language

* Formatting

* Update return type
  • Loading branch information
joedixon authored Sep 26, 2018
1 parent 42f8b64 commit 815e242
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 12 deletions.
41 changes: 38 additions & 3 deletions resources/helpers.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
<?php

if (! function_exists('set_active')) {
/**
* Determine if a route is the currently active route.
*
* @param string $path
* @param string $class
* @return string
*/
function set_active($path, $class = 'active')
{
return Request::is(config('translation.ui_url').$path) ? $class : '';
}
}

if (! function_exists('strs_contain')) {
/**
* Determine whether any of the provided strings in the haystack contain the needle.
*
* @param array $haystacks
* @param string $needle
* @return bool
*/
function strs_contain($haystacks, $needle)
{
$haystacks = (array) $haystacks;
Expand All @@ -23,25 +37,46 @@ function strs_contain($haystacks, $needle)
}

if (! function_exists('array_diff_assoc_recursive')) {
/**
* Recursively diff two arrays.
*
* @param array $arrayOne
* @param array $arrayTwo
* @return array
*/
function array_diff_assoc_recursive($arrayOne, $arrayTwo)
{
foreach ($arrayOne as $key => $value) {
if (is_array($value)) {
if (is_array($value) || $value instanceof Illuminate\Support\Collection) {
if (! isset($arrayTwo[$key])) {
$difference[$key] = $value;
} elseif (! is_array($arrayTwo[$key])) {
} elseif (! (is_array($arrayTwo[$key]) || $arrayTwo[$key] instanceof Illuminate\Support\Collection)) {
$difference[$key] = $value;
} else {
$new_diff = array_diff_assoc_recursive($value, $arrayTwo[$key]);
if ($new_diff != false) {
$difference[$key] = $new_diff;
}
}
} elseif (! isset($arrayTwo[$key]) || $arrayTwo[$key] != $value) {
} elseif (! isset($arrayTwo[$key])) {
$difference[$key] = $value;
}
}

return ! isset($difference) ? false : $difference;
}
}

if (! function_exists('str_before')) {
/**
* Get the portion of a string before a given value.
*
* @param string $subject
* @param string $search
* @return string
*/
function str_before($subject, $search)
{
return $search === '' ? $subject : explode($search, $subject)[0];
}
}
2 changes: 1 addition & 1 deletion src/Console/Commands/ListLanguagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ListLanguagesCommand extends BaseCommand
*/
public function handle()
{
$headers = [__('translation::translation.name'), __('translation::translation.language')];
$headers = [__('translation::translation.language_name'), __('translation::translation.language')];
$languages = $this->translation->allLanguages()->toArray();
$mappedLanguages = [];

Expand Down
6 changes: 3 additions & 3 deletions src/Console/Commands/ListMissingTranslationKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function handle()
}

// set some headers for the table of results
$headers = [__('translation::translation.language'), __('translation::translation.type'), __('translation::translation.file'), __('translation::translation.key')];
$headers = [__('translation::translation.language'), __('translation::translation.type'), __('translation::translation.group'), __('translation::translation.key')];

// iterate over each of the missing languages
foreach ($missingTranslations as $language => $types) {
Expand All @@ -59,12 +59,12 @@ public function handle()
// due to the need to store the filename, so handle accordingly
if (is_array($value)) {
foreach ($value as $k => $v) {
$rows[] = [$language, $type, "{$key}.php", $k];
$rows[] = [$language, $type, $key, $k];
}
}
// handle json file type
else {
$rows[] = [$language, $type, "{$language}.json", $key];
$rows[] = [$language, $type, strtolower(__('translation::translation.single')), $key];
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/DatabaseLoader.php → src/ContractDatabaseLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Illuminate\Contracts\Translation\Loader;
use JoeDixon\Translation\Drivers\Translation;

class DatabaseLoader implements Loader
class ContractDatabaseLoader implements Loader
{
private $translation;

Expand Down
6 changes: 3 additions & 3 deletions src/Http/Controllers/LanguageTranslationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function index(Request $request, $language)
$groups = $this->translation->getGroupsFor(config('app.locale'))->prepend('single');
$translations = $this->translation->filterTranslationsFor($language, $request->get('filter'));

if ($request->filled('group')) {
if ($request->has('group') && $request->get('group')) {
if ($request->get('group') === 'single') {
$translations = $translations->get('single');
$translations = new Collection(['single' => $translations]);
Expand All @@ -51,8 +51,8 @@ public function create(Request $request, $language)

public function store(TranslationRequest $request, $language)
{
if ($request->filled('group')) {
$namespace = $request->filled('namespace') ? "{$request->get('namespace')}::" : '';
if ($request->has('group') && $request->get('group')) {
$namespace = $request->has('namespace') && $request->get('namespace') ? "{$request->get('namespace')}::" : '';
$this->translation->addGroupTranslation($language, "{$namespace}{$request->get('group')}.{$request->get('key')}", $request->get('value') ?: '');
} else {
$this->translation->addSingleTranslation($language, $request->get('key'), $request->get('value') ?: '');
Expand Down
74 changes: 74 additions & 0 deletions src/InterfaceDatabaseLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace JoeDixon\Translation;

use Illuminate\Translation\LoaderInterface;
use JoeDixon\Translation\Drivers\Translation;

class InterfaceDatabaseLoader implements LoaderInterface
{
private $translation;

public function __construct(Translation $translation)
{
$this->translation = $translation;
}

/**
* Load the messages for the given locale.
*
* @param string $locale
* @param string $group
* @param string $namespace
* @return array
*/
public function load($locale, $group, $namespace = null)
{
if ($group == '*' && $namespace == '*') {
return $this->translation->getSingleTranslationsFor($locale)->toArray();
}

if (is_null($namespace) || $namespace == '*') {
return $this->translation->getGroupTranslationsFor($locale)->filter(function ($value, $key) use ($group) {
return $key === $group;
})->first();
}

return $this->translation->getGroupTranslationsFor($locale)->filter(function ($value, $key) use ($group, $namespace) {
return $key === "{$namespace}::{$group}";
})->first();
}

/**
* Add a new namespace to the loader.
*
* @param string $namespace
* @param string $hint
* @return void
*/
public function addNamespace($namespace, $hint)
{
//
}

/**
* Add a new JSON path to the loader.
*
* @param string $path
* @return void
*/
public function addJsonPath($path)
{
//
}

/**
* Get an array of all the registered namespaces.
*
* @return array
*/
public function namespaces()
{
return [];
}
}
9 changes: 8 additions & 1 deletion src/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,14 @@ private function registerDatabaseLoader()
private function registerLoader()
{
$this->app->singleton('translation.loader', function ($app) {
return new DatabaseLoader($this->app->make(Translation::class));
// Post Laravel 5.4, the interface was moved to the contracts
// directory. Here we perform a check to see whether or not the
// interface exists and instantiate the relevant loader accordingly.
if (interface_exists('Illuminate\Contracts\Translation\Loader')) {
return new ContractDatabaseLoader($this->app->make(Translation::class));
}

return new InterfaceDatabaseLoader($this->app->make(Translation::class));
});
}
}

0 comments on commit 815e242

Please sign in to comment.