MultiLanguage captions module

mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Wed, 2004-12-01 21:46

Edit: The latest version does not need any modification to core code! Simply install and use..
Edit: This module is now in cvs!

Hi all--
I think there are a few people out there who really want this feature.. we don't have a full solution yet, but I have put together some code to support this with only a small change to the G2 core code, so if you're interested you can try it out.

What it does:

  • Adds a site admin page to pick the languages you wish to support
  • Adds a MultiLanguage tab on the edit item view.. here you can set the title/summary/description for your additional languages (you still put the "primary" title/summary/description in the normal edit view)
  • When items are loaded it checks the multilanguage table for the user's language and overrides the title/summary/description if data is found

The first 2 items above fit nicely in G2's module system.. the last one requires a small change to G2 core code.

To use this module:

  • Download the attached file and unzip in your gallery2/modules directory
  • Edit modules/core/classes/GalleryItem.class and add the following code just before the last } in the file:
    function onLoad() {
        $ret = parent::onLoad();
        if ($ret->isError()) { return $ret->wrap(__FILE__, __LINE__); }
        $view = GalleryUtilities::getRequestVariables('view');
        if ($view != 'core:ItemAdmin') {
          GalleryCoreApi::relativeRequireOnce('modules/multilang/classes/MultiLangHelper.class');
          $ret = MultiLangHelper::onLoad($this);
          if ($ret->isError()) { return $ret->wrap(__FILE__, __LINE__); }
        }
        return GalleryStatus::success();
    }
  • Install/activate the module, visit the site admin page to pick languages
  • Edit a few items to set captions in multiple languages
  • Change the language for your session (visit site admin / general to turn on language selector in sidebar if needed) to see the captions change!

Enjoy![/]

[/]

AttachmentSize
multilang.zip21.45 KB
 
baschny
baschny's picture

Joined: 2003-01-04
Posts: 328
Posted: Wed, 2004-12-01 22:32

Awesome, it totally rocks! The UI needs some improvement, but its a first nice step in a good direction here!
Thanks, mindless

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Wed, 2004-12-01 22:42

Agreed.. I just put together the minimum to get the functionality for those who really want it.. it's lacking in:
- seamless integration (needs that patch)
- search support
- performance (adds a query for every item loaded.. no caching now)

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Wed, 2004-12-01 23:49

and baschny pointed out that if you edit the regular title/summary/description while your session language is one of your additional-languages then it will show you the multilanguage data instead of the real title/summary/description data. so be careful of this if you try out this module... edit the primary title/summary/description fields with your session using your primary language.

 
Robert_Dubinski

Joined: 2003-10-06
Posts: 24
Posted: Tue, 2005-01-18 19:37

Are there any updates to this code? Any work to get it into the mainline?

It'd be great to have multi-language captioning out-of-the-box.

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Tue, 2005-01-18 19:44

no further work has been done. if anyone is interested in working on this, let us know.

i just updated the attachment above to work with recent core api changes.

 
galfn

Joined: 2005-01-31
Posts: 2
Posted: Mon, 2005-01-31 07:41

API is updated again. Can you please update your module? Thanks a lot. :D

 
virshu
virshu's picture

Joined: 2003-09-13
Posts: 314
Posted: Mon, 2005-01-31 08:21

mindless, if you don't mind I'll be happy to take over it (especially that I really need it as well)

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Mon, 2005-01-31 16:49
galfn wrote:
API is updated again. Can you please update your module? Thanks a lot. :D

attachment above is updated.. thanks for the reminder to post the update.

virshu, do you have a design in mind to make this a well-behaved module? (ie, not requiring that patch in GalleryItem....) Perhaps an onLoad event? (adds a big load in event management though, as this event happens extremely frequently)

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Mon, 2005-01-31 16:58

I also updated the "onLoad" block in the first post above to resolve the problem where multilang data is shown in Edit Item / General... it won't put the multilang data in place for core:ItemAdmin view so you'll see the primary language data while editing items.

 
virshu
virshu's picture

Joined: 2003-09-13
Posts: 314
Posted: Mon, 2005-01-31 17:36

My first reaction would be to have module to register an even listener, something like:

function registerEventListeners() {
    GalleryCoreApi::registerEventListener('GalleryIem::load', 
        new MultilangModule(), true);
}

