Product Table And Languages

I’ve a Products table with many fields. I’ve to manage 5 different languages for these products (some of table fields needs to be translated).

Can you suggest me the best way for manage this? I mean:

  • create field_EN field_FR field_IT … for every field

  • use other solution (for example with external files)?

Thanks

I would create a relation to hold a field value for each language for a specific product.

I use field_EN to avoid unnecessary joins.

Do you mean create N different field, for every language

Description_EN

Description_IT

Description_FR

Description_ES

Do you mean this??

But is not better to have a single field in DB and use this field for manage translation with external file? I mean that field in DB is a variable for a function that translates from variable to right language. What do you think?

Thanks

Yes.

Of course, it can be painful to add new language to system (all the tables must be altered), but luckily it happens not so often :)

I don’t see any reason to use external files while we have database.

Anyway, my primary goal is speed, so one query is always better than N queries, or one query + file, or whatever.

There are some caveats, but I can deal with that.

Ok, so do you suggest _LANGUAGE for every field? This for my case with 5 languages… but having 100 languages, are you sure that this is better way?

Thanks

I’m facing a similar situation with a website I’m starting to build. I think @orey’s solution is better but only if one is absolutely sure that no more languages beyond the initial 5-10 will be supported. If more languages may be needed afterward then I would use @nineinchnick’s approach.

Yes, if there are 100 languages, it’s hard to do it my way.

I usually have 2-3 max.

I think in the end the language structures should be invisible to the application logic. That is, you set current language somewhere and all the components shouldn’t rely on knowing that value, just like with using Yii::t() for labels.

Using ORey’s solution, there could be a getter for an attribute that would read the value from the proper one.

Using a relation in the database there could be a condition in the default scope that would make sure only one row would be returned for the current language. And that one row could be further wrapped in a getter to avoid reading it like an array with one element.

I think the best solutions is get a all db filed when type is varchar using CDbTableSchema

so first tou can create the languauge table defind all the languauge and this languauge id on main user table.

then after fetch all varchar filed to all db then you can use this syntax


Yii::t('app', 'Save')

every lable and button so all varchar filed are translate

like


public static function getLanguageFields($model,$attribute, $form ,$error=false){


		$admin=new AdminUser();

		$wine_detail = AdminUser::model()->find('default_languauge');

		$id=$wine_detail->default_languauge;




		$user=new AdminUser();

		$uname=$user->getUserData(Yii::app()->admin->id);

		$wine_detail = AdminUser::model()->find('id='.Yii::app()->admin->id);

		$id=$wine_detail->attributes['default_languauge'];


  	//get all field varchar

		$all_field = $model->getTableSchema()->getColumnNames();

		$type_detail = $model->getTableSchema()->getColumn($attribute);

		$type = $type_detail->dbType;

  	//end




		$lang = Languages::model()->findALL('id='.$id);

		

		foreach ($lang as $row) {

			

			echo '<div class="row">';

			echo CHtml::label(ucfirst($attribute.'['.trim($row->lang_name).']'),$attribute.'_translate_'.$row->id.'',"");

			$lang_val = LanguagesTrnaslate::model()->find("lang_id='".$row->id."' AND table_id='".$model->id."'");


			if($model->id!=''){

				$val = '';

				if(isset($lang_val->value) && !empty($lang_val->value)){

					$val = $lang_val->value;

				}

				if(stristr($type, 'varchar') == $type_detail->dbType) {

					echo CHtml::textField($attribute.'_translate['.$row->id.']',$val, array('id'=>$attribute.'_translate_'.$row->id));

				}

				if(stristr($type, 'text') == $type_detail->dbType) {

					echo CHtml::textArea($attribute.'_translate['.$row->id.']', $val, array('id'=>$attribute.'_translate_'.$row->id));

				}

			}else{

				$val = '';

				if(stristr($type, 'varchar') == $type_detail->dbType) {

					echo CHtml::textField($attribute.'_translate['.$row->id.']',$val, array('id'=>$attribute.'_translate_'.$row->id));

				}

				if(stristr($type, 'text') == $type_detail->dbType) {

					echo CHtml::textArea($attribute.'_translate['.$row->id.']', $val, array('id'=>$attribute.'_translate_'.$row->id));

				}

			}

			echo '</div>';

		}

	}