PHP code example of upon / mlang

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

    

upon / mlang example snippets


// config/mlang.php
'models' => [
    \App\Models\Category::class,
    \App\Models\Product::class,
    // Add all translatable models
]


// ...
use Upon\Mlang\Models\MlangModel;

class Category extends MlangModel
{
    // ...
}


// ...
use Illuminate\Database\Eloquent\Model;
use Upon\Mlang\Contracts\MlangContractInterface;
use Upon\Mlang\Models\Traits\MlangTrait;

class Category extends Model implements MlangContractInterface
{
    use MlangTrait;
    
    // ...
}

use Upon\Mlang\Facades\Mlang;
use App\Models\Category;

// Specify which model to work with
Mlang::forModel(Category::class)->migrate();

// Chain multiple operations together
Mlang::forModel(Category::class)
    ->migrate()
    ->generate();

// Get model information
$modelName = Mlang::forModel(Category::class)->getModelName(); // Returns "Category"
$tableName = Mlang::forModel(Category::class)->getTableName(); // Returns the table name

// You can also use a model instance
$category = new Category();
Mlang::forModel($category)->generate();

// config/mlang.php

// Control automatic generation of translations
'auto_generate' => false,

// Enable automatic migration after Laravel migrations
'auto_migrate' => false,

// Control observer behavior during console operations
'observe_during_console' => false,

// Auto-generate translations after seeding
'auto_generate_after_seed' => false,

// Specify which models to process after migrations/seeding
'auto_generate_models' => 'all',

// Control rollback behavior
'auto_rollback' => true,

// Toggle debug output
'debug_output' => true,

use Upon\Mlang\Facades\Mlang;
use App\Models\Category;

// Generate translations for a specific model
Mlang::forModel(Category::class)->generate();

// Generate translations for a specific model and language
Mlang::generate('Category', 'fr');

// Run migrations for a specific model
Mlang::forModel(Category::class)->migrate();

// Roll back migrations for a specific model
Mlang::forModel(Category::class)->rollback();

protected $middlewareGroups = [
    'web' => [
        // ...
        \Upon\Mlang\Middleware\DetectUserLanguageMiddleware::class,
    ],
    // ...
];

app()->setLocale('fr');

// Returns the record in the current language
$category = Category::trFind(1);

// Gets a collection in the current language
$categories = Category::trWhere(['name' => 'test'])->get();

$categories = Category::trWhere('status', 'active')
                     ->orderBy('created_at', 'desc')
                     ->paginate(10);

// routes/web.php
Route::get('/categories/{category}', function (Category $category) {
    // $category will be in the current application language
    return view('categories.show', compact('category'));
});

class Category extends Model implements MlangContractInterface
{
    use MlangTrait;
    
    // Your model code...
}
bash
php artisan vendor:publish --tag="mlang"
bash
php artisan mlang:migrate
bash
php artisan mlang:generate
bash
php artisan mlang:generate {model}
# Example: php artisan mlang:generate Category
# Or for nested models: php artisan mlang:generate Shop\\Product
bash
php artisan mlang:generate {model|all} {locale}
# Example: php artisan mlang:generate all fr
bash
php artisan mlang:remove {table} {locale}
# Example: php artisan mlang:remove categories fr