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
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
- Build application and use as many static identifiers as possible.
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; }
Use Poedit to sync with source and translate the strings that are found.
- During testing/debugging phase check the log for untranslated strings.
Add the untranslated strings to the .po file.
msgid "IDENTIFIER" msgstr "TRANSLATION STRING"
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:
- Auto generated by poedit called <language>.po
- 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/
来源:https://stackoverflow.com/questions/873017/poedit-workaround-for-dynamic-gettext