VOOZH about

URL: https://deepwiki.com/hypervel/translation/4-contracts-and-interfaces

⇱ Contracts and Interfaces | hypervel/translation | DeepWiki


Loading...
Menu

Contracts and Interfaces

Overview

The hypervel/translation package uses a contract-based architecture to define system boundaries and enable loose coupling. Three interfaces in src/Contracts/ establish the architectural boundaries:

ContractPurpose
TranslatorTranslation retrieval, pluralization, and locale management
LoaderTranslation data loading and path/namespace registration
HasLocalePreferenceEntity-level locale preference declaration

Child Pages:

Related Pages:

Sources: src/Contracts/Translator.php1-32 src/Contracts/Loader.php1-36 src/Contracts/HasLocalePreference.php1-14


Contract Layer Architecture

The contract layer extends Hyperf's base translation contracts while adding Hypervel-specific functionality. This layered inheritance pattern ensures compatibility with the Hyperf framework while providing additional capabilities.


Contract Layer Architecture

Sources: src/Contracts/Translator.php1-32 src/Contracts/Loader.php1-36 src/Contracts/HasLocalePreference.php1-14


Contract Overview

The package defines three contracts that serve distinct architectural purposes:

ContractFile PathExtendsPurpose
Translatorsrc/Contracts/Translator.phpHyperf\Contract\TranslatorInterfaceDefines translation retrieval, pluralization, and locale management methods
Loadersrc/Contracts/Loader.phpHyperf\Contract\TranslatorLoaderInterfaceDefines translation data loading and path/namespace registration
HasLocalePreferencesrc/Contracts/HasLocalePreference.phpNoneEnables domain entities to declare preferred locales

Translator Contract

The Translator contract src/Contracts/Translator.php10 extends Hyperf's TranslatorInterface and declares four core methods:

See Translator Contract for detailed method documentation and usage examples.

Sources: src/Contracts/Translator.php1-32

Loader Contract

The Loader contract src/Contracts/Loader.php9 extends Hyperf's TranslatorLoaderInterface and declares five methods for managing translation data sources:

See Loader Contract for detailed method documentation and path resolution behavior.

Sources: src/Contracts/Loader.php1-36

HasLocalePreference Interface

The HasLocalePreference interface src/Contracts/HasLocalePreference.php7 is a single-method contract that domain entities implement to provide locale preferences:

This contract enables user-specific or context-specific locale selection. See HasLocalePreference Interface for integration examples.

Sources: src/Contracts/HasLocalePreference.php1-14


Dependency Injection and Contract Resolution

The contract layer integrates with Hyperf's dependency injection container through factory mappings defined in ConfigProvider. The following diagram illustrates how abstract contracts resolve to concrete implementations at runtime:


Dependency Resolution Flow

The ConfigProvider::__invoke() method registers contract-to-factory mappings in the dependencies array. When application code requests Contracts\Translator, the container invokes TranslatorFactory::__invoke(), which requests Contracts\Loader, triggering LoaderFactory::__invoke(). This factory pattern enables lazy instantiation and environment-specific implementations.

Sources: src/ConfigProvider.php18-25 src/TranslatorFactory.php1-40 src/LoaderFactory.php1-30


Contract-Implementation Mapping

The following table maps contracts to their concrete implementations:

ContractPrimary ImplementationAlternative ImplementationSource Files
TranslatorHypervel\Translation\TranslatorNonesrc/Translator.php
LoaderHypervel\Translation\FileLoaderHypervel\Translation\ArrayLoadersrc/FileLoader.php src/ArrayLoader.php
HasLocalePreferenceApplication-defined entitiesN/AApplication code (e.g., User model)

Loader Implementations

The Loader contract src/Contracts/Loader.php9 has two implementations:

ImplementationUse CaseKey Methods
FileLoader src/FileLoader.phpProduction - reads from filesystemload(), loadPath(), loadNamespaced(), loadJsonPaths()
ArrayLoader src/ArrayLoader.phpTesting - stores in memoryload(), addMessages(), addJsonMessages()

Loader Contract Implementations

The default configuration uses FileLoader via LoaderFactory::__invoke() src/LoaderFactory.php20-28 Applications can substitute ArrayLoader by overriding the container binding. See 5.1 File-Based Loading and 5.2 Memory-Based Loading for implementation details.

Sources: src/Contracts/Loader.php9-35 src/FileLoader.php1-200 src/ArrayLoader.php1-100


Design Principles

The contract layer follows SOLID principles:

PrincipleApplication
Single ResponsibilityTranslator handles translation retrieval; Loader handles data loading
Open/ClosedNew implementations can be created without modifying contracts
Liskov SubstitutionFileLoader and ArrayLoader are interchangeable via Loader contract
Interface SegregationHasLocalePreference declares only preferredLocale() method
Dependency InversionTranslator depends on Loader abstraction, not concrete classes

The Translator class src/Translator.php40-42 type-hints Loader contract, not FileLoader or ArrayLoader. This enables swapping implementations via DI configuration without code changes.

Sources: src/Translator.php40-42 src/Contracts/Loader.php9 src/Contracts/HasLocalePreference.php7


Extension Points

The contract layer provides three primary extension points for applications:

Custom Loader Implementation

Applications can implement custom Loader classes to load translations from databases, APIs, or other sources:


Custom Loader Examples

Custom loaders must implement load(), addNamespace(), addJsonPath(), addPath(), and namespaces() methods src/Contracts/Loader.php14-34

Sources: src/Contracts/Loader.php9-35

Entity Locale Preferences

Domain models implement HasLocalePreference src/Contracts/HasLocalePreference.php7 to provide user-specific or context-specific locales. The Translator::get() method checks for this interface when determining the active locale. See 4.3 HasLocalePreference Interface for implementation patterns.

Sources: src/Contracts/HasLocalePreference.php7-13

Translator Customization

Applications can extend the Translator class src/Translator.php21 or create alternative implementations of Contracts\Translator src/Contracts/Translator.php10 Override TranslatorFactory::__invoke() src/TranslatorFactory.php20 to return custom instances. See 6 Configuration and Dependency Injection for factory customization.

Sources: src/Contracts/Translator.php10 src/TranslatorFactory.php20-38


Related Documentation