Back to Code repository sumary
The enhancement I propose is to collect all untranslated strings in a second file (for example, if your i18n settings are zh_CN, you will have a new file called CN_untranslated.php in the same directory as your CN.php file. Then you will have to translate and move the strings in this new file to the other one. And then, it’s done !
Here is my physical file’s directories:
\the-apache-document-root\ \the-apache-document-root\my-app\ \the-apache-document-root\my-app\index.php \the-apache-document-root\my-app\messages\ \the-apache-document-root\my-app\messages\en\ \the-apache-document-root\my-app\messages\en\US.php \the-apache-document-root\my-app\messages\en\US_untranslated.php // this one is created automaticaly \the-apache-document-root\my-app\messages\fr\ // 'cause I'm French ;) \the-apache-document-root\my-app\messages\fr\FR.php \the-apache-document-root\my-app\messages\fr\FR_untranslated.php // this one is created automaticaly \the-apache-document-root\my-app\messages\zh\ // 'cause I'm working with china ;) \the-apache-document-root\my-app\messages\zh\CN.php \the-apache-document-root\my-app\messages\zh\CN_untranslated.php // this one is created automaticaly \the-apache-document-root\my-app\objects\ \the-apache-document-root\my-app\objects\my-app.php // the main class \the-apache-document-root\my-app\objects\masks\ // 'cause I like to have things well sorted \the-apache-document-root\my-app\objects\masks\loginMask.php etc.
The files US.php, US_untranslated.php, FR.php, FR_untranslated.php etc. appearing here contains only MY strings and not the standard p4a strings. The way p4a manage his and your strings should not bother you, so forget it. You just have to maintain those files mentionned herebefore.
So first, you have to create a new function to be used in your code, in place of the original function:
function __2($org,$forced = false) { // Try the 'old' i18n system $dst = __($org); if ($dst !== $org) return $dst; return P4A::singleton()->__2($org,$forced); }
Then, in your application, you can create those two functions:
function checkUntranslatedSystem() { if (!isset($this->lastLanguage) || $this->lastLanguage != $this->i18n->getLanguage() // this is to render imediately language changing || $this->lastCountry != $this->i18n->getCountry()) // same here { $this->untranslatedFileName = P4A_APPLICATION_LOCALES_DIR . "/" . $this->i18n->getLanguage() . "/" . $this->i18n->getCountry() . "_untranslated.php"; if (is_file($this->untranslatedFileName)) { include($this->untranslatedFileName); $this->messageUntranslated = $message; unset($message); } else $this->messageUntranslated = array(); $this->lastLanguage = $this->i18n->getLanguage(); $this->lastCountry = $this->i18n->getCountry(); } } function __2($org,$forced = false) { // Here, I suppose that you create your original strings in en_US. You can change your preferences // by changing the hardcoded value "en" and "US" here if ($forced || $this->i18n->getLanguage() != "en" || $this->i18n->getCountry() != "US") { $this->checkUntranslatedSystem(); if (!isset($this->messageUntranslated[$org])) { // Create a new untranslated entry $this->messageUntranslated[$org] = $org; // Save the file : it would be better for the file to be writable here ... $handle = fopen($this->untranslatedFileName,"w+b"); fwrite($handle,"<?php\n"); fwrite($handle,"$" . "message = array\n"); fwrite($handle,"(\n"); foreach($this->messageUntranslated as $key => $string) fwrite($handle,"\"" . $key . "\" => \"" . $string . "\",\n"); fwrite($handle,")\n"); fwrite($handle,"?>"); fclose($handle); } } return $org; }
You can see a new parameter in functions : forced. This is used to force a translation even if the standard language and country are selected. For example, you should want (I do it ...) to use this stuff to force translation of a string created by concatenating the name of a table with another word (this is just an example !!):
....
$humanNameForTable = __2("DB_TABLE_" . $tableName,true);
....
And in your translation files, you could find, for the table corp for example :
....
"DB_TABLE_corp" = "Corporation table",
....
But I have to say that this little stuff is not very usefull ;)
That’s all folks !!
2007-01-16: Created ... and waiting for comments and improvements ! - pespie
2007-01-31: Few enhancement and explanations added - pespie