Re: [fw-general] Zend_Translate doesn't cache when addTranslation is called
UPDATE: After a message from Robert Castley (thanks), I tried directory scan instead of filename scan. It seemed to work. Here is an example of what works and what doesn't work for me: - WORKS: ($dir contains folders zh_CN/ zh_TW/ en/ and de/ with translation files inside) $translate = new Zend_Translate('my_custom_adapter', $dir, null, array('scan' => Zend_Translate::LOCALE_DIRECTORY)); print_r($translate->getList()); prints: Array ( [en] => en [zh_TW] => zh_TW [zh_CN] => zh_CN [de] => de ) - DOESN'T WORK: ($dir contains files zh_CN.txt zh_TW.txt en.txt and de.txt) $translate = new Zend_Translate('my_custom_adapter', $dir, null, array('scan' => Zend_Translate::LOCALE_FILENAME)); print_r($translate->getList()); prints: Array ( [en] => en [de] => de [zh] => zh ) I will continue with DIRECTORY, but I would prefer to use FILENAME, but what am I doing wrong that it combines zh_CN and zh_TW into a single locale? Rob -- View this message in context: http://www.nabble.com/Zend_Translate-doesn%27t-cache-when-addTranslation-is-called-tp16539888p16558540.html Sent from the Zend Framework mailing list archive at Nabble.com.
RE: [fw-general] Zend_Translate doesn't cache when addTranslation is called
Hi David, As per your suggestion, I have put a page up on the Wiki: http://framework.zend.com/wiki/x/p6o http://framework.zend.com/wiki/x/p6o> Cheers, - Robert _ From: David Rogers [mailto:[EMAIL PROTECTED] Sent: 08 April 2008 12:49 To: Robert Castley Subject: Re: [fw-general] Zend_Translate doesn't cache when addTranslation is called Is there a snippet board somewhere in the ZF project? It would be invaluable to keep this little code piece around for the community, methinks. David On Apr 8, 2008, at 5:48 AM, Robert Castley wrote: class MyPlugin_Language extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { $locale = new Zend_Locale(); /** * Cache options */ $frontendOptions = array('cache_id_prefix' => 'MyApp', 'lifetime' => 86400); $backendOptions = array('cache_dir' => 'cache'); $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions); Zend_Translate::setCache($cache); $options = array('scan' => Zend_Translate::LOCALE_DIRECTORY); /** * Scan languages dir. For CSV language files. Auto-detect browser * language settings */ $translate = new Zend_Translate('csv', 'languages', 'auto', $options); /** * If browser language setting is not available default to English */ if (!$translate->isAvailable($locale->getLanguage())) { $locale->setLocale('en'); } $translate->setLocale($locale); Zend_Registry::set('Zend_Translate', $translate); } } This email has been scanned for all known viruses by the MessageLabs Email Security Service and the Macro 4 plc internal virus protection system. This email has been scanned for all known viruses by the MessageLabs Email Security Service and the Macro 4 plc internal virus protection system.
RE: [fw-general] Zend_Translate doesn't cache when addTranslation is called
Hi David, As per your suggestion, I have put a page up on the Wiki: http://framework.zend.com/wiki/x/p6o <http://framework.zend.com/wiki/x/p6o> Cheers, - Robert _ From: David Rogers [mailto:[EMAIL PROTECTED] Sent: 08 April 2008 12:49 To: Robert Castley Subject: Re: [fw-general] Zend_Translate doesn't cache when addTranslation is called Is there a snippet board somewhere in the ZF project? It would be invaluable to keep this little code piece around for the community, methinks. David On Apr 8, 2008, at 5:48 AM, Robert Castley wrote: class MyPlugin_Language extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { $locale = new Zend_Locale(); /** * Cache options */ $frontendOptions = array('cache_id_prefix' => 'MyApp', 'lifetime' => 86400); $backendOptions = array('cache_dir' => 'cache'); $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions); Zend_Translate::setCache($cache); $options = array('scan' => Zend_Translate::LOCALE_DIRECTORY); /** * Scan languages dir. For CSV language files. Auto-detect browser * language settings */ $translate = new Zend_Translate('csv', 'languages', 'auto', $options); /** * If browser language setting is not available default to English */ if (!$translate->isAvailable($locale->getLanguage())) { $locale->setLocale('en'); } $translate->setLocale($locale); Zend_Registry::set('Zend_Translate', $translate); } } This email has been scanned for all known viruses by the MessageLabs Email Security Service and the Macro 4 plc internal virus protection system. This email has been scanned for all known viruses by the MessageLabs Email Security Service and the Macro 4 plc internal virus protection system.
RE: [fw-general] Zend_Translate doesn't cache when addTranslation is called
Hi Rob, My code snippet seems to be handling this. I just did a quick test whereby I had the following language dir. Structure: En -> lang.en En_US -> lang.en_US If I set my browser to en-us then it picks up the en_US lang file, if I don't have a language set in my browser it defaults to en. As, stated before don't use the addTranslation method, this didn't work for me either and I had no end of troubles. - Robert -Original Message- From: Rob L [mailto:[EMAIL PROTECTED] Sent: 08 April 2008 13:51 To: fw-general@lists.zend.com Subject: Re: [fw-general] Zend_Translate doesn't cache when addTranslation is called Thomas, Thank you for such a quick and detailed reply to my question. I was originally using the directory search with a constructor like this: $translate = new Zend_Translate('[our custom adapter]', '/path_here', null, array('scan' => Zend_Translate::LOCALE_FILENAME)); I had several translation files in a single directory: en.txt de.txt ... zh_CN.txt zh_TW.txt ... en and de (and any other 2-letter language code) worked fine. But the problem was that zh_CN and zh_TW were both getting saved under the key "zh". When I tried to access the data like this... if($translate->isAvailable('zh_CN')) ...it would be false. So then I changed to call addTranslation once for every language like this... $translate->addTranslation('/path_here/zh_CN.txt', 'zh_CN'); ...then when I call isAvailable('zh_CN') it would be true. Is there a different way to support both zh_CN and zh_TW? Thanks, Rob -- View this message in context: http://www.nabble.com/Zend_Translate-doesn%27t-cache-when-addTranslation-is- called-tp16539888p16558218.html Sent from the Zend Framework mailing list archive at Nabble.com. This email has been scanned for all known viruses by the MessageLabs Email Security Service and the Macro 4 plc internal virus protection system. This email has been scanned for all known viruses by the MessageLabs Email Security Service and the Macro 4 plc internal virus protection system.
Re: [fw-general] Zend_Translate doesn't cache when addTranslation is called
Thomas, Thank you for such a quick and detailed reply to my question. I was originally using the directory search with a constructor like this: $translate = new Zend_Translate('[our custom adapter]', '/path_here', null, array('scan' => Zend_Translate::LOCALE_FILENAME)); I had several translation files in a single directory: en.txt de.txt ... zh_CN.txt zh_TW.txt ... en and de (and any other 2-letter language code) worked fine. But the problem was that zh_CN and zh_TW were both getting saved under the key "zh". When I tried to access the data like this... if($translate->isAvailable('zh_CN')) ...it would be false. So then I changed to call addTranslation once for every language like this... $translate->addTranslation('/path_here/zh_CN.txt', 'zh_CN'); ...then when I call isAvailable('zh_CN') it would be true. Is there a different way to support both zh_CN and zh_TW? Thanks, Rob -- View this message in context: http://www.nabble.com/Zend_Translate-doesn%27t-cache-when-addTranslation-is-called-tp16539888p16558218.html Sent from the Zend Framework mailing list archive at Nabble.com.
RE: [fw-general] Zend_Translate doesn't cache when addTranslation is called
Hi Rob, I have come up with my own plugin that auto loads the translations and caches them. The code may not be perfect but hopefully it gives you an idea: class MyPlugin_Language extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { $locale = new Zend_Locale(); /** * Cache options */ $frontendOptions = array('cache_id_prefix' => 'MyApp', 'lifetime' => 86400); $backendOptions = array('cache_dir' => 'cache'); $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions); Zend_Translate::setCache($cache); $options = array('scan' => Zend_Translate::LOCALE_DIRECTORY); /** * Scan languages dir. For CSV language files. Auto-detect browser * language settings */ $translate = new Zend_Translate('csv', 'languages', 'auto', $options); /** * If browser language setting is not available default to English */ if (!$translate->isAvailable($locale->getLanguage())) { $locale->setLocale('en'); } $translate->setLocale($locale); Zend_Registry::set('Zend_Translate', $translate); } } Cheers, - Robert -Original Message- From: Rob L [mailto:[EMAIL PROTECTED] Sent: 07 April 2008 21:45 To: fw-general@lists.zend.com Subject: [fw-general] Zend_Translate doesn't cache when addTranslation is called I wrote a custom adapter and am trying to load 13 different languages from files. I can't simply read the entire directory because I am using non-standard locale names and need full control. So in my bootstrap I call the Zend_Translate constructor for my first language and then call addTranslation for each of the remaining languages. All of the languages get cached, but only the Zend_Translate constructor actually checks to see if a language has been cached. If you call addTranslation directly, it *always* calls the adapter to load the file. For now as a workaround, I manually check to see if Zend_Cache already has the key 'Zend_Translate_[my adapter here]' cached. But really, Zend should simply check the cache for a hit every time addTranslation is called, right? Rob -- View this message in context: http://www.nabble.com/Zend_Translate-doesn%27t-cache-when-addTranslation-is- called-tp16539888p16539888.html Sent from the Zend Framework mailing list archive at Nabble.com. This email has been scanned for all known viruses by the MessageLabs Email Security Service and the Macro 4 plc internal virus protection system. This email has been scanned for all known viruses by the MessageLabs Email Security Service and the Macro 4 plc internal virus protection system.
Re: [fw-general] Zend_Translate doesn't cache when addTranslation is called
Rob, just use directory or file search. Calling multiple times addTranslation is really depreciated. See into the manual for details. Related to you request. It is called but not at the place you expected it. The data is written in the cache. But when you add a new translation to the adapter using the same language as already existing there is no way to detect if the new file which shall be added is the same or another one. So we MUST overwrite existing data. Think of translations which exist in a module based layout. Each module adds new translations for the same language. When there is no way to add a translation because the language already exists, such a model would not work anymore. Therefor when you do not work with a module based layout you should always work with directory search. Greetings Thomas Weidner, I18N Team Leader http://www.thomasweidner.com - Original Message - From: "Rob L" <[EMAIL PROTECTED]> To: Sent: Monday, April 07, 2008 10:45 PM Subject: [fw-general] Zend_Translate doesn't cache when addTranslation is called I wrote a custom adapter and am trying to load 13 different languages from files. I can't simply read the entire directory because I am using non-standard locale names and need full control. So in my bootstrap I call the Zend_Translate constructor for my first language and then call addTranslation for each of the remaining languages. All of the languages get cached, but only the Zend_Translate constructor actually checks to see if a language has been cached. If you call addTranslation directly, it *always* calls the adapter to load the file. For now as a workaround, I manually check to see if Zend_Cache already has the key 'Zend_Translate_[my adapter here]' cached. But really, Zend should simply check the cache for a hit every time addTranslation is called, right? Rob -- View this message in context: http://www.nabble.com/Zend_Translate-doesn%27t-cache-when-addTranslation-is-called-tp16539888p16539888.html Sent from the Zend Framework mailing list archive at Nabble.com.