poedit workaround for dynamic gettext
Asked Answered
V

4

7

I have started using gettext for translating text and messages i send to user. I am using poedit as an editor, but i am struggling with dynamic messages.

For example i have things like the login where i have a variable that tells the type of error.

$this->translate('page-error-' . $error);

When i auto update from poedit this gets read like "page-error-". What i do is have a file where i place dummy calls to the translate method with all the possible keys to have them added in my poedit when auto updating.

I don't particularly like this situation. How do you guys do it.

Thanks for your ideas

Vietcong answered 16/5, 2009 at 18:36 Comment(1)
I could not understand the problem. can you describe it more clear?Mcgurn
A
5

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.

Aman answered 17/5, 2009 at 12:17 Comment(1)
how can i add keys manually ? i wasnt able to find a way. Also are those manuallly keys removed when doing a sync with sources ?Vietcong
B
2

Are you trying something like

$this->translate(sprintf('page-error-%s', $error));
Blocker answered 17/5, 2009 at 11:44 Comment(1)
how i make the dinamic keys is not the problem, the problem is how you add them in poedit.Vietcong
R
0

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.

Remotion answered 13/6, 2012 at 8:55 Comment(0)
R
0

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/

Revitalize answered 8/12, 2017 at 14:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.