PHP Class omgdef\multilingual\MultilingualBehavior

Inheritance: extends yii\base\Behavior
Show file Open project: omgdef/yii2-multilingual-behavior Class Usage Examples

Public Properties

Property Type Description
$abridge whether to abridge the language ID. Default to true.
$attributes array Multilingual attributes
$currentLanguage
$defaultLanguage the default language. Example: 'en'.
$dynamicLangClass whether to dynamically create translation model class. If true, the translation model class will be generated on runtime with the use of the eval() function so no additional php file is needed. See {@link createLangClass()} Default to true.
$forceDelete whether to force deletion of the associated translations when a base model is deleted. Not needed if using foreign key with 'on delete cascade'. Default to true.
$langClassName the name of translation model class.
$langClassSuffix if $langClassName is not set, it will be assumed that $langClassName is get_class($this->owner) . $this->langClassSuffix
$langForeignKey the name of the foreign key field of the translation table related to base model table.
$languageField the name of the lang field of the translation table. Default to 'language'.
$languages array Available languages It can be a simple array: array('fr', 'en') or an associative array: array('fr' => 'Français', 'en' => 'English') For associative arrays, only the keys will be used.
$localizedPrefix the prefix of the localized attributes in the lang table. Here to avoid collisions in queries. In the translation table, the columns corresponding to the localized attributes have to be name like this: 'l_[name of the attribute]' and the id column (primary key) like this : 'l_id' Default to ''.
$requireTranslations if this property is set to true required validators will be applied to all translation models. Default to false.
$tableName the name of the translation table

Public Methods

Method Description
__get ( $name )
__isset ( $name )
__set ( $name, $value )
afterDelete ( ) Handle 'afterDelete' event of the owner.
afterFind ( ) Handle 'afterFind' event of the owner.
afterInsert ( ) Handle 'afterInsert' event of the owner.
afterUpdate ( ) Handle 'afterUpdate' event of the owner.
attach ( $owner )
beforeValidate ( ) Handle 'beforeValidate' event of the owner.
canGetProperty ( $name, $checkVars = true )
canSetProperty ( $name, $checkVars = true )
createLangClass ( )
events ( )
getCurrentLanguage ( ) : mixed | string
getLangAttribute ( string $name ) : string
getTranslation ( $language = null ) : ActiveQuery Relation to model translation
getTranslations ( ) : ActiveQuery Relation to model translations
hasLangAttribute ( string $name ) : boolean Whether an attribute exists
setLangAttribute ( string $name, string $value )

Protected Methods

Method Description
getAttributeName ( $attribute, $language ) : string
getLanguageBaseName ( $language ) : string
indexByLanguage ( $records ) : array

Private Methods

Method Description
getShortClassName ( string $className ) : string
saveTranslations ( array $translations = [] )

Method Details

__get() public method

public __get ( $name )

__isset() public method

public __isset ( $name )

__set() public method

public __set ( $name, $value )

afterDelete() public method

Handle 'afterDelete' event of the owner.
public afterDelete ( )

afterFind() public method

Handle 'afterFind' event of the owner.
public afterFind ( )

afterInsert() public method

Handle 'afterInsert' event of the owner.
public afterInsert ( )

afterUpdate() public method

Handle 'afterUpdate' event of the owner.
public afterUpdate ( )

attach() public method

public attach ( $owner )

beforeValidate() public method

Handle 'beforeValidate' event of the owner.
public beforeValidate ( )

canGetProperty() public method

public canGetProperty ( $name, $checkVars = true )

canSetProperty() public method

public canSetProperty ( $name, $checkVars = true )

createLangClass() public method

public createLangClass ( )

events() public method

public events ( )

getAttributeName() protected method

protected getAttributeName ( $attribute, $language ) : string
$attribute
$language
return string

getCurrentLanguage() public method

public getCurrentLanguage ( ) : mixed | string
return mixed | string

getLangAttribute() public method

public getLangAttribute ( string $name ) : string
$name string the name of the attribute
return string the attribute value

getLanguageBaseName() protected method

protected getLanguageBaseName ( $language ) : string
$language
return string

getTranslation() public method

Relation to model translation
public getTranslation ( $language = null ) : ActiveQuery
$language
return yii\db\ActiveQuery

getTranslations() public method

Relation to model translations
public getTranslations ( ) : ActiveQuery
return yii\db\ActiveQuery

hasLangAttribute() public method

Whether an attribute exists
public hasLangAttribute ( string $name ) : boolean
$name string the name of the attribute
return boolean

indexByLanguage() protected method

protected indexByLanguage ( $records ) : array
$records
return array

setLangAttribute() public method

public setLangAttribute ( string $name, string $value )
$name string the name of the attribute
$value string the value of the attribute

Property Details

$abridge public property

whether to abridge the language ID. Default to true.
public $abridge

$attributes public property

Multilingual attributes
public array $attributes
return array

$currentLanguage public property

public $currentLanguage

$defaultLanguage public property

the default language. Example: 'en'.
public $defaultLanguage

$dynamicLangClass public property

whether to dynamically create translation model class. If true, the translation model class will be generated on runtime with the use of the eval() function so no additional php file is needed. See {@link createLangClass()} Default to true.
public $dynamicLangClass

$forceDelete public property

whether to force deletion of the associated translations when a base model is deleted. Not needed if using foreign key with 'on delete cascade'. Default to true.
public $forceDelete

$langClassName public property

the name of translation model class.
public $langClassName

$langClassSuffix public property

if $langClassName is not set, it will be assumed that $langClassName is get_class($this->owner) . $this->langClassSuffix
public $langClassSuffix

$langForeignKey public property

the name of the foreign key field of the translation table related to base model table.
public $langForeignKey

$languageField public property

the name of the lang field of the translation table. Default to 'language'.
public $languageField

$languages public property

Available languages It can be a simple array: array('fr', 'en') or an associative array: array('fr' => 'Français', 'en' => 'English') For associative arrays, only the keys will be used.
public array $languages
return array

$localizedPrefix public property

the prefix of the localized attributes in the lang table. Here to avoid collisions in queries. In the translation table, the columns corresponding to the localized attributes have to be name like this: 'l_[name of the attribute]' and the id column (primary key) like this : 'l_id' Default to ''.
public $localizedPrefix

$requireTranslations public property

if this property is set to true required validators will be applied to all translation models. Default to false.
public $requireTranslations

$tableName public property

the name of the translation table
public $tableName