We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

I18N - I need a hook on not found keys / translations

I use the Phalcon build in I18N functionality as shown in docu and now want to achieve this: In case a key is not already in csv or in case a requested translation is not there I want to trigger a certain action. Action could be just logging or adding the key to the csv or any kind of notification. Why? I have written a bigger project at the beginning only one language but already using always something like this in my templates:

<?= $t->_('Please complete your profile!'); ?>

In I18N controller

   public function initialize()
    {
        $this->view->t = $this->getTranslation();
    }

    protected function getTranslation()
    {
        $messages = [];
        $language = strtolower(substr($this->detectLanguage(), 0, 2));
        $translationFile = $this->di->getConfig()->translation->messagesDir . $language . '.php';

        if (!file_exists($translationFile)) { // Is there a translation file for that language
            $language = 'en';
        }

        require $this->di->getConfig()->translation->messagesDir . $language . '.php';
        setcookie('language', $language, time() + 15 * 86400, '/', "", false, false);
        $this->view->selectedLang = $language;

        return new NativeArray(
            [
                'content' => $messages,
            ]
        );
    }

How can i Hook something in here on failing translation?

edited Aug '20

First - maybe you should use try/catch for exceptions. Second - you need rethink what you exactly want to do. If you have many languages then it's normal situation that sometimes you have some differences between data sets in languages files - but, you probably always use one language as basic/default. If you need compare with another array/language you can use array_diff and you can also use array_merge for putting keys from default language if you haven't them in selected language. Instead of putting full phrase in template like <?= $t->_('Please complete your profile!'); ?> you can use flat or multidimensional array with simple keys 'profile_alert'=>'Please complete your profile!''. Then you can put all uniqe keys to for example database and it should be easiest way to make some translation system with DB... but, again - it's depend. How you would like add new keys?



260

First thanks for your reply. Regarding full phrase vs simpler key you are right it was just a sample. Also with translations i can check with other technique and use the array functions

My main issue is to become notified if a key is missing. Its a big project with a lot of keys written in the templates and now I need a way to find the missing ones without going manually through all files.

I use a google sheet with a key column and for each language a column. And I use a manually started jobscript that replaces the translation csv's used in production. So i do not have differences in the key base and I can search for empty translations. Sometimes the translators put some weird things in the google sheet and the import gets broken. Thats the usecase where i want to get notified about missing translations.

What do you mean with I should use a try catch block. I case of missing translations I18N returns the default langua and in case of missing keys the phrase/key without any error/warning/exception. If there would be any I would try to catch it and I wouldn't have to ask.

So again my question is there a way to hook somewhere on I18N or is overwriting the class the only way here. I would prefer to stay with the fast Phalcon way instead of something handwritten in PHP.

Hmm database could be a good idea. I could write a stored procedure triggering something on missing attempts but I would prefer not to have such kind of logic in the storage and it could slow down the db...

Pls look at : https://github.com/phalcon/incubator/tree/master/Library/Phalcon/Translate/Adapter. You can do some checking with DB, or you really can use native php functions - it's enough, to checking differences between two arrays.