From d68dec4deee9575cab75c8ba31b93e5af563726f Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Thu, 13 Dec 2018 11:43:43 -0500 Subject: [PATCH 01/14] Change default config to add locale name --- config/translation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/translation.php b/config/translation.php index 9d43040..364c2a3 100644 --- a/config/translation.php +++ b/config/translation.php @@ -14,7 +14,7 @@ */ 'available_locales' => [ - 'en' => ['system' => ['en_CA.UTF-8']], + 'en' => ['name' => 'English', 'system' => ['en_CA.UTF-8']], ], ]; From ceba9d1edb64c99853271514abf6ec32024d33b8 Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Thu, 13 Dec 2018 11:44:32 -0500 Subject: [PATCH 02/14] Add a getCurrentLocaleName and getLocaleName helper function --- src/LocaleService.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/LocaleService.php b/src/LocaleService.php index edb9bdc..6d998e3 100644 --- a/src/LocaleService.php +++ b/src/LocaleService.php @@ -194,4 +194,33 @@ public function setSystemLocale(string $locale = null) setlocale(LC_ALL, ...$systemConfig); } } + + /** + * Return the current locale name + * @return string + * @throws \Exolnet\Translation\TranslationException + */ + public function getCurrentLocaleName() + { + return $this->getLocaleName($this->getCurrentLocale()); + } + + /** + * Return the locale name for a specified locale + * @param string|null $locale + * @return string + * @throws \Exolnet\Translation\TranslationException + */ + public function getLocaleName(string $locale = null) + { + if (!$locale) { + $locale = $this->getCurrentLocale(); + } + + if (!isset($this->getLocalesAvailable()[$locale])) { + return ''; + } + + return array_get($this->getLocalesAvailable()[$locale], 'name', ''); + } } From 354bf99d232fe0c358183771e77777fc781aef54 Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Thu, 13 Dec 2018 11:44:54 -0500 Subject: [PATCH 03/14] Change default config to add locale native name --- config/translation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/translation.php b/config/translation.php index 364c2a3..056761a 100644 --- a/config/translation.php +++ b/config/translation.php @@ -14,7 +14,7 @@ */ 'available_locales' => [ - 'en' => ['name' => 'English', 'system' => ['en_CA.UTF-8']], + 'en' => ['name' => 'English', 'native' => 'English', 'system' => ['en_CA.UTF-8']], ], ]; From 1ad69da767ba420106e89d985b96d56ff9bbcc34 Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Thu, 13 Dec 2018 11:45:36 -0500 Subject: [PATCH 04/14] Add a getCurrentLocaleNativeName and getLocaleNativeName helper function --- src/LocaleService.php | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/LocaleService.php b/src/LocaleService.php index 6d998e3..a7e4e4a 100644 --- a/src/LocaleService.php +++ b/src/LocaleService.php @@ -223,4 +223,33 @@ public function getLocaleName(string $locale = null) return array_get($this->getLocalesAvailable()[$locale], 'name', ''); } + + /** + * Return the current locale native name + * @return string + * @throws \Exolnet\Translation\TranslationException + */ + public function getCurrentLocaleNativeName() + { + return $this->getLocaleNativeName($this->getCurrentLocale()); + } + + /** + * Return the locale native name for a specified locale + * @param string|null $locale + * @return string + * @throws \Exolnet\Translation\TranslationException + */ + public function getLocaleNativeName(string $locale = null) + { + if (!$locale) { + $locale = $this->getCurrentLocale(); + } + + if (!isset($this->getLocalesAvailable()[$locale])) { + return ''; + } + + return array_get($this->getLocalesAvailable()[$locale], 'native', ''); + } } From 79965b9fb8b5218927830dad2a66c3c58806e2fd Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Thu, 13 Dec 2018 13:47:00 -0500 Subject: [PATCH 05/14] Offer a blade view to generate a Language Switcher --- resources/views/language-switcher.blade.php | 11 +++++++++++ src/TranslationServiceProvider.php | 16 ++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 resources/views/language-switcher.blade.php diff --git a/resources/views/language-switcher.blade.php b/resources/views/language-switcher.blade.php new file mode 100644 index 0000000..53e3cd4 --- /dev/null +++ b/resources/views/language-switcher.blade.php @@ -0,0 +1,11 @@ +@forelse (URL::alternates(isset($alternateParameters) ? $alternateParameters : []) as $locale => $url) + + {{$locale}} + +@empty + @foreach (Route::getAlternateLocales() as $locale) + + {{$locale}} + + @endforeach +@endforelse diff --git a/src/TranslationServiceProvider.php b/src/TranslationServiceProvider.php index 3bcef25..8cbbaae 100644 --- a/src/TranslationServiceProvider.php +++ b/src/TranslationServiceProvider.php @@ -25,6 +25,11 @@ public function boot() $this->publishes([ $this->getConfigFile() => config_path('translation.php'), ], 'config'); + + $this->loadViewsFrom( + $this->getViewsPath(), + 'laravel-translation' + ); } /** @@ -127,4 +132,15 @@ protected function getConfigFile(): string DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'translation.php'; } + + /** + * @return string + */ + protected function getViewsPath(): string + { + return __DIR__ . + DIRECTORY_SEPARATOR . '..' . + DIRECTORY_SEPARATOR . 'resources' . + DIRECTORY_SEPARATOR . 'views'; + } } From 290ff367edfd1c1e660c5414b4ccecda8dc2c87d Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Thu, 13 Dec 2018 13:47:37 -0500 Subject: [PATCH 06/14] Offer some Facade Helper as Blade directive --- src/Blade/LanguageBladeExtension.php | 42 ++++++++++++++++++++++++++++ src/TranslationServiceProvider.php | 19 +++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 src/Blade/LanguageBladeExtension.php diff --git a/src/Blade/LanguageBladeExtension.php b/src/Blade/LanguageBladeExtension.php new file mode 100644 index 0000000..ed7d530 --- /dev/null +++ b/src/Blade/LanguageBladeExtension.php @@ -0,0 +1,42 @@ + [$this, 'getCurrentLocale'], + 'currentLocaleName' => [$this, 'getCurrentLocaleName'], + 'currentLocaleNativeName' => [$this, 'getCurrentLocaleNativeName'], + ]; + } + + /** + * @return string + */ + public function getCurrentLocale() + { + return ''; + } + + /** + * @return string + */ + public function getCurrentLocaleName() + { + return ''; + } + + /** + * @return string + */ + public function getCurrentLocaleNativeName() + { + return ''; + } +} diff --git a/src/TranslationServiceProvider.php b/src/TranslationServiceProvider.php index 8cbbaae..e063974 100644 --- a/src/TranslationServiceProvider.php +++ b/src/TranslationServiceProvider.php @@ -2,6 +2,7 @@ namespace Exolnet\Translation; +use Exolnet\Translation\Blade\LanguageBladeExtension; use Exolnet\Translation\Listeners\LocaleUpdatedListener; use Exolnet\Translation\Routing\Router; use Exolnet\Translation\Routing\UrlGenerator; @@ -26,6 +27,8 @@ public function boot() $this->getConfigFile() => config_path('translation.php'), ], 'config'); + $this->bootBladeExtensions(); + $this->loadViewsFrom( $this->getViewsPath(), 'laravel-translation' @@ -40,6 +43,7 @@ public function register() $this->registerRouter(); $this->registerUrlGenerator(); $this->registerLocaleService(); + $this->registerBladeExtensions(); $this->mergeConfigFrom($this->getConfigFile(), 'translation'); } @@ -143,4 +147,19 @@ protected function getViewsPath(): string DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'views'; } + + private function bootBladeExtensions() + { + foreach ($this->app->tagged('blade.extension') as $extension) { + foreach ($extension->getDirectives() as $name => $callable) { + $this->app['blade.compiler']->directive($name, $callable); + } + } + } + + private function registerBladeExtensions() + { + $this->app->singleton(LanguageBladeExtension::class); + $this->app->tag(LanguageBladeExtension::class, ['blade.extension']); + } } From c09febd06ebaec1830bfda9a9d6dc5d20a188ec2 Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Thu, 13 Dec 2018 13:48:27 -0500 Subject: [PATCH 07/14] Update Readme to provide information about the Facade, Blade views and directives --- README.md | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1c29293..11d5323 100644 --- a/README.md +++ b/README.md @@ -33,9 +33,7 @@ To make sure the routing system is using the one supporting the translation you Now you're ready to start using the translation in your application. -## Config - -### Config Files +#### Config Files In order to edit the default configuration (where for e.g. you can find `available_locales`) for this package you may execute: @@ -76,6 +74,25 @@ Route::groupLocales(function () { ``` +#### Facades + +Laravel Translation autoload a facade called `LaravelTranslation`. In this facade you will find functions like + +- `getCurrentLocale` to retrieve the current locale +- `getCurrentLocaleName` to retrieve the current locale name +- `getCurrentLocaleNativeName` to retrieve the current locale native name + +#### Blade views and directives +Laravel Translation offers some blade views and directives like + +- `@currentLocale` to show the current locale +- `@currentLocaleName` to show the current locale name +- `@currentLocaleNativeName` to show the current locale native name + +##### Language Switcher +In order to use the language switcher generated by LaravelTranslation you must include the `laravel-translation::language-switcher` in your blade templates like so : + +`@include('laravel-translation::language-switcher')` ## Testing From a4c105c6fde130cccc41e4044462fcfef9029d30 Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Thu, 13 Dec 2018 14:37:55 -0500 Subject: [PATCH 08/14] Fix language switcher blade space after locale code --- resources/views/language-switcher.blade.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/resources/views/language-switcher.blade.php b/resources/views/language-switcher.blade.php index 53e3cd4..4fab0fb 100644 --- a/resources/views/language-switcher.blade.php +++ b/resources/views/language-switcher.blade.php @@ -1,7 +1,5 @@ @forelse (URL::alternates(isset($alternateParameters) ? $alternateParameters : []) as $locale => $url) - - {{$locale}} - + {{$locale}} @empty @foreach (Route::getAlternateLocales() as $locale) From 5f2774b24b7159950bf8aa865e10b0e257b58132 Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Thu, 13 Dec 2018 14:39:45 -0500 Subject: [PATCH 09/14] Change default config to add locale script --- config/translation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/translation.php b/config/translation.php index 056761a..4be08c5 100644 --- a/config/translation.php +++ b/config/translation.php @@ -14,7 +14,7 @@ */ 'available_locales' => [ - 'en' => ['name' => 'English', 'native' => 'English', 'system' => ['en_CA.UTF-8']], + 'en' => ['name' => 'English', 'native' => 'English', 'script' => 'Latn', 'system' => ['en_CA.UTF-8']], ], ]; From 86ac8ce7ceffb752ccefdcaddaf16fed9af92471 Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Thu, 13 Dec 2018 14:41:09 -0500 Subject: [PATCH 10/14] Add a helper function, blade directive and blade condition to retreive the current locale direction --- src/Blade/LanguageBladeExtension.php | 38 ++++++++++++++++ src/LocaleService.php | 66 ++++++++++++++++++++++++++++ src/TranslationServiceProvider.php | 3 ++ 3 files changed, 107 insertions(+) diff --git a/src/Blade/LanguageBladeExtension.php b/src/Blade/LanguageBladeExtension.php index ed7d530..7f0f915 100644 --- a/src/Blade/LanguageBladeExtension.php +++ b/src/Blade/LanguageBladeExtension.php @@ -2,6 +2,8 @@ namespace Exolnet\Translation\Blade; +use Exolnet\Translation\Facades\LaravelTranslation; + class LanguageBladeExtension { /** @@ -13,6 +15,16 @@ public function getDirectives() 'currentLocale' => [$this, 'getCurrentLocale'], 'currentLocaleName' => [$this, 'getCurrentLocaleName'], 'currentLocaleNativeName' => [$this, 'getCurrentLocaleNativeName'], + 'currentLocaleScript' => [$this, 'getCurrentLocaleScript'], + 'currentLocaleDirection' => [$this, 'getCurrentLocaleDirection'], + ]; + } + + public function getConditionals() + { + return [ + 'isrtl' => [$this, 'isRtl'], + 'isltr' => [$this, 'isLtr'], ]; } @@ -39,4 +51,30 @@ public function getCurrentLocaleNativeName() { return ''; } + + /** + * @return string + */ + public function getCurrentLocaleScript() + { + return ''; + } + + /** + * @return string + */ + public function getCurrentLocaleDirection() + { + return ''; + } + + public function isRtl() + { + return !$this->isLtr(); + } + + public function isLtr() + { + return LaravelTranslation::getCurrentLocaleDirection() === "rtl"; + } } diff --git a/src/LocaleService.php b/src/LocaleService.php index a7e4e4a..8fbbfa7 100644 --- a/src/LocaleService.php +++ b/src/LocaleService.php @@ -252,4 +252,70 @@ public function getLocaleNativeName(string $locale = null) return array_get($this->getLocalesAvailable()[$locale], 'native', ''); } + + /** + * Return the current locale native name + * @return string + * @throws \Exolnet\Translation\TranslationException + */ + public function getCurrentLocaleScript() + { + return $this->getLocaleScript($this->getCurrentLocale()); + } + + /** + * Return the locale native name for a specified locale + * @param string|null $locale + * @return string + * @throws \Exolnet\Translation\TranslationException + */ + public function getLocaleScript(string $locale = null) + { + if (!$locale) { + $locale = $this->getCurrentLocale(); + } + + if (!isset($this->getLocalesAvailable()[$locale])) { + return ''; + } + + return array_get($this->getLocalesAvailable()[$locale], 'script', ''); + } + + /** + * Returns current locale direction. + * + * @return string current locale direction + * @throws \Exolnet\Translation\TranslationException + */ + public function getCurrentLocaleDirection() + { + return $this->getLocaleDirection($this->getCurrentLocale()); + } + + /** + * Returns locale direction. + * + * @param string|null $locale + * @return string current locale direction + * @throws \Exolnet\Translation\TranslationException + */ + public function getLocaleDirection(string $locale = null) + { + if (!$locale) { + $locale = $this->getCurrentLocale(); + } + + switch ($this->getLocaleScript($locale)) { + // Other (historic) RTL scripts exist, but this list contains the only ones in current use. + case 'Arab': + case 'Hebr': + case 'Mong': + case 'Tfng': + case 'Thaa': + return 'rtl'; + default: + return 'ltr'; + } + } } diff --git a/src/TranslationServiceProvider.php b/src/TranslationServiceProvider.php index e063974..155bebb 100644 --- a/src/TranslationServiceProvider.php +++ b/src/TranslationServiceProvider.php @@ -154,6 +154,9 @@ private function bootBladeExtensions() foreach ($extension->getDirectives() as $name => $callable) { $this->app['blade.compiler']->directive($name, $callable); } + foreach ($extension->getConditionals() as $name => $callable) { + $this->app['blade.compiler']->if($name, $callable); + } } } From f6883b8d9f8b7b4a7aa502d958eb0705f8ceb60c Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Fri, 14 Dec 2018 11:47:01 -0500 Subject: [PATCH 11/14] Fallback on the Locale Name if the Native Name doesn't exist --- src/LocaleService.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/LocaleService.php b/src/LocaleService.php index 8fbbfa7..4126782 100644 --- a/src/LocaleService.php +++ b/src/LocaleService.php @@ -247,10 +247,10 @@ public function getLocaleNativeName(string $locale = null) } if (!isset($this->getLocalesAvailable()[$locale])) { - return ''; + return $this->getLocaleName($locale); } - return array_get($this->getLocalesAvailable()[$locale], 'native', ''); + return array_get($this->getLocalesAvailable()[$locale], 'native', $this->getLocaleName($locale)); } /** From aa1ba7e847350c2fed6c83f9a833db238d4436d0 Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Fri, 14 Dec 2018 11:50:00 -0500 Subject: [PATCH 12/14] Remove the usage of $this->getCurrentLocale() as parameter since we are already checking if the local exist --- src/LocaleService.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/LocaleService.php b/src/LocaleService.php index 4126782..32409da 100644 --- a/src/LocaleService.php +++ b/src/LocaleService.php @@ -151,7 +151,7 @@ public function getLocaleInformation(string $locale = null) */ public function getCurrentLocaleSystem() { - return $this->getLocaleSystem($this->getCurrentLocale()); + return $this->getLocaleSystem(); } /** @@ -202,7 +202,7 @@ public function setSystemLocale(string $locale = null) */ public function getCurrentLocaleName() { - return $this->getLocaleName($this->getCurrentLocale()); + return $this->getLocaleName(); } /** @@ -231,7 +231,7 @@ public function getLocaleName(string $locale = null) */ public function getCurrentLocaleNativeName() { - return $this->getLocaleNativeName($this->getCurrentLocale()); + return $this->getLocaleNativeName(); } /** @@ -260,7 +260,7 @@ public function getLocaleNativeName(string $locale = null) */ public function getCurrentLocaleScript() { - return $this->getLocaleScript($this->getCurrentLocale()); + return $this->getLocaleScript(); } /** @@ -290,7 +290,7 @@ public function getLocaleScript(string $locale = null) */ public function getCurrentLocaleDirection() { - return $this->getLocaleDirection($this->getCurrentLocale()); + return $this->getLocaleDirection(); } /** From 063e84797fa33702011a28d046b338dba945fe20 Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Fri, 14 Dec 2018 12:08:45 -0500 Subject: [PATCH 13/14] Separate nested foreach in order to make the code readable and much clean. --- src/Blade/BladeExtension.php | 20 +++++++++++++++++++ src/Blade/LanguageBladeExtension.php | 2 +- src/TranslationServiceProvider.php | 29 ++++++++++++++++++++++------ 3 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 src/Blade/BladeExtension.php diff --git a/src/Blade/BladeExtension.php b/src/Blade/BladeExtension.php new file mode 100644 index 0000000..ffc3091 --- /dev/null +++ b/src/Blade/BladeExtension.php @@ -0,0 +1,20 @@ +app->tagged('blade.extension') as $extension) { - foreach ($extension->getDirectives() as $name => $callable) { - $this->app['blade.compiler']->directive($name, $callable); - } - foreach ($extension->getConditionals() as $name => $callable) { - $this->app['blade.compiler']->if($name, $callable); - } + $this->addBladeDirectives($extension); + $this->addBladeConditionals($extension); } } @@ -165,4 +162,24 @@ private function registerBladeExtensions() $this->app->singleton(LanguageBladeExtension::class); $this->app->tag(LanguageBladeExtension::class, ['blade.extension']); } + + /** + * @param \Exolnet\Translation\Blade\BladeExtension $extension + */ + private function addBladeDirectives(BladeExtension $extension) + { + foreach ($extension->getDirectives() as $name => $callable) { + $this->app['blade.compiler']->directive($name, $callable); + } + } + + /** + * @param \Exolnet\Translation\Blade\BladeExtension $extension + */ + private function addBladeConditionals(BladeExtension $extension) + { + foreach ($extension->getConditionals() as $name => $callable) { + $this->app['blade.compiler']->if($name, $callable); + } + } } From fd9672ff17ab86373706945bf695976a4dc96141 Mon Sep 17 00:00:00 2001 From: Simon Gaudreau Date: Mon, 17 Dec 2018 14:50:58 -0500 Subject: [PATCH 14/14] Use single quote notation like it should --- src/Blade/LanguageBladeExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Blade/LanguageBladeExtension.php b/src/Blade/LanguageBladeExtension.php index fbe0a5b..c7688ec 100644 --- a/src/Blade/LanguageBladeExtension.php +++ b/src/Blade/LanguageBladeExtension.php @@ -75,6 +75,6 @@ public function isRtl() public function isLtr() { - return LaravelTranslation::getCurrentLocaleDirection() === "rtl"; + return LaravelTranslation::getCurrentLocaleDirection() === 'rtl'; } }