ADVICE: multi-language database content support for Gallery2

nepto
nepto's picture

Joined: 2004-07-06
Posts: 22
Posted: Mon, 2005-01-10 00:50

Yet another feature/hack from me, probably the last one :-)

Multilanguage site is a site serving content in more than one language, typically in two languages - native one and the english one. For example my http://nepto.sk/ is serving content in the slovak language as well as in the english language.

Once you have this kind of site, you may want to have multilanguage not only interface, but some of the database content as well. For example the name of root node in the Gallery2 is called "Gallery. Assume that in your native language is "Gallery" called "Galeria" (this is how we say "Gallery" in the Slovakia). When serving engligh language content, "Gallery" should be fetched from database, when serving other (slovak) language content "Galeria" should be fetched from database.

But there is only one field/column, so how to achieve this kind of behaviour? It is not so difficult as it probably might looks like!

We will use Smarty modifiers for this task and store database content in this form:

[lang:sk]Galeria[lang:en]Gallery

  1. First of all we need to create simple parsing function. Assume that in the variable $cfg[params][lang] is two-letter code of language, which content is site currently serving.
    function Nepto_SK_extract_language_content($content) /* {{{ */
    {
        global $cfg;
        if (preg_match('/^(\[lang:\w\w\].+){2,}$/', $content)) {
            $array = preg_split('/\[lang:(\w\w)\]/', $content, -1, PREG_SPLIT_DELIM_CAPTURE);
            $count = count($array);
            $cur_content = null;
            $eng_content = null;
            for ($i = 1; $i < $count; $i += 2) {
                if (! strcasecmp($cfg['params']['lang'], $array[$i])) {
                    $cur_content = $array[$i + 1];
                    break;
                } else if (! strcasecmp('en', $array[$i])) {
                    $eng_content = $array[$i + 1];
                }
            }
            if ($cur_content !== null) {
                return $cur_content;
            }
            if ($eng_content !== null) {
                return $eng_content;
            }
        }   
        return $content;
    } /* }}} */ 

    You should alter this function to fit your needs.

    This function will search for paticular language content. If not available, english content willl be used. If any other problem occurs, for example content for particular language cannot be extracted, content will remain unchaged.

  2. We need to make this function a Smarty modifier and activate it. Open file modules/core/classes/GalleryTemplate.class, search for the _getSmarty() function, it is around the line 280. In this function is Smarty object created and initialized. We need to add there following command:

    $smarty->register_modifier('lang_extract', 'Nepto_SK_extract_language_content');

    This will include Nepto_SK_extract_language_content() or better say lang_extract into the list of available Smarty modifiers and we will use it in our templates.

  3. Now open all neccessary templates and add this new lang_extract modifier where neccessary. For example page title template part could looks like this:
    <h1 class="giTitle">                                                                                                                 
      {$layout.item.title|markup}                                                                                           
    </h1>                                
    

    Change this to:

    <h1 class="giTitle">                                                                                                                 
      {$layout.item.title|lang_extract|markup}                                                                                           
    </h1>                                
    

    Do not forget to make your local copy of the template file and leave original template file unchanged; see initial template file comment for more information about this.

  4. We are done. Now instead just Gallery enter [lang:sk]Galeria[lang:en]Gallery into the gallery name input field. Of course, this could work also for other fields, for any other database content.

If you find this advice useful or you have some questions or troubles, please let me know.

Nepto, http://nepto.sk/[/]

 
mindless
mindless's picture

Joined: 2004-01-04
Posts: 8601
Posted: Mon, 2005-01-10 04:05

interesting approach..
also see this topic for other work in this area.

 
bharat
bharat's picture

Joined: 2002-05-21
Posts: 7994
Posted: Mon, 2005-01-10 05:21

Neat hack! I hope that this isn't the last one.. your hacks are very thought provoking.

The main problem I have with this approach is that we're storing non-normalized data in the database. It's definitely a workable short term solution, but I know that it will lead to all kinds of interesting long term issues. For example, we'll have to know that we have non normalized data when we export data to EXIF, IPTC, as XML, etc. We'll also have issues with searching the data because our SQL will search the entire column, and can't limit itself to the active language.

I agree that we should get this functionality into the product; I'm still just not sure what will be the best way to do it...

 
nepto
nepto's picture

Joined: 2004-07-06
Posts: 22
Posted: Sun, 2005-10-23 04:55

bharat, I'm considering your comment as a compliment, thanks ;-)

Of course, this is not the *solution*. This is a hack. And it is the reason why I posted it here to the forums and not for example to the developer mailinglist. I hope this will be archived for a longer period and anyone interested and capable can use it.

