VOOZH about

URL: https://deepwiki.com/hypervel/translation/4.3-haslocalepreference-interface

⇱ HasLocalePreference Interface | hypervel/translation | DeepWiki


Loading...
Menu

HasLocalePreference Interface

Purpose and Scope

The HasLocalePreference interface defines a contract for entities that maintain their own locale preferences. This interface enables per-entity localization, allowing different users, customers, or models to receive translated content in their preferred language rather than the application's global locale.

This document covers the interface definition, integration patterns with the Translator class, and implementation strategies for entities that require locale preferences. For information about the core Translator contract, see Translator Contract. For details on global locale management within the translation system, see Core Translation System.


Interface Definition

The HasLocalePreference interface is defined in src/Contracts/HasLocalePreference.php7-13 and consists of a single method:


Method Specification

MethodReturn TypeDescription
preferredLocale()?stringReturns the entity's preferred locale code (e.g., 'en', 'es', 'fr-CA') or null if no preference is set

The nullable return type allows entities to indicate they have no explicit preference, deferring to the application's default locale resolution behavior.

Sources: src/Contracts/HasLocalePreference.php1-14


Interface Contract Structure


Diagram: Interface Implementation Pattern

This diagram illustrates how different entity types implement the HasLocalePreference interface. Each implementing class provides its own logic for determining the preferred locale based on its internal state or database fields.

Sources: src/Contracts/HasLocalePreference.php7-13


Integration with Translation System

The HasLocalePreference interface integrates with the translation system by providing entity-aware locale resolution. When the Translator processes translations for operations involving entities, it can check if the entity implements this interface and use the returned locale preference.

Locale Resolution Hierarchy


Diagram: Locale Resolution Flow with HasLocalePreference

This diagram shows the decision tree for determining which locale to use when translating content. The entity's preferredLocale() takes precedence over context and fallback locales when available.

Sources: src/Contracts/HasLocalePreference.php7-13


Implementation Patterns

Basic Database-Backed Implementation

A typical implementation stores the locale preference in a database column:


Computed Preference Implementation

More complex implementations may compute the preference based on multiple factors:


Hierarchical Preference Implementation

Entities may inherit preferences from parent entities:


Sources: src/Contracts/HasLocalePreference.php7-13


Implementation Strategy Table

StrategyUse CaseReturn BehaviorExample
Direct FieldSingle locale columnReturns field value or nullUser model with locale column
ComputedMultiple factors determine localeReturns calculated locale or nullCustomer with country-based fallback
HierarchicalInherit from parent entityReturns own preference or delegates to parentTeam member inheriting from team
DynamicExternal service determines localeReturns value from external sourceUser preferences from separate service
Null ReturnNo preference setAlways returns nullEntities that defer to global locale

Use Cases

User-Specific Localization

The primary use case is providing personalized translations based on user preferences:


Multi-Tenant Applications

Different tenants or organizations may require different default locales:


Email and Notification Localization

When sending emails or notifications, the system can use the recipient's preferred locale:


Sources: src/Contracts/HasLocalePreference.php7-13


Code Entity Mapping


Diagram: Code Entity Relationships

This diagram maps the interface definition to its implementation classes and integration points within the translation system.

Sources: src/Contracts/HasLocalePreference.php1-14


Comparison with Context-Based Locale

The HasLocalePreference interface provides entity-level locale preferences, which differs from the request-level locale stored in Hyperf's context:

AspectHasLocalePreferenceContext Locale
ScopePer-entity (user, customer, etc.)Per-request/coroutine
StorageEntity property/databaseHyperf Context (Context::get('locale'))
LifetimePersistent across requestsRequest-scoped
Use CaseUser preferences, personalizationRequest language negotiation
PriorityTakes precedence when entity providedFallback when no entity preference
Accessed Via$entity->preferredLocale()$translator->getLocale()

Interaction Pattern


Diagram: Entity Preference vs Context Locale

This sequence diagram shows how entity-level locale preferences take precedence over context-stored locales when explicitly provided to translation methods.

Sources: src/Contracts/HasLocalePreference.php7-13


Namespace and Location

The HasLocalePreference interface is located in the contracts namespace:

Hypervel\Translation\Contracts\HasLocalePreference

File path: src/Contracts/HasLocalePreference.php5

This placement alongside other contracts (Translator Contract, Loader Contract) establishes it as a core architectural boundary in the translation system.


Best Practices

Return Type Consistency

Always return valid locale codes or null. Invalid locale codes may cause translation failures:


Performance Considerations

Cache computed locale preferences to avoid repeated calculations:


Null Semantics

Return null to explicitly indicate "no preference" rather than returning a default locale. This allows the translation system to apply its own fallback logic:


Sources: src/Contracts/HasLocalePreference.php7-13


Summary

The HasLocalePreference interface provides a lightweight, flexible contract for entity-level locale preferences within the translation system. Key characteristics:

  • Single Method: preferredLocale() returns ?string
  • Nullable Return: null indicates no preference, allowing fallback to context/default locale
  • Entity-Scoped: Provides personalization at the model level rather than request level
  • Integration Point: Used by Translator to resolve locale before translation
  • Implementation Flexibility: Supports direct field mapping, computed preferences, and hierarchical delegation

The interface enables sophisticated multi-locale applications where different entities require different translation preferences without coupling entity logic to the translation system internals.

Sources: src/Contracts/HasLocalePreference.php1-14