Advertisement
booradleys

custom I18n\Translator\Loader\PhpIni

Jul 11th, 2012
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. I want to translate my ini files.
  2. I'm using a custom I18n\Translator\Loader\PhpIni class but i need to instanciate this class with a parameter "Site" into its constructor to get the correct 'section' of ini file.
  3.  
  4. Here is my config file:
  5.  
  6. 'translator' => array(
  7.         'locale' => 'fr_FR',
  8.         'translation_patterns' => array(
  9.                 array(
  10.                         'type' => 'Mix\I18n\Translator\Loader\PhpIni',
  11.                         'base_dir' => __DIR__ . '/../language',
  12.                         'pattern' => 'common-%s.ini',
  13.                         'text_domain' => 'application'
  14.                 )
  15.         ),
  16. ),
  17. 'service_manager' => array(
  18.     'factories' => array(
  19.         'translator'                        =>  'Zend\I18n\Translator\TranslatorServiceFactory',
  20.         'Mix\I18n\Translator\Loader\PhpIni' => function ($serviceManager) {
  21.             $site = $serviceManager->get('Mix\Site');
  22.             $service = new Mix\I18n\Translator\Loader\PhpIni($site);
  23.             return $service;
  24.         },
  25.     ),
  26. ),
  27. But i got the following error :
  28.  
  29. Catchable fatal error: Argument 1 passed to Mix\I18n\Translator\Loader\PhpIni::__construct()
  30. must be an instance of Mix\Site, none given, called in vendor\zendframework\zendframework\library\Zend\ServiceManager\AbstractPluginManager.php on line 177
  31. and defined in library\Mix\I18n\Translator\Loader\PhpIni.php on line 13
  32.  
  33.  
  34. Here is my PhpIni class:
  35.  
  36.  
  37. class PhpIni implements LoaderInterface
  38. {
  39.     private $domain;
  40.     public function __construct(\Mix\Site $site) {
  41.         $this->domain = $site;
  42.     }
  43.     /**
  44.     * load(): defined by LoaderInterface.
  45.     *
  46.     * @see    LoaderInterface::load()
  47.     * @param  string $filename
  48.     * @param  string $locale
  49.     * @return TextDomain|null
  50.     * @throws Exception\InvalidArgumentException
  51.     */
  52.     public function load($filename, $locale) {
  53.         $domainConfig = new \Mix\Config\Ini($filename, strtolower($this->domain->get('name')),false);
  54.        
  55.        if (!is_file($filename) || !is_readable($filename)) {
  56.            throw new Exception\InvalidArgumentException(sprintf(
  57.                'Could not open file %s for reading',
  58.                $filename
  59.            ));
  60.        }
  61.        if(count($domainConfig->toArray())>0) {
  62.             $messages = $domainConfig->toArray();
  63.    
  64.             if (!is_array($messages)) {
  65.                 throw new Exception\InvalidArgumentException(sprintf(
  66.                     'Expected an array, but received %s',
  67.                     gettype($messages)
  68.                 ));
  69.             }
  70.             $textDomain = new TextDomain($messages);
  71.    
  72.             if (array_key_exists('', $textDomain)) {
  73.                 if (isset($textDomain['']['plural_forms'])) {
  74.                     $textDomain->setPluralRule(
  75.                         PluralRule::fromString($textDomain['']['plural_forms'])
  76.                     );
  77.                 }
  78.    
  79.                 unset($textDomain['']);
  80.             }
  81.    
  82.             return $textDomain;
  83.        }
  84.    }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement