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 6008425..ddebf1b 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); + } } }