Several months ago I was in need to estabilish my gallery imediately, because I had a really huge amount of pending photos. I was sure that if I would not put them on the website then, I will never do so. So I went with hacks...

Now I'm starting to watch Gallery2 development again. I see you did a good job during last few months. There is stable release 2, even 2.0.1. That is great! Once all my requirements will be met, I will migrate my gallery instance to the stable release.

Of course I will make some code upgrades, if neccessary. I'm skilled PHP coder, however I know nothing about G2 API. But that is not a big problem anyway. If I will find a time, I'll provide you with serious patches.

http://nepto.sk/gallery2/?lang=en

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Sun, 2005-10-23 13:16

also see:
http://gallery.menalto.com/node/24999 (just linking the two topics).
which also points to:
http://codex.gallery2.org/index.php/Gallery2:Embedding_Development_Roadmap#Variable_Input_.2F_Output_character_encoding
since i see this feature as an embedding relevant feature.
i'd prefer seeing a output filter solution that doesn't require changing the templates etc. one should be able to change the charset globally in config.php or in a site admin page.

 
jraczkow

Joined: 2005-10-26
Posts: 7
Posted: Wed, 2005-11-02 22:25

Hi.. Sorry for not being competent enough, but I'm just starting with Gallery (and in the same moment PHP/Smarty/SQL everything....).
This looks as a very interesting feature, whitch I would like to use...
Could you please tell me where to register this function? (which file to add it to?) Thanks.
I added it to GalleryTemplate.class as one of the functions, but the system seems not to see it....
Thanks,

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Wed, 2005-11-02 23:33

jraczkow

what are you refering to?
multilanguage features?
since this topic was created in 2005/01, a multilanguage module has been added to G2. which basically does what's discussed here. you can annotate each item in G2 in multiple languages with it.
install and activate the multilanguage module and start annotating in multiple languages as you wish.

 
jraczkow

Joined: 2005-10-26
Posts: 7
Posted: Thu, 2005-11-03 07:44

Hi,
Of course I did, but I was thinking about adding some translation which is not done (or is it?) by Multilanguage module. For instance I would like to add names to small navigation icons in PGtheme in 4 languages. Could this be done straightforward by Mlng module? I was thinking about adding this modifier, then adding a variable with the names in some .tpl displaying correct language.

Either way... Where should this modifier be put to be visible for another classes? (I put it to /lib/smarty/plugins/modifier.lang_extract.php but still it doesn't work (and cleared the cache).
Is there any graphical tutorial about developing gallery ( i mean UML or sth). It would be easier to understand what depends on what...

Thanks! (Great JoB!!!! WOW!)

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Thu, 2005-11-03 12:12

you've got to differentiate between:
1. G2 application / navigation texts
and
2. G2 content / item annotation texts

1. is text that ships with G2 / G2 themes / modules. These texts are handled by G2's translations that ship with G2 / G2's modules/themes
if G2 or a user contributed theme/module isn't 100% translated into a language you'd like to have, then you have a problem. you could start translating yourself or get one of the g2 translaters to work on it.
http://codex.gallery2.org/index.php/Gallery2:Localization

2. is text that you added. you added titles, descriptions, summaries etc.
G2 can't translate that automatically. thus we added the "multilanguage" module. With the multilanguage module you can add descriptions in as many languages as you want for all items.

the combination of 1. and 2. results in a 100% translated / localized G2 experience for all your users.

 
u4david

Joined: 2007-03-25
Posts: 11
Posted: Sun, 2007-04-15 04:27

I'm more interested in actual translation of non default g2 text.Typing all in more languages can get out of date.So actual on fly translation would be the way to go.
Implementing like google "translate this page engine" >to default/browser language on the fly would be nice future.That would imply only on user added text/comments.

 
valiant

Joined: 2003-01-04
Posts: 32509
Posted: Sun, 2007-04-15 13:12

If google offers an API to translate stuff via GData or so, why not.
Machine translation is a very challenging topic and google must be throwing a lot of resources at it to get the impressive results we see.
It doesn't make a lot of sense to start a machine translation module for G2, unless it uses existing machine translation tools / services.

--------------
Documentation: Support / Troubleshooting | Installation, Upgrade, Configuration and Usage

 
gopu
gopu's picture

Joined: 2009-08-28
Posts: 137
Posted: Wed, 2009-12-02 14:56

Hello,

Since the thread is similar to what I want, I am posting my queries in this thread.

I would like to develop (or enhance the existin multilang) module that will enable user to have multilingual URLs for items, with rewrite module enabled.
For example for a "beautiful rose" photo under rose album, I would like to have following URLs for english and spanish respectively:

Can somebody please give me some pointer to go about to implement it?

-----------------------------
my wings of love - work, experince, learnings, open source, technologies on my way to God