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

Feature/add helpers #12

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
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
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion config/translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

'available_locales' => [
'en' => ['system' => ['en_CA.UTF-8']],
'en' => ['name' => 'English', 'native' => 'English', 'script' => 'Latn', 'system' => ['en_CA.UTF-8']],
],

];
9 changes: 9 additions & 0 deletions resources/views/language-switcher.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@forelse (URL::alternates(isset($alternateParameters) ? $alternateParameters : []) as $locale => $url)
<a href="{{ $url }}" hreflang="{{ $locale }}">{{$locale}}</a>
@empty
@foreach (Route::getAlternateLocales() as $locale)
<a href="{{ route('home.'. $locale) }}" data-locale="{{ $locale }}" hreflang="{{ $locale }}">
{{$locale}}
</a>
@endforeach
@endforelse
20 changes: 20 additions & 0 deletions src/Blade/BladeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Exolnet\Translation\Blade;

interface BladeExtension
{
/**
* Return all the directives
*
* @return array
*/
public function getDirectives();

/**
* Return all the cnditionals
*
* @return array
*/
public function getConditionals();
}
80 changes: 80 additions & 0 deletions src/Blade/LanguageBladeExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Exolnet\Translation\Blade;

use Exolnet\Translation\Facades\LaravelTranslation;

class LanguageBladeExtension implements BladeExtension
{
/**
* @return array
*/
public function getDirectives()
{
return [
'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'],
];
}

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

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

/**
* @return string
*/
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';
}
}
126 changes: 125 additions & 1 deletion src/LocaleService.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function getLocaleInformation(string $locale = null)
*/
public function getCurrentLocaleSystem()
{
return $this->getLocaleSystem($this->getCurrentLocale());
return $this->getLocaleSystem();
}

/**
Expand Down Expand Up @@ -194,4 +194,128 @@ 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();
}

/**
* 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', '');
}

/**
* Return the current locale native name
* @return string
* @throws \Exolnet\Translation\TranslationException
*/
public function getCurrentLocaleNativeName()
{
return $this->getLocaleNativeName();
}

/**
* 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 $this->getLocaleName($locale);
}

return array_get($this->getLocalesAvailable()[$locale], 'native', $this->getLocaleName($locale));
}

/**
* Return the current locale native name
* @return string
* @throws \Exolnet\Translation\TranslationException
*/
public function getCurrentLocaleScript()
{
return $this->getLocaleScript();
}

/**
* 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();
}

/**
* 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';
}
}
}
55 changes: 55 additions & 0 deletions src/TranslationServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Exolnet\Translation;

use Exolnet\Translation\Blade\BladeExtension;
use Exolnet\Translation\Blade\LanguageBladeExtension;
use Exolnet\Translation\Listeners\LocaleUpdatedListener;
use Exolnet\Translation\Routing\Router;
use Exolnet\Translation\Routing\UrlGenerator;
Expand All @@ -25,6 +27,13 @@ public function boot()
$this->publishes([
$this->getConfigFile() => config_path('translation.php'),
], 'config');

$this->bootBladeExtensions();

$this->loadViewsFrom(
$this->getViewsPath(),
'laravel-translation'
);
}

/**
Expand All @@ -35,6 +44,7 @@ public function register()
$this->registerRouter();
$this->registerUrlGenerator();
$this->registerLocaleService();
$this->registerBladeExtensions();

$this->mergeConfigFrom($this->getConfigFile(), 'translation');
}
Expand Down Expand Up @@ -127,4 +137,49 @@ 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';
}

private function bootBladeExtensions()
{
foreach ($this->app->tagged('blade.extension') as $extension) {
$this->addBladeDirectives($extension);
$this->addBladeConditionals($extension);
}
}

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);
}
}
}