Internationalization in database

Is there an build-in method to have multiple languages in database? I have to build a website with more than one language, and I would like to know, how to handle this in databse table. I.e. i have a table with a column strName, so do I have to make a column strNameDe and a column strNameEn and in and view file something like this:

echo $model->strName{Yii::app()->language};

Is this the way, to handle it?

Ever thought of using the CDbMessageSource?

Either use the existing one (if it fits to your needs) or write your own…

You can find further information on this topic in the section i18n of The Definitive Guide to Yii here:

http://www.yiiframew…ide/topics.i18n

Ok, l'll have to take a look at this, but it's not quite cleat how I can use this for example when a have I a product table, and want to store product names in german and english.

I've just asked for a build-in solution, because "Symfony" has got one.

It's still not clear to me how I can handle the following scenario:

I have a table called "Products" and in the _form.php of products I have two input fields (there are a lot of more):

strProductNameEn

strProductNameDe

Can I use CDbMessageSource to save the product names (english and german) in database with having the multi-langual values in a separate table, with only one column in table "Products" for strProductName or does the "Products" table needs to have two columns, to hold multi-lingual values of a model (which doesn't require an extra table)?

Other fields of the model are i.e. strProductExcerpt(En/De), strProductDescription(En/De).

Any suggestions?

extend the CActiveRecord class and override tableName() method.

Insead of:



return get_class($this);


use



return get_class($this).Yii::app()->language;


Then create your model which should use the class you just created instead of CActiveRecord.

on this topic :

is there something built in that resembles a translate behavior where you have a translated fields table used as a repository for all the fields you need translated ? The translation table has the following fields :

  • locale (language)

  • model (like Post, Comment, Product etc … )

  • foreign_key (the id of the row in foreign table)

  • field (the field in the row that has to be translated)

  • content (the translated content)

Thanks

Quote

extend the CActiveRecord class and override tableName() method.

Insead of:



return get_class($this);


use



return get_class($this).Yii::app()->language;


Then create your model which should use the class you just created instead of CActiveRecord.

which means, that I have two tables: "ProductsEn" and "ProductsDe"?

If so, that's no good solution, because all other fields (which are not translated) has got to be stored in two tables.

If you want to translate messages stored in the database in a easy way, have a look here: http://www.yiiframew…pic,433.0.html

Quote

If you want to translate messages stored in the database in a easy way, have a look here: http://www.yiiframew...pic,433.0.html

I think you light want to look at the Translate behavior in CakePhp

http://book.cakephp…ew/92/Translate

which does what you are looking for I believe

Let  us know  if this is the case and if you come up with a nice behavior for Yii :)

hey thomas, this is exactly what I'm looking for. how could this be handled in Yii?

As I explained in a former post to this thread you need to have a table that will store translation of table(model) fields(attributes) for you

locale (language)

model

field

foreign_key

translated_content

you have to write the logic for saving / displaying the translated content

my guess is that havng a look at how it’s done in cakephp might help ;)

but it can be tricky especially if you deal with lots of relations

any insight from the dev team here would be really nice :)

ok, I'll try it. hope I will get it working.

Quote

thank you for the reply, but I think that's not what I'm looking for. I don't want to store the translation coming from php code in the database! what I want to do is, to handle multi-language columns in all tables

oh I see… I’ve misunderstood your post  :P

anyway the question you made is interesting for mee too… so please let me know wich will be your solution ;)

hope that anyway you'll find my tool useful as you are going to work with multiple language (I do prefer to store strings in db rather than in text files but this is another topic) … anyway just for say that I believe that Yii is very powerfull in handling multi language projects but since now I didn't saw many projects that are using this features in a heavy way.

And also didn’t knew about that cake php feature that looks useful ;) The Yii dev. team is very open to this kind of improvements according to what I saw… so I believe that version 1.1 will be awesome.

bye,

Giovanni.

@giovanni: I made a feature request, because I need this feature for 3 projects in the near future, and I don't know how to handle this without Yii having this feature built-in.

could you please join this request here: http://www.yiiframew…pic,1599.0.html

Bitmatix, I have the exact need for this. I think there is a solution already. The Doc system is very similar to what we need. You click any language icon and you see the same chapter in that language. I am not sure how much coding was included there but I assume it should be pretty light.

Until Yii doesn't provide that functionallity, maybe "behaviors" could be helping with this problem.

Hmm, I have to think about that. I really need this for a project which is starting today.

Ok, I've found a way to handle this with attached behaviors. The onyl problem is, that the "fake" translation fields have to added manually in the model, i.e. if your main language is german and you have a field called "strName" which should be translated into english, you have to add:

public $strNameEn;

and the in rules() function:

public function rules()


{


	return array(


		array('strName','length','max'=>255),


		array('strNameEn','length','max'=>255),


		array('strName, strNameEn', 'required'),


	);


}

you also have to add the safeAttributes function with something like that:

public function safeAttributes()


{


  return CArray::merge($this->attributeNames(), array('strNameEn'));


}

I hope to get this things a little bit more dynamically, but actually I don't know how!? Maybe there's some help or inspiration from the Yii developers :slight_smile:

the rest (reading and writing the translated values) is done by the behavior class.