Skip to content

Commit

Permalink
Add a helper function, blade directive and blade condition to retreiv…
Browse files Browse the repository at this point in the history
…e the current locale direction
  • Loading branch information
Gandhi11 committed Dec 14, 2018
1 parent 3fe74c6 commit b6750af
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Blade/LanguageBladeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Exolnet\Translation\Blade;

use Exolnet\Translation\Facades\LaravelTranslation;

class LanguageBladeExtension
{
/**
Expand All @@ -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'],
];
}

Expand All @@ -39,4 +51,30 @@ public function getCurrentLocaleNativeName()
{
return '<?php echo LaravelTranslation::getCurrentLocaleNativeName(); ?>';
}

/**
* @return string
*/
public function getCurrentLocaleScript()
{
return '<?php echo LaravelTranslation::getCurrentLocaleScript(); ?>';
}

/**
* @return string
*/
public function getCurrentLocaleDirection()
{
return '<?php echo LaravelTranslation::getCurrentLocaleDirection(); ?>';
}

public function isRtl()
{
return !$this->isLtr();
}

public function isLtr()
{
return LaravelTranslation::getCurrentLocaleDirection() === "rtl";
}
}
66 changes: 66 additions & 0 deletions src/LocaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
}
3 changes: 3 additions & 0 deletions src/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

Expand Down

0 comments on commit b6750af

Please sign in to comment.