Zend_Translate_Adapter, ...] * @var array */ protected static $_translatorCache = []; // usage: // Exapmle 1: Qs_Translate::getPlural('Credit', 3); // Exapmle 2: Qs_Translate::getPlural(array('Query', 'Queries'), 3); public static function getPlural($word, $count) { $count = abs($count); if (is_array($word)) { if ($count > 1 || $count == 0) { if (isset($word[1])) { return $word[1]; } return null; } return $word[0]; } elseif(is_string($word)) { if ($count > 1 || $count == 0) { return $word . 's'; } return $word; } return null; } public static function translate($text, $section = self::SECTION_COMMON) { if (($translator = self::getTranslator($section))) { return $translator->translate($text); } return $text; } public static function plural($singular, $plural, $count, $section = self::SECTION_COMMON) { if (($translator = self::getTranslator($section))) { return $translator->plural($singular, $plural, $count); } return $singular; } public static function getTranslator($section = self::SECTION_COMMON) { if (array_key_exists($section, static::$_translatorCache)) { return static::$_translatorCache[$section]; } if (!Zend_Registry::isRegistered('Zend_Translate')) { static::$_translatorCache[$section] = null; return null; } $translator = Zend_Registry::get('Zend_Translate'); if ($translator instanceof Zend_Translate) { $translator = $translator->getAdapter(); } /** @var $translator Zend_Translate_Adapter */ if (!($translator instanceof Zend_Translate_Adapter)) { throw new Exception('Translator is not instance of "Zend_Translate_Adapter"'); } /** @var \Qs_Translate_Adapter_Gettext $translator */ if ($section === self::SECTION_COMMON) { if ($translator instanceof Qs_Translate_Adapter_Gettext) { $translator->setSection($section); } static::$_translatorCache[$section] = $translator; return $translator; } $file = constant('BASE_PATH') . '/languages/' . CURRENT_LANG. '/' . $section . '.mo'; if (file_exists($file)) { $translator = clone $translator; if ($translator instanceof Qs_Translate_Adapter_Gettext) { $translator->setSection($section); } $translator->addTranslation($file, CURRENT_LANG); static::$_translatorCache[$section] = $translator; return $translator; } if (constant('DEBUG')) { Qs_Debug::log('Translation file not found: ' . $file); } return self::getTranslator(self::SECTION_COMMON); } }