Translations
Last updated:
On this page
Without anything installed, every notification is sent in its notifiable's language (see Languages) and stored in that language.
With javaabu/translatable v3 installed and at least one language active, two things change:
- Only active languages are used. A notifiable whose locale isn't one of translatable's active languages is notified in translatable's default language.
- Database notifications are stored in every active language. The title, body and action text are rendered once per language (with
message()running in each), and kept undertranslationsin the data. The bell, the page andtitle(),body()andactionText()show the reader's current language, so an English-speaking admin and a Dhivehi-speaking user see the same notification in their own language.
$notification->title(); // in the current locale
$notification->title('dv'); // in Dhivehi
$notification->data['translations']; // ['en' => ['title' => …, 'body' => …, 'action_text' => …], 'dv' => [...]]
Mail, SMS and push notifications are sent in the notifiable's language only.
Write message() with __() so every language has its text:
public function message(object $notifiable): NotificationMessage
{
return NotificationMessage::make(__('Your order has shipped'))
->line(__('Order :number is on its way.', ['number' => $this->order->number]));
}
Data that doesn't change with the language (URLs, icons, data()) is stored once.
Turning it off
// config/notifications.php
'translations' => false,
The package then works as if translatable weren't installed. It's detected with Javaabu\Notifications\Support\Translations::enabled(), which is false when the package isn't installed, translatable.enabled is off, there are no active languages, or notifications.translations is false.
Testing
use Javaabu\Translatable\Testing\InteractsWithLanguages;
class OrderNotificationsTest extends TestCase
{
use InteractsWithLanguages;
#[Test]
public function it_stores_the_dhivehi_title(): void
{
$this->setUpLanguages(['en', 'dv']);
$user->notifyNow(new OrderShippedNotification($order), ['database']);
$this->assertEquals('ތިބާގެ އޯޑަރު ފޮނުވައިފި', $user->notifications()->first()->title('dv'));
}
}