and then have GalleryItem traverse the list of registered modules. That's what you are suggesting, right?

Performance-wise, I thought that if there are no modules registered, and there is no list to got through, then the performance overhead is minimal (although minimal times a lot may still be too bad). I guess, it's worth getting it implemented - and then do some profiling...

 
ovis
ovis's picture

Joined: 2004-06-02
Posts: 8
Posted: Wed, 2005-02-02 13:07
mindless wrote:
Hi all--
I think there are a few people out there who really want this feature.. we don't have a full solution yet, but I have put together some code to support this with only a small change to the G2 core code, so if you're interested you can try it out.

Is there any online example of this setup ?

thanks

Gr Ovis

 
virshu
virshu's picture

Joined: 2003-09-13
Posts: 314
Posted: Wed, 2005-02-02 17:43

mindless, I noticed that if I start gallery and I am already logged in, I am getting default captures only. Apparently, Gallery::setActiveUser() is never called, and my language never gets put in the session. So, when Gallery::getActiveLanguageCode() is trying to get language from the session - there ain't none!

If I log out and then log back in - it's working fine.

I realize, that if my analysis is correct - it isn't really up to this module to fix it... But I think you still would want to know it :wink:

 
virshu
virshu's picture

Joined: 2003-09-13
Posts: 314
Posted: Wed, 2005-02-02 19:48

ouch, and if you make all the changes that are required you won't be able to get into SiteAdmin menu...

  Error (ERROR_STORAGE_FAILURE ERROR_UNKNOWN):

    * in file E:\web\gallery2\modules\core\classes\GalleryStorage\DatabaseStorage.class at line 735 (MySqlDatabaseStorage::error)
    * in file E:\web\gallery2\modules\core\classes\GalleryStorage.class at line 220 (MySqlDatabaseStorage::search)
    * in file E:\web\gallery2\modules\core\classes\Gallery.class at line 184 (GalleryStorage::search)
    * in file E:\web\gallery2\modules\multilang\classes\MultiLangHelper.class at line 51 (Gallery::search)
    * in file E:\web\gallery2\modules\multilang\classes\MultiLangHelper.class at line 128 (GalleryAlbumItem::getItemData)
    * in file E:\web\gallery2\modules\core\classes\GalleryItem.class at line 465 (GalleryAlbumItem::onLoad)
    * in file E:\web\gallery2\modules\core\classes\helpers\GalleryEntityHelper_simple.class at line 72 (GalleryAlbumItem::onLoad)

Apparently, GalleryItem is trying to execute the module before it is even activated. So, for now you need to install and activate the module first, and make changes in GalleryItem only after that...

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Wed, 2005-02-02 19:49

yes, i didn't put an active-check in this hack :)

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Wed, 2005-02-02 19:59
virshu wrote:
mindless, I noticed that if I start gallery and I am already logged in, I am getting default captures only. Apparently, Gallery::setActiveUser() is never called, and my language never gets put in the session. So, when Gallery::getActiveLanguageCode() is trying to get language from the session - there ain't none!

If I log out and then log back in - it's working fine.

I realize, that if my analysis is correct - it isn't really up to this module to fix it... But I think you still would want to know it :wink:

how can setActiveUser not get called? it's in init.php, GalleryInitSecondPass. maybe you can debug further why you see the wrong language..

 
virshu
virshu's picture

Joined: 2003-09-13
Posts: 314
Posted: Thu, 2005-02-03 21:09

will do... Bharat gave me 4-page code review; my ears are buring; in the meantime I played with the locks too much (the habits of old thief die hard :) ) and now nothing is working; so it will take a little while!

btw, what did we decide about 'registering' for OnLoad (if anything)?

I'll keep posting here (probably tomorrow)

 
virshu
virshu's picture

Joined: 2003-09-13
Posts: 314
Posted: Fri, 2005-02-04 07:08

mindless, you were right - my guess about setActiveUser was off-base... Here is what's happening - if you go to User preferences, and change the language - this change doesn't propagate to current session. Once I have the following change in UserPreferences

if (isset($form['language'])) {
    $user->setLanguage($form['language']);
    /* update the effective language in the current session */
    $session =& $gallery->getSession();
    $session->put('core.language', $form['language']);
}

it works fine.

If you don't have any objections - I'll check it in.
Cheers

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Fri, 2005-02-04 07:19

