From bc5e90330121c14728636f6334d91c969c4e64d0 Mon Sep 17 00:00:00 2001 From: Diego Smania Date: Sat, 13 Apr 2024 17:59:13 -0300 Subject: [PATCH] [src/Filters]: Improve the Lang menu filter --- src/Menu/Filters/LangFilter.php | 47 ++++----- tests/Menu/BuilderTest.php | 75 -------------- tests/Menu/LangFilterTest.php | 172 ++++++++++++++++++++++++++++++++ tests/TestCase.php | 26 +---- 4 files changed, 195 insertions(+), 125 deletions(-) create mode 100644 tests/Menu/LangFilterTest.php diff --git a/src/Menu/Filters/LangFilter.php b/src/Menu/Filters/LangFilter.php index a690b8e3..af7f0b58 100644 --- a/src/Menu/Filters/LangFilter.php +++ b/src/Menu/Filters/LangFilter.php @@ -2,19 +2,12 @@ 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 */ @@ -22,33 +15,31 @@ class LangFilter implements FilterInterface /** * 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] ?? []; @@ -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; } diff --git a/tests/Menu/BuilderTest.php b/tests/Menu/BuilderTest.php index 0c81f0ba..baf2a948 100644 --- a/tests/Menu/BuilderTest.php +++ b/tests/Menu/BuilderTest.php @@ -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']); - } } diff --git a/tests/Menu/LangFilterTest.php b/tests/Menu/LangFilterTest.php new file mode 100644 index 00000000..42800172 --- /dev/null +++ b/tests/Menu/LangFilterTest.php @@ -0,0 +1,172 @@ + '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']); + } +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 6f93e60a..cd67c35f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -23,8 +23,6 @@ class TestCase extends BaseTestCase { private $routeCollection; - private $translator; - /** * Load the package services providers. * @@ -42,32 +40,19 @@ protected function getPackageProviders($app) * * @return Builder */ - protected function makeMenuBuilder( - $uri = 'http://example.com', - GateContract $gate = null, - $locale = 'en' - ) { + protected function makeMenuBuilder($uri = 'http://example.com', GateContract $gate = null) + { return new Builder([ new GateFilter($gate ?: $this->makeGate()), new HrefFilter($this->makeUrlGenerator($uri)), new ActiveFilter($this->makeActiveChecker($uri)), new ClassesFilter(), new DataFilter(), - new LangFilter($this->makeTranslator($locale)), + new LangFilter(), new SearchFilter(), ]); } - protected function makeTranslator($locale = 'en') - { - $translationLoader = new Illuminate\Translation\FileLoader(new Illuminate\Filesystem\Filesystem, 'resources/lang/'); - - $this->translator = new Illuminate\Translation\Translator($translationLoader, $locale); - $this->translator->addNamespace('adminlte', 'resources/lang/'); - - return $this->translator; - } - protected function makeActiveChecker($uri = 'http://example.com', $scheme = null) { return new ActiveChecker($this->makeUrlGenerator($uri, $scheme)); @@ -114,9 +99,4 @@ protected function getRouteCollection() return $this->routeCollection; } - - protected function getTranslator() - { - return $this->translator; - } }