3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\I18n\Translator\Loader
;
12 use Zend\Config\Reader\Ini
as IniReader
;
13 use Zend\I18n\Exception
;
14 use Zend\I18n\Translator\Plural\Rule
as PluralRule
;
15 use Zend\I18n\Translator\TextDomain
;
18 * PHP INI format loader.
20 class Ini
implements FileLoaderInterface
23 * load(): defined by FileLoaderInterface.
25 * @see FileLoaderInterface::load()
26 * @param string $locale
27 * @param string $filename
28 * @return TextDomain|null
29 * @throws Exception\InvalidArgumentException
31 public function load($locale, $filename)
33 if (!is_file($filename) ||
!is_readable($filename)) {
34 throw new Exception\
InvalidArgumentException(sprintf(
35 'Could not open file %s for reading',
41 $iniReader = new IniReader();
42 $messagesNamespaced = $iniReader->fromFile($filename);
44 $list = $messagesNamespaced;
45 if (isset($messagesNamespaced['translation'])) {
46 $list = $messagesNamespaced['translation'];
49 foreach ($list as $message) {
50 if (!is_array($message) ||
count($message) < 2) {
51 throw new Exception\
InvalidArgumentException(
52 'Each INI row must be an array with message and translation'
55 if (isset($message['message']) && isset($message['translation'])) {
56 $messages[$message['message']] = $message['translation'];
59 $messages[array_shift($message)] = array_shift($message);
62 if (!is_array($messages)) {
63 throw new Exception\
InvalidArgumentException(sprintf(
64 'Expected an array, but received %s',
69 $textDomain = new TextDomain($messages);
71 if (array_key_exists('plural', $messagesNamespaced)
72 && isset($messagesNamespaced['plural']['plural_forms'])
74 $textDomain->setPluralRule(
75 PluralRule
::fromString($messagesNamespaced['plural']['plural_forms'])