poedit workaround for dynamic gettext

荒凉一梦 提交于 2019-12-01 04:50:17

No -- this is not possible, because the editor (and the gettext tools) are reading your sources, not executing your program. You'll have to keep the dummy calls or add the keys to the translation files yourself.

Are you trying something like

$this->translate(sprintf('page-error-%s', $error));

I came across the same problem.

for example i have

in php

//Gives FORM_HEADER_ADD or FORM_HEADER_EDIT
echo $this->translate('FORM_HEADER_' . strtoupper($this->data)); 

When i sync with poedit it will pick up 'FORM_HEADER_' which is not a identifier i have (generated) in the code.

So i had to fix the problem my giving Poedit full identifiers i solved that by doing the following in php

echo ($this->data === 'add') ? $this->translate('FORM_HEADER_ADD') : $this->translate('FORM_HEADER_EDIT');

UPDATE!

I kept looking into this problem. And i currently gave up importing from source. This is how i continue until i find a better solution

  1. Build application and use as many static identifiers as possible.
  2. Enable logging of untranslated (dynamic) identifiers

    protected function _initMyTranslate(){
    
        $date = new Zend_Date();
        $fileName = sprintf('/../logs/translation_%1$s.log', $date->toString('dd-MM'));
    
        $writer = new Zend_Log_Writer_Stream(APPLICATION_PATH . $fileName);
        $log    = new Zend_Log($writer);
    
        // get the translate resource from the ini file and fire it up
        $resource = $this->getPluginResource('translate');
        $translate = $resource->getTranslate();
    
        // add the log to the translate
        $translate->setOptions(
                array(
                    'log'             => $log,
                    'logUntranslated' => true
                )
            );
    
        // return the translate to get it in the registry and so on
        return $translate;
    }
    
  3. Use Poedit to sync with source and translate the strings that are found.

  4. During testing/debugging phase check the log for untranslated strings.
  5. Add the untranslated strings to the .po file.

    msgid "IDENTIFIER"
    msgstr "TRANSLATION STRING"
    
  6. Open po file and save it to create the mo file (DO NOT SYNC WITH SOURCE OR ALL IS LOST).

UPDATE 2.

I now use a seperate file for my manual identifiers , by using a text editor (gedit/notepad). Now i have two files:

  1. Auto generated by poedit called <language>.po
  2. manually editted file called <language>_manual.po

i configured my translate in application.ini to scan for all files in the language directory

resources.translate.adapter = gettext
resources.translate.content = APPLICATION_PATH "/../library/languages/"
resources.translate.locale = auto ;use en to force english or nl for dutch..etc
resources.translate.scan = directory
resources.translate.options.disableNotices = false

if you want to translate into another language in poedit do file -> new catalog from POT file, and start translating your manually added strings.

If you have a finite number of errors, you can add some dummy code inside a if (false) condition, whose sole purpose is to have PoEdit pick up the translations.

For example:

if (false) {
  _('role_visitor');
  _('role_hiker_reader');
  _('role_hiker');
  _('role_translator');
  _('role_proofreader');
  _('role_moderator');
  _('role_moderator_2');
  _('role_moderator_3');
  _('role_admin');
}

You can then translate with:

$translated_role = _('role_' . $role);

Credits to: http://eng.marksw.com/2012/12/05/how-to-expose-dynamic-translatable-text-to-translation-tools-like-poedit/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!