You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					55 lines
				
				1.1 KiB
			
		
		
			
		
	
	
					55 lines
				
				1.1 KiB
			| 
											7 years ago
										 | <?php
 | ||
|  | /**
 | ||
|  |  * Created by Error202
 | ||
|  |  * Date: 26.08.2018
 | ||
|  |  */
 | ||
|  | 
 | ||
|  | namespace core\components;
 | ||
|  | 
 | ||
|  | use yii\db\ActiveQuery;
 | ||
|  | use Yii;
 | ||
|  | 
 | ||
|  | /**
 | ||
|  |  * Translate trait.
 | ||
|  |  * Modify ActiveRecord query for translate support
 | ||
|  |  *
 | ||
|  |  * @mixin ActiveQuery
 | ||
|  |  */
 | ||
|  | trait LanguageTranslateTrait
 | ||
|  | {
 | ||
|  | 	/**
 | ||
|  | 	 * @var string the name of the lang field of the translation table. Default to 'language'.
 | ||
|  | 	 */
 | ||
|  | 	public $languageField = 'language';
 | ||
|  | 	/**
 | ||
|  | 	 * Scope for querying by languages
 | ||
|  | 	 *
 | ||
|  | 	 * @param string $language
 | ||
|  | 	 * @param bool   $abridge
 | ||
|  | 	 *
 | ||
|  | 	 * @return $this
 | ||
|  | 	 */
 | ||
|  | 	public function localized($language = null, $abridge = true)
 | ||
|  | 	{
 | ||
|  | 		$language = $language ?: Yii::$app->language;
 | ||
|  | 		if (!isset($this->with['translations'])) {
 | ||
|  | 			$this->with(['translation' => function ($query) use ($language, $abridge) {
 | ||
|  | 				/** @var ActiveQuery $query */
 | ||
|  | 				$query->where([$this->languageField => $abridge ? substr($language, 0, 2) : $language]);
 | ||
|  | 			}]);
 | ||
|  | 		}
 | ||
|  | 		return $this;
 | ||
|  | 	}
 | ||
|  | 	/**
 | ||
|  | 	 * Scope for querying by all languages
 | ||
|  | 	 * @return $this
 | ||
|  | 	 */
 | ||
|  | 	public function multilingual()
 | ||
|  | 	{
 | ||
|  | 		if (isset($this->with['translation'])) {
 | ||
|  | 			unset($this->with['translation']);
 | ||
|  | 		}
 | ||
|  | 		$this->with('translations');
 | ||
|  | 		return $this;
 | ||
|  | 	}
 | ||
|  | }
 |