How can I find out which Language was used by Translator in ZF2?

a 夏天 提交于 2019-12-13 04:47:17

问题


In my ZF2 application I want to retrieve the language that was used to translate the page. I use the ZendSkeletonApplication as basis. In the Module.php I set the Locale like this:

public function onBootstrap($e){
  $translator = $e->getApplication()->getServiceManager()->get('translator');
  $translator->setLocale(
      Locale::acceptFromHttp($_SERVER['HTTP_ACCEPT_LANGUAGE'])
    )
    ->setFallbackLocale('en_US');
}

I can retrieve the locale and the fallbackLocale from the Zend\I18n\Translator\Translator, but then I still don't know which one was used for the translation. Since there is also no way (as far as I didn't found one) to find out which languages where actually loaded into the translator. I can't even test myself if a translation exists for the locale.

Maybe I'm missing something here. I hope someone can point that out.


回答1:


One option may be retrieving cache object and checking for the existence of the primary locale by recreating the cache key. Looking at the loadMessages() method it seems the cache result would contain the list of available messages.

Otherwise it seems the translate functions simply try the first locale then the fallback.




回答2:


I decided to take the most simple approach. I translate the text of the language too.



来源:https://stackoverflow.com/questions/12567869/how-can-i-find-out-which-language-was-used-by-translator-in-zf2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!