PHP code example of mikemenard / laravel-localization
1. Go to this page and download the library: Download mikemenard/laravel-localization library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mikemenard / laravel-localization example snippets
namespace App\Localization;
use Mikemenard\Localization\Locale;
class CustomLocale extends Locale
{
public function displayName(): string
{
return strtoupper($this->label);
}
public function flagEmoji(): string
{
return $this->flag ?? 'π³οΈ';
}
}
namespace App\Localization;
use Mikemenard\Localization\Locale as BaseLocale;
class CustomLocale extends BaseLocale
{
public function uppercaseLabel(): string
{
return strtoupper($this->label);
}
public function isRtl(): bool
{
return $this->direction === 'rtl';
}
public function flagEmoji(): string
{
return $this->flag ?? 'π³οΈ';
}
public function formattedDate(\DateTime $date): string
{
return $date->format($this->date_format);
}
}
session('localization:locale'); // 'fr'
session(['localization:locale' => 'en']); // Manually set
// Config: ['en', 'fr-ca']
Localization::isSupported('fr-ca'); // true (exact)
Localization::isSupported('fr'); // true (matches 'fr-ca' via prefix)
Localization::isSupported('en-us'); // true (matches 'en' via language code)
use Mikemenard\Localization\Facades\Localization;
test('homepage shows French content', function () {
Localization::setLocale('fr');
$response = $this->get(route('home', ['locale' => 'fr']));
$response->assertSee('Bienvenue');
});
try {
Localization::setLocale('de'); // Not in config
} catch (\Mikemenard\Localization\Exceptions\UnsupportedLocaleException $e) {
// "Locale 'de' is not supported. Supported locales: en, fr."
}
// config/localization.php
'route_parameter' => 'locale/invalid', // Invalid!
// Throws: "The route parameter 'locale/invalid' is invalid.
// It must be a single word without slashes, optionals (?), or special characters."
// config/localization.php
'locale_class' => \App\Models\User::class, // Doesn't extend Locale!
// Throws: "The locale class 'App\Models\User' must extend \Mikemenard\Localization\Locale."