Skip to content

API Reference ​

HasTranslations Trait Methods ​

Translation Methods ​

MethodParametersReturnsDescription
getTranslatestring $field, ?string $locale?stringGet translation for field
setTranslatestring $field, string $locale, ?string $valuevoidSet translation (null to delete)
hasTranslationstring $field, ?string $localeboolCheck if translation exists
hasAnyTranslationsboolCheck for any translations
getAllTranslationsarrayGet all translations as nested array
getFieldTranslationsstring $fieldarrayGet all locales for a field
getMissingLocalesstring $fieldarrayGet locales without translation

Configuration Methods ​

MethodReturnsDescription
getTranslatableFieldsarrayGet translatable field names
getRequiredTranslatableFieldsarrayGet required field names
isFieldTranslatable(string $field)boolCheck if field is translatable
isTranslationExcludedboolCheck if model is excluded

Locale Methods ​

MethodParametersReturnsDescription
setPreferredLanguagestring $localestaticSet preferred locale for instance
getPreferredLanguage?stringGet preferred locale
hasAllRequiredFieldsForLocale?string $localeboolCheck required fields

Cache Methods ​

MethodDescription
clearTranslationCacheClear all cached translations for model

Query Scopes ​

ScopeParametersDescription
searchTranslatablestring $field, string $search, ?string $localeSearch in field
searchMultipleTranslatablearray $fields, string $search, ?string $localeSearch multiple fields
searchTranslatableInLanguagestring $field, string $search, string $localeSearch in specific locale
hasAllRequiredFieldsForLocaleScope?string $localeFilter by complete translations
searchRelationTranslatablestring $relation, string $field, string $search, ?string $localeSearch in relation

Translation Model ​

php
use PolyglotModel\Models\Translation;

// Query translations directly
$translations = Translation::forLocale('tr')->get();

// Access parent model
$translation->translatable; // Returns Post, Page, etc.

// Clean orphaned
Translation::cleanOrphaned();

Model Properties ​

PropertyTypeDescription
translatable_typestringParent model class
translatable_idintParent model ID
fieldstringField name
localestringLocale code
valuestringTranslated value

Model Scopes ​

ScopeDescription
forModel($model)Filter by parent model
forField($field)Filter by field name
forLocale($locale)Filter by locale

Events ​

EventPropertyDescription
TranslationCreatedTranslation $translationDispatched on create
TranslationUpdatedTranslation $translationDispatched on update
TranslationDeletedTranslation $translationDispatched on delete

Exceptions ​

ExceptionStatic Methods
TranslationExceptionfieldNotTranslatable(), modelNotPersisted(), translationNotFound()
InvalidLocaleExceptionunsupportedLocale(), emptyLocale()

Released under the MIT License.