1. Go to this page and download the library: Download nxmad/eloquent-i18n 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/ */
nxmad / eloquent-i18n example snippets
use Nxmad\EloquentI18n\Traits\HasTranslations;
class Something extends Model
{
use HasTranslations;
// ...
}
$translations = [
'title' => [
'en' => 'Hello, world!',
'de' => 'Hallo Welt!',
],
'content' => [
'en' => 'This is my page content.',
'de' => 'Dies ist mein Seiteninhalt.',
]
];
$page = new Page();
// First way
$page->translations = $translations;
// Alt. way
$page->addTranslations($translations);
$page->addTranslations('title', 'Hello, world!'); // would add translation for current locale
$page->addTranslations('title', 'Hello, world!', 'en'); // would add translation for specified locale
$page->save();
// Remove all existing translations
$page->removeTranslations();
// Remove by the key
$page->removeTranslations('title');
// Remove by locale
$page->removeTranslations(null, 'de');
// Or both:
$page->removeTranslations('title', 'de');
app()->setLocale('de');
echo $page->title; // Hallo Welt!
app()->setLocale('en');
echo $page->title; // Hello, world!
// alt. way
$page->getTranslation('title', 'default', 'en'); // Hello, world!
$page->getTranslation('non-existing-key', 'default', 'en'); // default
// get all translations for key
$page->getAllTranslations('title');
// serialize
echo $page->toJson(); // { id, title, content, etc. }