Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the Lang menu filter (stage 2) #1269

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 20 additions & 27 deletions src/Menu/Filters/LangFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,44 @@

namespace JeroenNoten\LaravelAdminLte\Menu\Filters;

use Illuminate\Translation\Translator;
use Illuminate\Support\Facades\Lang;

class LangFilter implements FilterInterface
{
/**
* The translator instance.
*
* @var Translator
*/
protected $translator;

/**
* The array of menu item properties to translate.
* The array of menu item's properties that can be translated.
*
* @var array
*/
protected $itemProperties;

/**
* Constructor.
*
* @param Translator $translator
*/
public function __construct(Translator $translator)
public function __construct()
{
$this->translator = $translator;
$this->itemProperties = ['header', 'text', 'label'];
}

/**
* Transforms a menu item. Makes the item translations.
* Transforms a menu item. Makes the translations on the expected item
* properties.
*
* @param array $item A menu item
* @return array The transformed menu item
* @return array
*/
public function transform($item)
{
// Translate the menu item properties.
// Translate the expected menu item properties.

foreach ($this->itemProperties as $prop) {
// Check if the property exists for the item.
// Check if the property exists in the item.

if (! isset($item[$prop])) {
if (empty($item[$prop])) {
continue;
}

// Check if the property value is valid for be translated.
// Check if the property value is valid to be translated.

if (is_array($item[$prop])) {
$params = $item[$prop][1] ?? [];
Expand All @@ -66,20 +57,22 @@ public function transform($item)
* Gets the translation for a given key.
*
* @param string $key The key to translate
* @param array $params The additional translation params
* @return string The translation
* @param array $params The additional translation parameters
* @return string
*/
protected function getTranslation($key, $params = [])
{
// Check for a translation.
// Check for a translation. Note we first check if translations are
// available in a "menu.php" file, then we check for translations in
// the published language resources (under the adminlte namespace).

if ($this->translator->has('menu.'.$key)) {
return $this->translator->get('menu.'.$key, $params);
} elseif ($this->translator->has('adminlte::menu.'.$key)) {
return $this->translator->get('adminlte::menu.'.$key, $params);
if (Lang::has("menu.{$key}")) {
return Lang::get("menu.{$key}", $params);
} elseif (Lang::has("adminlte::menu.{$key}")) {
return Lang::get("adminlte::menu.{$key}", $params);
}

// When no translation available, return the original key.
// When there is no translation available, return the original key.

return $key;
}
Expand Down
75 changes: 0 additions & 75 deletions tests/Menu/BuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -765,79 +765,4 @@ public function testCanOnWholeRestrictedSubmenu()

$this->assertCount(0, $builder->menu);
}

public function testLangTranslate()
{
$builder = $this->makeMenuBuilder('http://example.com');
$builder->add(['header' => 'profile']);
$builder->add(['text' => 'profile', 'url' => '/profile', 'label' => 'labels']);
$builder->add(['text' => 'blog', 'url' => '/blog']);
$builder->add(['header' => 'TEST']);
$this->assertCount(4, $builder->menu);
$this->assertEquals('Profile', $builder->menu[0]['header']);
$this->assertEquals('Profile', $builder->menu[1]['text']);
$this->assertEquals('LABELS', $builder->menu[1]['label']);
$this->assertEquals('Blog', $builder->menu[2]['text']);
$this->assertEquals('TEST', $builder->menu[3]['header']);

$builder = $this->makeMenuBuilder('http://example.com', null, 'de');
$builder->add(['header' => 'profile']);
$builder->add(['text' => 'profile', 'url' => '/profile', 'label' => 'labels']);
$builder->add(['text' => 'blog', 'url' => '/blog']);
$builder->add(['header' => 'TEST']);
$this->assertCount(4, $builder->menu);
$this->assertEquals('Profil', $builder->menu[0]['header']);
$this->assertEquals('Profil', $builder->menu[1]['text']);
$this->assertEquals('Beschriftungen', $builder->menu[1]['label']);
$this->assertEquals('Blog', $builder->menu[2]['text']);
$this->assertEquals('TEST', $builder->menu[3]['header']);
}

public function testLangTranslateWithExtraParams()
{
$builder = $this->makeMenuBuilder('http://example.com', null, 'es');

$lines = [
'menu.header_with_params' => 'MENU :cat / :subcat',
'menu.profile_with_params' => 'Perfil de :name',
'menu.label_with_params' => 'Etiqueta :type',
];

$translator = $this->getTranslator();
$translator->addLines($lines, 'es', 'adminlte');

$builder->add(
[
'header' => [
'header_with_params',
['cat' => 'CAT', 'subcat' => 'SUBCAT'],
],
],
[
'text' => ['profile_with_params', ['name' => 'Diego']],
'url' => '/profile',
'label' => ['label_with_params', ['type' => 'Tipo']],
],
[
// Test case with partial parameters.
'header' => ['header_with_params', ['subcat' => 'SUBCAT']],
],
[
// Test case with empty parameters.
'header' => ['header_with_params'],
],
[
// Test case with non-array parameters.
'header' => ['header_with_params', 'non-array-value'],
]
);

$this->assertCount(5, $builder->menu);
$this->assertEquals('MENU CAT / SUBCAT', $builder->menu[0]['header']);
$this->assertEquals('Perfil de Diego', $builder->menu[1]['text']);
$this->assertEquals('Etiqueta Tipo', $builder->menu[1]['label']);
$this->assertEquals('MENU :cat / SUBCAT', $builder->menu[2]['header']);
$this->assertEquals('MENU :cat / :subcat', $builder->menu[3]['header']);
$this->assertEquals('MENU :cat / :subcat', $builder->menu[4]['header']);
}
}
172 changes: 172 additions & 0 deletions tests/Menu/LangFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

class LangFilterTest extends TestCase
{
public function testEnglishTranslations()
{
// Add translation lines and setup the locale.

$lines = [
'menu.header_x' => 'Header X',
'menu.home' => 'Home',
'menu.home_label' => 'Home Label',
'menu.about' => 'About',
];

$this->app->make('translator')->addLines($lines, 'en', 'adminlte');
$this->app->setLocale('en');

// Setup menu.

$builder = $this->makeMenuBuilder();
$builder->add(['header' => 'header_x']);
$builder->add(['text' => 'home', 'url' => 'home', 'label' => 'home_label']);
$builder->add(['text' => 'about', 'url' => 'about']);
$builder->add(['header' => 'header_y']);

// Check translations.

$this->assertEquals('Header X', $builder->menu[0]['header']);
$this->assertEquals('Home', $builder->menu[1]['text']);
$this->assertEquals('Home Label', $builder->menu[1]['label']);
$this->assertEquals('About', $builder->menu[2]['text']);
$this->assertEquals('header_y', $builder->menu[3]['header']);
}

public function testSpanishTranslations()
{
// Add translation lines and setup the locale.

$lines = [
'menu.header_x' => 'Encabezado X',
'menu.home' => 'Inicio',
'menu.home_label' => 'Etiqueta de inicio',
'menu.about' => 'Acerca de',
];

$this->app->make('translator')->addLines($lines, 'es', 'adminlte');
$this->app->setLocale('es');

// Setup menu.

$builder = $this->makeMenuBuilder();
$builder->add(['header' => 'header_x']);
$builder->add(['text' => 'home', 'url' => 'home', 'label' => 'home_label']);
$builder->add(['text' => 'about', 'url' => 'about']);
$builder->add(['header' => 'header_y']);

// Check translations.

$this->assertEquals('Encabezado X', $builder->menu[0]['header']);
$this->assertEquals('Inicio', $builder->menu[1]['text']);
$this->assertEquals('Etiqueta de inicio', $builder->menu[1]['label']);
$this->assertEquals('Acerca de', $builder->menu[2]['text']);
$this->assertEquals('header_y', $builder->menu[3]['header']);
}

public function testTranslationsWithoutAdminlteNamespace()
{
// Add translation lines and setup the locale.

$lines = [
'menu.header_x' => 'Header X',
'menu.home' => 'Home',
'menu.home_label' => 'Home Label',
'menu.about' => 'About',
];

$this->app->make('translator')->addLines($lines, 'en');
$this->app->setLocale('en');

// Setup menu.

$builder = $this->makeMenuBuilder();
$builder->add(['header' => 'header_x']);
$builder->add(['text' => 'home', 'url' => 'home', 'label' => 'home_label']);
$builder->add(['text' => 'about', 'url' => 'about']);
$builder->add(['header' => 'header_y']);

// Check translations.

$this->assertEquals('Header X', $builder->menu[0]['header']);
$this->assertEquals('Home', $builder->menu[1]['text']);
$this->assertEquals('Home Label', $builder->menu[1]['label']);
$this->assertEquals('About', $builder->menu[2]['text']);
$this->assertEquals('header_y', $builder->menu[3]['header']);
}

public function testTranslationsWithoutNamespaceHasPriority()
{
// Add translation lines and setup the locale.

$lines = ['menu.home' => 'Home A'];
$this->app->make('translator')->addLines($lines, 'en', 'adminlte');

$lines = ['menu.home' => 'Home B'];
$this->app->make('translator')->addLines($lines, 'en');

$this->app->setLocale('en');

// Setup menu.

$builder = $this->makeMenuBuilder();
$builder->add(['text' => 'home', 'url' => 'home']);

// Check translations.

$this->assertEquals('Home B', $builder->menu[0]['text']);
}

public function testTranslationWithParams()
{
// Add translation lines and setup the locale.

$lines = [
'menu.header_with_params' => 'MENU :cat / :subcat',
'menu.profile_with_params' => 'Perfil de :name',
'menu.label_with_params' => 'Etiqueta :type',
];

$this->app->make('translator')->addLines($lines, 'es', 'adminlte');
$this->app->setLocale('es');

// Setup menu.

$builder = $this->makeMenuBuilder();

$builder->add(
[
'header' => [
'header_with_params',
['cat' => 'CAT', 'subcat' => 'SUBCAT'],
],
],
[
'text' => ['profile_with_params', ['name' => 'Diego']],
'url' => 'profile',
'label' => ['label_with_params', ['type' => 'Tipo']],
],
[
// Test case with partial parameters.
'header' => ['header_with_params', ['subcat' => 'SUBCAT']],
],
[
// Test case with empty parameters.
'header' => ['header_with_params'],
],
[
// Test case with non-array parameters.
'header' => ['header_with_params', 'non-array-value'],
]
);

// Check translations.

$this->assertEquals('MENU CAT / SUBCAT', $builder->menu[0]['header']);
$this->assertEquals('Perfil de Diego', $builder->menu[1]['text']);
$this->assertEquals('Etiqueta Tipo', $builder->menu[1]['label']);
$this->assertEquals('MENU :cat / SUBCAT', $builder->menu[2]['header']);
$this->assertEquals('MENU :cat / :subcat', $builder->menu[3]['header']);
$this->assertEquals('MENU :cat / :subcat', $builder->menu[4]['header']);
}
}
Loading