Package Data | |
---|---|
Maintainer Username: | kevindierkx |
Maintainer Contact: | kevin@distortedfusion.com (Kevin Dierkx) |
Package Create Date: | 2015-08-28 |
Package Last Update: | 2024-10-05 |
Home Page: | https://distortedfusion.com/docs/kevindierkx/laravel-domain-localization/getting-started |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-15 03:04:24 |
Package Statistics | |
---|---|
Total Downloads: | 5,676 |
Monthly Downloads: | 30 |
Daily Downloads: | 2 |
Total Stars: | 28 |
Total Watchers: | 7 |
Total Forks: | 7 |
Total Open Issues: | 2 |
A tool for easy i18n domain based localization in Laravel applications.
This package is build for Laravel framework based applications.
For Laravel 4.2+ please refer to version 1.0.
Require this package with composer:
composer require kevindierkx/laravel-domain-localization
Open config/app.php
and register the required service provider.
'providers' => [
...
Kevindierkx\LaravelDomainLocalization\Provider\LaravelServiceProvider::class,
]
If you'd like to make configuration changes, you can publish it with the following Artisan command:
php artisan vendor:publish --tag=config
The Laravel Domain Localization uses the URL given for the request. In order to achieve this purpose it uses a middleware, to utilize the middleware add the following to you middleware array in app\Http\Kernel.php
:
protected $middleware = [
...
\Kevindierkx\LaravelDomainLocalization\Middleware\SetupLocaleMiddleware::class,
];
Once this middleware is enabled, the user can access all the locales defined in the supported_locales
array ('en' by default, look at the config section to change the supported locales).
For example, when you add the dutch locale nl
the user could access two different locales, using the following addresses:
http://example.com
http://example.nl
If the locale is not defined in supported_locales
array, the system will use the applications default locale.
Incase you only want to use domain localization on specific routes you could use the middleware groups or route middlewares instead.
The facade is used for easy access to the domain localization helper. If you would like to use the facade you need to open config/app.php
and register the facade in the aliases array.
'aliases' => [
...
'Localization' => Kevindierkx\LaravelDomainLocalization\Facade\DomainLocalization::class,
]
The package provides some useful helper functions. For a full list of methods and method descriptions please refer to the DomainLocalization class.
Uses the current URL to create a localized URL using the tld value from the config.
{{ Localization::getLocalizedUrl('en') }}
Returns all the supported currently configured locales.
{{ Localization::getSupportedLocales() }}
Returns a supported currently configured locale by 'name'.
{{ Localization::getSupportedLocale('en') }}
Determines a supported currently configured locale exists by 'name'.
{{ Localization::hasSupportedLocale('en') }}
Returns a supported currently configured locale 'name' by top level domain.
{{ Localization::getSupportedLocaleByTld('.com') }}
Returns a supported currently configured locale by top level domain.
{{ Localization::getSupportedLocaleNameByTld('.com') }}
Returns the attribute value of the current locale.
{{ Localization::getTldForCurrentLocale() }}
{{ Localization::getNameForCurrentLocale() }}
{{ Localization::getDirectionForCurrentLocale() }}
{{ Localization::getScriptForCurrentLocale() }}
{{ Localization::getNativeForCurrentLocale() }}
Returns the attribute value of a supported locale.
{{ Localization::getTldForLocale('en') }}
{{ Localization::getNameForLocale('en') }}
{{ Localization::getDirectionForLocale('en') }}
{{ Localization::getScriptForLocale('en') }}
{{ Localization::getNativeForLocale('en') }}
Returns the current top level domain.
{{ Localization::getTld() }}
Returns the current locale.
{{ Localization::getCurrentLocale() }}
Sets the current locale.
{{ Localization::setCurrentLocale('en') }}
Returns the applications default locale.
{{ Localization::getDefaultLocale() }}
Using the helper methods we can create a simple but effective language switcher. The example below uses a Bootstrap dropdown.
...
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
{{ Localization::getNameForCurrentLocale() }}
</a>
<ul class="dropdown-menu">
@foreach(Localization::getSupportedLocales() as $locale => $properties)
<li>
<a rel="alternate" hreflang="{{ $locale }}" href="{{ Localization::getLocalizedUrl($locale) }}">
{{ Localization::getNameForLocale($locale) }} - {{ Localization::getNativeForLocale($locale) }}
</a>
</li>
@endforeach
</ul>
</li>
</ul>
...
The MIT License (MIT). Please see License File for more information.