# Localization Usage Guide

This document explains how to use the localization system implemented in the Baraka application.

## Overview

The localization system uses JSON columns in the database to store multiple language versions of text fields. This approach is simple, efficient, and doesn't require complex relationships.

## Supported Tables

- **Products**: `localized_name`, `localized_description`
- **Companies**: `localized_company_name`, `localized_business_field`, `localized_company_owner`, `localized_company_address`, `localized_notes`
- **Categories**: `localized_category_name`

## Usage Examples

### 1. Using in Models

```php
// Get localized product name
$product = Product::find(1);
$arabicName = $product->getLocalizedName('ar');
$englishName = $product->getLocalizedName('en');
$currentLocaleName = $product->getLocalizedName(); // Uses current app locale

// Set localized product name
$product->setLocalizedName('اسم المنتج', 'ar');
$product->setLocalizedName('Product Name', 'en');
$product->save();

// Get all localized versions
$allNames = $product->getAllLocalized('name');
// Returns: ['ar' => 'اسم المنتج', 'en' => 'Product Name']

// Check if localized data exists
if ($product->hasLocalized('name', 'ar')) {
    // Arabic name exists
}

// Get supported locales for a field
$locales = $product->getSupportedLocales('name');
// Returns: ['ar', 'en']
```

### 2. Using in Blade Templates

#### Display Localized Content

```blade
{{-- Simple display --}}
<x-localized-display :value="$product->localized_name" :fallback="$product->product_name" />

{{-- With specific locale --}}
<x-localized-display :value="$product->localized_name" :fallback="$product->product_name" locale="en" />

{{-- With custom styling --}}
<x-localized-display 
    :value="$product->localized_name" 
    :fallback="$product->product_name" 
    class="text-primary fw-bold"
    tag="h3"
/>
```

#### Localized Form Inputs

```blade
{{-- Product name input --}}
<x-localized-input 
    name="localized_name" 
    label="Product Name" 
    :value="$product->localized_name"
    :locales="['ar', 'en']"
    required
/>

{{-- Product description textarea --}}
<x-localized-input 
    name="localized_description" 
    label="Product Description" 
    :value="$product->localized_description"
    :locales="['ar', 'en']"
    type="textarea"
    rows="4"
/>

{{-- Company name input --}}
<x-localized-input 
    name="localized_company_name" 
    label="Company Name" 
    :value="$company->localized_company_name"
    :locales="['ar', 'en']"
    required
/>
```

### 3. Using in Controllers

```php
public function store(Request $request)
{
    $product = new Product();
    $product->product_name = $request->product_name; // Fallback value
    $product->description = $request->description; // Fallback value
    
    // Set localized values
    if ($request->has('localized_name')) {
        foreach ($request->localized_name as $locale => $value) {
            if (!empty($value)) {
                $product->setLocalizedName($value, $locale);
            }
        }
    }
    
    if ($request->has('localized_description')) {
        foreach ($request->localized_description as $locale => $value) {
            if (!empty($value)) {
                $product->setLocalizedDescription($value, $locale);
            }
        }
    }
    
    $product->save();
    
    return redirect()->back()->with('success', 'Product created successfully');
}
```

### 4. Database Structure

The localized data is stored as JSON in the database:

```json
{
    "ar": "اسم المنتج",
    "en": "Product Name",
    "fr": "Nom du Produit"
}
```

### 5. Migration Commands

To run the migrations:

```bash
php artisan migrate
```

To rollback:

```bash
php artisan migrate:rollback
```

## Benefits of This Approach

1. **Simple Implementation**: No complex relationships or additional tables
2. **Performance**: Single query to get all translations
3. **Flexible**: Easy to add new languages without schema changes
4. **Backward Compatible**: Original fields remain as fallbacks
5. **Easy to Use**: Simple methods and Blade components

## Adding New Languages

To add support for a new language (e.g., French):

1. Update the `supportedLocales` in `config/laravellocalization.php`
2. Add the new locale to your Blade components:
   ```blade
   <x-localized-input 
       name="localized_name" 
       :locales="['ar', 'en', 'fr']"
   />
   ```

## Best Practices

1. Always provide fallback values
2. Use the original field as the primary language (usually Arabic in your case)
3. Validate localized inputs in your controllers
4. Consider using the `HasLocalization` trait for other models that need localization
5. Use the Blade components for consistent UI across the application

## Troubleshooting

- If localized data doesn't display, check that the JSON is properly formatted
- Ensure the locale key exists in the JSON data
- Verify that the `HasLocalization` trait is used in your models
- Check that the localized columns are in the model's `$fillable` array
