# spatie/laravel-translatable architecture

> Architecture diagram of the GitHub repository spatie/laravel-translatable, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/spatie/laravel-translatable
- Repository: https://github.com/spatie/laravel-translatable

## Overview

##### Project Type and Purpose
This is a Laravel package that provides a trait for making Eloquent models translatable, storing translations as JSON in the database without requiring additional tables.

##### Main Components

a. Core Components:
- HasTranslations Trait
- Translatable Service Provider
- Events System
- Model Integration Layer
- Database Layer

b. Supporting Components:
- Exception Handling
- Facade Layer

##### Application Layer (Top)
- How a Laravel application integrates with the package
- Example Eloquent model using the HasTranslations trait

##### Package Core (Middle)
- HasTranslations Trait (central component)
- TranslatableServiceProvider (initialization and configuration)
- Translatable Facade (public interface)

##### Implementation Layer (Bottom)
- JSON Storage in Database
- Event System
- Exception Handling

##### Specific Elements

Use the following visual elements:
- Rounded rectangles for traits and interfaces

- Blue: Core components (HasTranslations, TranslatableServiceProvider)
- Green: Public interfaces (Facade)
- Yellow: Event system
- Red: Exception handling
- Gray: Database storage

##### Component Relationships

Key interactions:
**Laravel Application**
- ↓
**Eloquent Model with HasTranslations Trait**
- ↓
**TranslatableServiceProvider**
- ↓
**JSON Storage in Database**

With side connections to:
- Events System (TranslationHasBeenSetEvent)
- Exception Handling (AttributeIsNotTranslatable)

##### Important Details to Highlight

- JSON storage mechanism (show how translations are stored in a single column)
- Locale handling and fallback system
- Query builder integration (whereLocale, whereLocales methods)
- Event dispatching system

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph "Application Layer"
        App[Laravel Application]
        Model["Eloquent Model"]:::model
        click Model "https://github.com/spatie/laravel-translatable/blob/main/tests/TestSupport/TestModel.php"
        ModelFallback["Model with Fallback"]:::model
        click ModelFallback "https://github.com/spatie/laravel-translatable/blob/main/tests/TestSupport/TestModelWithFallbackLocale.php"
    end

    subgraph "Package Core"
        HasTrans["HasTranslations Trait"]:::core
        click HasTrans "https://github.com/spatie/laravel-translatable/blob/main/src/HasTranslations.php"
        
        ServiceProvider["Translatable Service Provider"]:::core
        click ServiceProvider "https://github.com/spatie/laravel-translatable/blob/main/src/TranslatableServiceProvider.php"
        
        TranslatableClass["Core Translatable Class"]:::core
        click TranslatableClass "https://github.com/spatie/laravel-translatable/blob/main/src/Translatable.php"
        
        Facade["Translatable Facade"]:::facade
        click Facade "https://github.com/spatie/laravel-translatable/blob/main/src/Facades/Translatable.php"
    end

    subgraph "Implementation Layer"
        Events["Events System"]:::event
        click Events "https://github.com/spatie/laravel-translatable/blob/main/src/Events/TranslationHasBeenSetEvent.php"
        
        Exceptions["Exception Handler"]:::error
        click Exceptions "https://github.com/spatie/laravel-translatable/blob/main/src/Exceptions/AttributeIsNotTranslatable.php"
        
        DB[(JSON Storage)]:::storage
    end

    %% Relationships
    App --> Model
    App --> ModelFallback
    Model --> HasTrans
    ModelFallback --> HasTrans
    HasTrans --> ServiceProvider
    ServiceProvider --> TranslatableClass
    TranslatableClass --> Facade
    HasTrans --> DB
    HasTrans -.-> Events
    HasTrans -.-> Exceptions

    %% Styles
    classDef core fill:#a8d1f0,stroke:#333
    classDef facade fill:#90EE90,stroke:#333
    classDef event fill:#FFE4B5,stroke:#333
    classDef error fill:#FFB6C1,stroke:#333
    classDef storage fill:#E6E6E6,stroke:#333
    classDef model fill:#F0F8FF,stroke:#333

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Facade Layer"]:::facade
        L3["Event System"]:::event
        L4["Error Handling"]:::error
        L5["Storage"]:::storage
        L6["Models"]:::model
    end
```
