#JAO NetteTranslator (cc)# Pavel Železný (2bfree), 2012 (www.pavelzelezny.cz)
Nette Framework 2.0 or higher. (PHP 5.3 edition)
Here will be my from scratch rewritten variant of great but old marten-cz/NetteTranslator what I used before.
Now here is just Gettex library to manipulate with gettext .po and .mo files.
$dictionary = new GettextDictionary();
$dictionary->loadDictionary('./dictionary.po');
printf("<h2>Files:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->files,true)));
printf("<h2>Headers:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->getHeaders(),true)));
printf("<h2>Translations:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->getTranslations(),true)));
$dictionary = new GettextDictionary('./dictionary.po');
printf("<h2>Files:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->files,true)));
printf("<h2>Headers:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->getHeaders(),true)));
printf("<h2>Translations:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->getTranslations(),true)));
$dictionary = new GettextDictionary();
$dictionary->loadDictionary('./dictionary.mo');
printf("<h2>Files:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->files,true)));
printf("<h2>Headers:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->getHeaders(),true)));
printf("<h2>Translations:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->getTranslations(),true)));
$dictionary = new GettextDictionary('./dictionary.mo');
printf("<h2>Files:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->files,true)));
printf("<h2>Headers:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->getHeaders(),true)));
printf("<h2>Translations:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->getTranslations(),true)));
$dictionary = new GettextDictionary();
// Setting optional but basic headers.
$dictionary->setHeader('Project-Id-Version','Test dictionary 1.0');
$dictionary->setHeader('Language-Team','Elite translator monkeys <[email protected]>');
$dictionary->setHeader('Plural-Forms','nplurals=3; plural=(n==1)? 0 : (n>=2 && n<=4)? 1 : 2;');
$dictionary->setHeader('Last-Translator','JAO NetteTranslator <[email protected]>');
$dictionary->setHeader('X-Poedit-Language','Czech');
$dictionary->setHeader('X-Poedit-Country','Czech Republic');
// Set simple untranslated string for translator team
$dictionary->addOriginal('Translate this please');
// Set simple translation without plurals
$dictionary->addOriginal('Translation team')->setTranslation('Překladatelský team');
// Set translation with plurals (most complicated way)
$dictionary->addOriginal('%s monkey')->setPlural('%s monkeys')->setTranslation('%s opice',0)->setTranslation('%s opičky',1)->setTranslation('%s opiček',2);
// Set Translation with plurals easier
$dictionary->addOriginal(array('%s dog','%s dogs'))->setTranslations(array('%s pes','%s psi','%s psů'));
// Set same translation with another context
// Many apps have problems with context or multiple same untranslated strings
$dictionary->addOriginal('New','New man')->setTranslation('Nový');
$dictionary->addOriginal('New','New woman')->setTranslation('Nová');
// Easiest way to define translation
$dictionary->addOriginal('Cat','',NULL,'Kočka'); //simple
$dictionary->addOriginal('Cat','Beautifull woman',NULL,'Kočička'); // another context
$dictionary->addOriginal(array('%s cat','%s cats'),'',NULL,array('%s kočka','%s kočky','%s koček')); // with plurals
// There is also support for adding many types of comments
$dictionary->getOriginal('Translate this please')->setComment('comment','translator-comments');
$dictionary->getOriginal('Translate this please')->setComment('extracted-comment','extracted-comments');
$dictionary->getOriginal('Translate this please')->setComment('reference','reference...');
$dictionary->getOriginal('Translate this please')->setComment('flag','flag...');
$dictionary->getOriginal('Translate this please')->setComment('previous-context','previous-context');
$dictionary->getOriginal('Translate this please')->setComment('previous-untranslated-string','previous-untranslated-string');
// Comments can be set by array
$dictionary->getOriginal('Translate this please')->setComments(
array(
'comment' => 'translator-comments',
'extracted-comment' => 'extracted-comments',
'reference' => 'reference...',
'flag' => 'flag...',
'previous-context' => 'previous-context',
'previous-untranslated-string' => 'previous-untranslated-string',
)
);
// Save dictionary
$dictionary->saveDictionary('./dictionary.po');
$dictionary->saveDictionary('./dictionary.mo');
// Optional show content of dictionary
printf("<h2>Files:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->files,true)));
printf("<h2>Headers:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->getHeaders(),true)));
printf("<h2>Translations:</h2><pre>%s</pre>",htmlspecialchars(print_r($dictionary->getTranslations(),true)));
When you need to discuss, write in Nette forum in czech language or on Google+ in english language.