1<?php 2/** 3 * Zend Framework (http://framework.zend.com/) 4 * 5 * @link http://github.com/zendframework/zf2 for the canonical source repository 6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 7 * @license http://framework.zend.com/license/new-bsd New BSD License 8 */ 9 10namespace Zend\I18n\Translator\Loader; 11 12use Zend\Config\Reader\Ini as IniReader; 13use Zend\I18n\Exception; 14use Zend\I18n\Translator\Plural\Rule as PluralRule; 15use Zend\I18n\Translator\TextDomain; 16 17/** 18 * PHP INI format loader. 19 */ 20class Ini extends AbstractFileLoader 21{ 22 /** 23 * load(): defined by FileLoaderInterface. 24 * 25 * @see FileLoaderInterface::load() 26 * @param string $locale 27 * @param string $filename 28 * @return TextDomain|null 29 * @throws Exception\InvalidArgumentException 30 */ 31 public function load($locale, $filename) 32 { 33 $resolvedIncludePath = stream_resolve_include_path($filename); 34 $fromIncludePath = ($resolvedIncludePath !== false) ? $resolvedIncludePath : $filename; 35 if (!$fromIncludePath || !is_file($fromIncludePath) || !is_readable($fromIncludePath)) { 36 throw new Exception\InvalidArgumentException(sprintf( 37 'Could not find or open file %s for reading', 38 $filename 39 )); 40 } 41 42 $messages = array(); 43 $iniReader = new IniReader(); 44 $messagesNamespaced = $iniReader->fromFile($fromIncludePath); 45 46 $list = $messagesNamespaced; 47 if (isset($messagesNamespaced['translation'])) { 48 $list = $messagesNamespaced['translation']; 49 } 50 51 foreach ($list as $message) { 52 if (!is_array($message) || count($message) < 2) { 53 throw new Exception\InvalidArgumentException( 54 'Each INI row must be an array with message and translation' 55 ); 56 } 57 if (isset($message['message']) && isset($message['translation'])) { 58 $messages[$message['message']] = $message['translation']; 59 continue; 60 } 61 $messages[array_shift($message)] = array_shift($message); 62 } 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 71 $textDomain = new TextDomain($messages); 72 73 if (array_key_exists('plural', $messagesNamespaced) 74 && isset($messagesNamespaced['plural']['plural_forms']) 75 ) { 76 $textDomain->setPluralRule( 77 PluralRule::fromString($messagesNamespaced['plural']['plural_forms']) 78 ); 79 } 80 81 return $textDomain; 82 } 83} 84