The reason is that models currently don't have a way to check if a missing attribute can be obtained through a behavior's magic getter/setter. This is also the main reason why I first wrote this as a CActiveRecord subclass instead of a behavior. But I prefer to have it as a behavior, even with this inconvenience.

I also have suggestions for better names for relName1 and relName2: localizedRelation and multilangRelation

As a final thought, the point where you create $this->langForeignKey from $owner->tableName() is broken if tableName returns a string containing the database name (e.g. 'mydb.products')
You can replace

$owner->tableName()

with

array_pop(explode('.',$owner->tableName()))

to achieve that extra compatibility.
But those are all small points. All in all, this multilang stuff is really useful.

I also have suggestions for better names for relName1 and relName2: localizedRelation and multilangRelation

As a final thought, the point where you create $this->langForeignKey from $owner->tableName() is broken if tableName returns a string containing the database name (e.g. 'mydb.products')
You can replace

$owner->tableName()

with

array_pop(explode('.',$owner->tableName()))

to achieve that extra compatibility.
But those are all small points. All in all, this multilang stuff is really useful.

I'm using the MultilingualBehaviour, and it's working fine in an example I created. The user gets to choose in wich language wants to display the sections and posts related, and it gets the translation correctly.

But my question is how I change the Models and Controllers of the Posts and Section (in the example) to be able to introduce all the files in the diferent lenguages in the Add and Update Form?
Using the previous MultilingualActiveRecord class, by defining the localizedAttributes, languages, etc. it was correctly displayed all the language fields on the form view...

So... i'm a bit lost in here, because I don't undestand why is looking for the file in the Posts model, instead of the PostsLang model...

Any help?

dianakwt, on 25 January 2012 - 07:20 AM, said:

Hi guillemc and heal!

I'm using the MultilingualBehaviour, and it's working fine in an example I created. The user gets to choose in wich language wants to display the sections and posts related, and it gets the translation correctly.

But my question is how I change the Models and Controllers of the Posts and Section (in the example) to be able to introduce all the files in the diferent lenguages in the Add and Update Form?
Using the previous MultilingualActiveRecord class, by defining the localizedAttributes, languages, etc. it was correctly displayed all the language fields on the form view...

Hi, it's looking in the Post model because that's what we're after, in "multilang" mode: to make appear the attributes of PostLang model as if they were attributes of Post, for easier handling (like title_es, title_de, etc).

Probably the problem is that you are not loading your model in multilang mode. In my controllers I use this modified loadModel():

I missed that completely! I'm not only a newbie on the forum, but also in yii developing... You did a very useful behavior!!

Now it's working perfectly.

Diana.

guillemc, on 26 January 2012 - 01:37 PM, said:

Hi, it's looking in the Post model because that's what we're after, in "multilang" mode: to make appear the attributes of PostLang model as if they were attributes of Post, for easier handling (like title_es, title_de, etc).

Probably the problem is that you are not loading your model in multilang mode. In my controllers I use this modified loadModel():

I put this all here into the base file, since the code will always be the same for each model and it will be needed by almost all of my models (and if not, that's no problem, they get ignored).
Note: RES::lc is just a helper function turning every letter into lower case.

Hey guys,
first of all thanks for the (I suppose, because I havn't tried it yet) great work! has anyone tried to compile the above behaviours, components and changes to be made in the models and controllers and publish it as an extension?

I'm going to try to filter all this information and use it to suit my needs anyways, but I just wanted to get the idea out there.

I've tried to make a compilation of everything in this thread and to make things work only with a behavior, without changing the model. So you don't have to overload getters or setters in the model. Only to attach the behavior and define the defaultScope.

And of course create a new table in the database to store translations.
The table has this form by default (example for a base table named "post") :
postLang: l_id,post_id,lang_id,[list of attributes to translate prefixed by 'l_'(configurable)]

The attributes to translate have to be also in the base table but with no prefix.

I've edited my previous post and the behavior file with bug corrections and improvements. Mainly I've tried to enable search on translated fields and to do so I've changed a little the way the lang table has to be created in the database.
Now the translated attributes and the id have to be prefixed (with 'l_' by default, but configurable).
And I've added a "modifySearchCriteria($criteria)" function in the behavior to use in the "search()" function of the model.
Look at my previous post if you are interested.

Hey there, @fredpeaks, I've been using your behavior and it works like a charm, I've also made a few changes of my own, namely including vars for a "created" and "modified" datetime fields. That was amazing work, man. Also thanks to @gillemc, I believe your work was based on his, right? And yes, this should be on the base setup of Yii sometime soon.

Attached File(s)

Thanks again and of course thanks to guillemc who has made the biggest part of the work on this behavior.
About your modifications, I'm sorry but I don't think datetime fields management have to be included in this behavior because it's a different functionality. In my application, I've used the CTimestampBehavior (included in Yii) next to the MultilingualBehavior and it work fine. And maybe I'm gonna have multilinguals models where I won't have datetime fields for some reason. So I don't think we should mix both functionalities.

I'm going to contact guillemc and ask him if he's interested in publishing this behavior in the Yii extension repository because I think this is an important feature and it should be easily accessible to everyone.

A few bugs corrected, add a "multilang()" function to ease the use and a change made on the "multilangRelation" default name ("multilangPost" instead of "multilang") in order to be able to retrieve languages translation of two or more related models in one query.
Example for a Page with a "articles" HAS_MANY relation :