PHP code example of wecansync / laravel-translations

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

    

wecansync / laravel-translations example snippets


    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    use WeCanSync\LaravelTranslations\Traits\HasTranslations;
    
    class ModelName extends Model
    {
       use HasTranslations;
   
       protected $translation_model = [
            'model' => TranslationsModelName::class,
        // Optional: Uncomment to customize foreign and owner keys.
        //  'foreign_key' => 'language_id', 
        //  'owner_key' => 'model_name_id',
        
        // Optional: Uncomment to customize key name for translations data in the request array.
        //  'translations_data_key' => 'translations_array'
        
       // Optional: Uncomment to skip storing/updating translations for specific routes.
       //   'skip_routes' => ['route.update', 'route.store', ...]
       // Use this to update the main model data without affecting translations.
       // Note: Deleting the model will still remove its associated translations.
       // Use wildcards like 'route.*' to match multiple routes.
       
        // Default values:
        // foreign_key: "language_id"
        // owner_key: "model_id"
        // translations_data_key: "translations"
       
        //  if you would like to replace it for all models
        //  you can change it in config/laravel-translations
        ]; 
    }
 

$translation = $model->getTranslations(1, 'name'); 
// Retrieves the 'name' translation for language ID 1 (e.g., English).

$translationByCode = $model->getTranslations('en', 'name', 'code'); 
// Retrieves the 'name' translation using the key 'code' instead of an ID.

$translation = $category->getTranslations(3, 'name'); // Assuming 3 is the language ID for a non-existent language

if ($translation) {
    echo "Translation: " . $translation;
} else {
    echo "Translation not available.";
}

$category->clearTranslations(1); 
// Delete the translations for language ID 1 (e.g., English).

$category->clearTranslations('en', 'code'); 
// Delete the translation using the key 'code' instead of an ID.

protected $translation_model = [
          'skip_routes' => ['categories.update', 'categories.store', ...]
     ]; 
]
 

$translations_data = [
    1 => [
        'name' => 'English Name',
    ],
    2 => [
        'name' => 'Arabic Name',
    ]
];

Category::query()->create($request->validated())->withTranslations($translations_data);
// Store the translations for language ID 1 (English) and language ID 2 (Arabic).

$category = Category::query()->find($category_id)->update($request->validated());

$translations_data = [
    1 => [
        'name' => 'Updated English Name',
    ],
    2 => [
        'name' => 'Updated Arabic Name',
    ]
];

$category->withTranslations($translations_data);
// Update the translations for language ID 1 (English) and language ID 2 (Arabic).
bash
php artisan vendor:publish --provider="WeCanSync\LaravelTranslations\PackageServiceProvider"