PHP code example of arnaldotomo / laravel-lusophone
1. Go to this page and download the library: Download arnaldotomo/laravel-lusophone 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/ */
arnaldotomo / laravel-lusophone example snippets
// Utilizador de Moçambique visita a aplicação
// Package detecta automaticamente MZ e adapta:
__('validation.tribute' => 'phone']);
// → "O campo celular é obrigatório" (usa 'celular', não 'telemóvel')
// Utilizador de Portugal visita a mesma aplicação
// Package detecta automaticamente PT e adapta:
__('validation.O campo telemóvel é obrigatório"
// tests/Feature/LusophoneTest.php
use ArnaldoTomo\LaravelLusophone\Facades\Lusophone;
class LusophoneTest extends TestCase
{
/** @test */
public function it_validates_mozambican_documents()
{
// Forçar região para teste
Lusophone::forceRegion('MZ');
$response = $this->post('/api/users', [
'name' => 'João Silva',
'email' => '[email protected]',
'nuit' => '123456789',
'phone' => '821234567',
]);
$response->assertStatus(201);
$this->assertDatabaseHas('users', [
'email' => '[email protected]',
]);
}
/** @test */
public function it_formats_currency_by_region()
{
Lusophone::forceRegion('PT');
$this->assertEquals('100,00 €', Str::lusophoneCurrency(100));
Lusophone::forceRegion('MZ');
$this->assertEquals('100,00 MT', Str::lusophoneCurrency(100));
Lusophone::forceRegion('BR');
$this->assertEquals('R$ 100,00', Str::lusophoneCurrency(100));
}
/** @test */
public function it_adapts_validation_messages()
{
Lusophone::forceRegion('PT');
$validator = Validator::make([], ['email' => '
// resources/lang/pt/custom.php
return [
'welcome' => [
'business' => 'Bem-vindo ao sistema, Estimado Cliente',
'casual' => 'Olá! Bem-vindo',
'government' => 'Respeitosos cumprimentos',
],
'checkout' => [
'total' => 'Total a pagar: :amount',
'success' => 'Compra realizada com sucesso',
],
];
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Str;
use ArnaldoTomo\LaravelLusophone\Facades\Lusophone;
public function boot()
{
// Macro para formatação de percentagem
Str::macro('lusophonePercentage', function ($value, $region = null) {
$region = $region ?: Lusophone::detectRegion();
$formatted = number_format($value, 1, ',', ' ');
return "{$formatted}%";
});
// Macro para formatação de data
\Carbon\Carbon::macro('lusophoneFormat', function ($region = null) {
$region = $region ?: Lusophone::detectRegion();
return match($region) {
'US', 'TL' => $this->format('m/d/Y'),
default => $this->format('d/m/Y')
};
});
}
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\Validator;
public function boot()
{
// Validador personalizado para Moçambique
Validator::extend('mz_phone_extended', function ($attribute, $value) {
// Lógica personalizada para telefones moçambicanos
return preg_match('/^(\+258|258)?[82][0-9]{7}$/', $value);
}, 'O :attribute deve ser um número de telemóvel moçambicano válido.');
}