PHP code example of nalingia / laravel-i18n

1. Go to this page and download the library: Download nalingia/laravel-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/ */

    

nalingia / laravel-i18n example snippets


use \Nalingia\I18n\Traits\HasI18n;

class Article extends Model {
    use HasI18n;
    
    public $catalogueAttributes = [
      'title',
      'abstract',
      'content',
    ];
}
 

$article->title

public function getCatalogueItem(string $attribute, string $locale) : string

$article->title
// or
$article->abstract

$article->translate('title', 'it')

$article->getCatalogueItem('title', 'it')

$article->getCatalogueItems()

$article->translations

$article
  ->setCatalogueItem('title', 'en', 'English title')
  ->setCatalogueItem('abstract', 'en', 'English abstract')
  ->setCatalogueItem('title', 'it', 'Italian title')
  ->setCatalogueItem('abstract', 'it', 'Italian abstract')
  ->setCatalogueItem('abstract', 'de', 'German abstract');
  
$article->getCatalogueLocales(); // ['it', 'en', 'de']

$article->title = 'Super cool title';
// or
$article->abstract = 'Exciting abstract...';

$article
  ->setCatalogueItem('title', 'en', 'English title')
  ->setCatalogueItem('abstract', 'en', 'English abstract')
  ->setCatalogueItem('title', 'it', 'Italian title')
  ->setCatalogueItem('abstract', 'it', 'Italian abstract');

public function forgetCatalogueItem(string $key, string $locale);

public function forgetCatalogueItemsForLocale(string $locale);

$article = Article::create([
    'title' => [
        'en' => 'English title',
        'it' => 'Italian title',
    ],
    'abstract' => [
        'en' => 'English abstract',
        'it' => 'Italian abstract',
    ],
]);

// app()->getLocale() == 'de'

$article = Article::create([
    'title' => 'German title'
    'abstract' => 'German abstract',
]);

$article = Article::create([
    'title' => [
        'de' => 'German title',
    ],
    'abstract' => [
        'de' => 'German abstract',
    ],
]);

composer test

vendor/bin/phpunit
bash
 php artisan i18n:table
bash
 php artisan migrate
bash
 php artisan vendor:publish --provider="Nalingia\I18n\I18nServiceProvider" --tag="config"