Onmissingtranslation

Hi

I have create a translation system.

If the translated message not find (using missing event) will be used another system tranlation


 'messages' => array(

            'onMissingTranslation' => array('myClass', 'alterTranslation'),

        ),



The alterTranslation use Yii:t function that raise another MissingTranslation recursively, so

Fatal error occurs with the message “Maximum function nesting level of ‘100’ reached, aborting!”

How to stop the MissingTranslation inside alterTranslation function ?

This article is related with this one

http://www.yiiframework.com/forum/index.php/topic/50406-check-if-translated-message-exists/

Thanks

Post your code for

‘myClass::alterTranslation()’


public static function alterTranslation($event) {

 return Yii::t("anotherCategory", $event->message, $event->params);

}

Also it is not exactly what I want to do!!

onMissingTranslation not runs when the translated not exists, but on another time

for example

var_dump (Yii::t(‘firstCategory’, ‘A not translated message’)); die();

the result is "A not translated message"

So, how to have alternatives translation system ?

Ok, so what you actually need is to look in multiple places for a certain translation, right?

If so, i would go something like this:




    public static function alterTranslation($event) {

        

        static $lookupTable = array(

            'firstCategory',

            'secondcategory',

            'thirdCategory',

            'nthCategory'

        );

        static $lookedInto = array();

        

        $category   = $event->category;

        $message    = $event->message;

        

        if (!in_array($category, $lookupTable)) {

            return;

        }


        if (!isset($lookedInto[$category])) {

            $lookedInto[$category] = array();

        }

        

        if (!isset($lookedInto[$category][$message])) {

            $lookedInto[$category][$message] = $message;

        } else {

            return $event->message = $lookedInto[$category][$message];

        }

        

        $nextCategory = null;

        foreach ($lookupTable as $index => $cat) {

            if ($cat == $category && !empty($lookupTable[$index + 1])) {

                $nextCategory = $lookupTable[$index + 1];

                break;

            }

        }

        

        if (empty($nextCategory)) {

            return;

        }


        return Yii::t($nextCategory, $message, $event->params);

    }



It is not tested and you will always have to declare the categories where you want Yii to look and the order you want this to happen.

It should at least give you a starting point.

L.E: Though, looking at this a second time, i can’t really understand how you would end up needing such a solution, it seems you are going wrong about this.

Thanks for response!

but the problem is not how to choose alternative categories but the onMissingTranslation runs revursevly (infinite)

The other main problem is that onMissingTranslation cannot use for Yii::t replacing directly or not. It used after of translation either exists or not.

I want something like that

my_t(…)

…check in php file categoryA (if not exists then…)

…check in php file categoryB (if not exists then…)

…check in database categoryA (if not exists then…)

…check in database categoryB

But I have to replace Yii::t with Yii::app()->myClass->my_t(…) for all my code…

Is there a way to override Yii::t ? In this case it is not nessesary to change Yii::t throughout entire my project…

Ah, i see.

Yii class is just extending the YiiBase class.

You can have it extend a MyYiiBase class which in turn extends YiiBase.

In your MyYiiBase class you would override the t static method to match your needs.

Ok, after of reading another forums and posts,

I think this is the most appropriate way to do that, although I would avoid to extends entire YiiBase class for only one method…

Thanks twisted1919 :)

No problem ( I don’t think you have another way to do it so just go ahead :) )

Because this functionality will be used for a module, I think is is preferable to write my own function t and replace all Yii::t. I dont want use this module for other application and forced to change the configuration of the main project.

In any case thanks!