sounds good to me.. and since you took us on a tangent, how about posting something to get this topic back on track? :)

 
virshu
virshu's picture

Joined: 2003-09-13
Posts: 314
Posted: Thu, 2005-02-10 04:15

attached is the module that doesn't require 'hacking' the core code; however it does require changing core code (the difference between hacking and changing will be a topic of another discussion :P )

You need to take out the changes that were described in GalleryItem.class and replace the onLoad function with the following:

function onLoad() {
    $ret = parent::onLoad();
    if ($ret->isError()) { return $ret->wrap(__FILE__, __LINE__); }
    $event = GalleryCoreApi::newEvent('GalleryEntity::load');
    $event->setEntity($this);
    list ($ret) = GalleryCoreApi::postEvent($event);
    if ($ret->isError()) {
        return $ret->wrap(__FILE__, __LINE__);
    }
    return GalleryStatus::success();
}

I think merging this into the base is coming soon now...

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Thu, 2005-02-10 06:34

yes, i already have code like this in my dev install.. the code you posted doesn't work though, right? to implement this event strategy i had to change GalleryEvent to set and get the entity by reference.. otherwise your event handler is just setting the multilang values on a copy of the item..
how will you handle searching? i suppose you can implement the search interface and search against the session's language, though it will seem odd to users that the main search checkboxes apply to some other language and to search against their own lang they must check some other box..

 
virshu
virshu's picture

Joined: 2003-09-13
Posts: 314
Posted: Thu, 2005-02-10 16:40

eeehhh - it does work... http://www.galan.rabinovich.org/gallery2/ I wouldn't post this code here if it didn't, now wouldn't I?

What I think is happening - PHP5 is copying obects by reference without any additional effort on my part. When I uploaded to PHP4 site - it is indeed making changes on the update only, and you are right - it doesn't work!

So, for all of you on the edge with PHP5 - take it away... The others should upgrade :evil:

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2005-02-19 03:30

Today I added an onLoadHandlers field to GalleryEntity. Now multilang module can add a handler on any item where multilang data is added. Bonuses:
1) this module works with no changes to core code
2) query on multilang table is only executed for items that have some multilang data (not necessarily for the active language though)

I've attached the updated module back in the first post of this topic. I also added a search impl.. so now you can search the title/summary/description of the active language too. seems like we are much closer now to getting this module in cvs.. we'll see how the next review goes.

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sat, 2005-02-19 21:58

Remaining problems:
1) if you have a non-default language selected the first time an item is loaded then the captions for that language are placed in the disk cache.. if you then switch to the default language you still see those captions (since it gets the item from the cache). i have a core update locally that fixes this.. basically always put the pre-onload item into the disk cache. this makes more sense anyway since we already run onload after we load an item from the disk cache (so currently those items get on-loaded twice).
2) sooner or later you're gonna get your default-language captions overwritten by another language.. since we replace the default language captions with the active language at the time the item is loaded, if at some time later in the request that item gets saved it will save with those values in place. the 0.3.0 code skips the multilanguage check for core:ItemAdmin view so you see the right thing in the edit-item General tab. i found if i save a change on edit-album Album tab I can get the active-language captions copied into the main captions. i can avoid this by checking for ItemEdit controller, but who knows where else an item may get saved? perhaps the right thing is to skip multilanguage check for any controller? hmm..

 
Jester2000
Jester2000's picture

Joined: 2003-06-24
Posts: 40
Posted: Sun, 2005-02-20 20:07

whoops.... ignore this post...

:roll:

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sun, 2005-02-20 20:35

um, Jester2000, maybe you thought you were posting in the G2 Demo Sites topic..

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Sun, 2005-02-20 22:16

fix for #1 above just committed to cvs.

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Thu, 2005-02-24 21:28

this module is now in cvs, yay!
for problem #2, i went with skipping the multilang check for any controller.. so it's possible some delegated view may show default language instead of active language, but much less likely your captions will get overwritten accidentally.

 
antisa33

Joined: 2006-06-26
Posts: 30
Posted: Tue, 2006-07-11 14:23

Hello
Thanks for this great MultiLanguage Module :-)
But i have a question about comments !
I don't want for example that a deutsh see french comments !
How can we do ?
Thanks a lot for everything
Isa