getId().' _ '.$ignoreBackendCulture; if (!isset(self::$instance[$hash])) self::$instance[$hash] = new stLanguageEditor($language, $ignoreBackendCulture); return self::$instance[$hash]; } public function __construct(Language $language, $ignoreBackendCulture = false) { $this->culture = $language->getShortcut(); $this->language = $language; if (!$ignoreBackendCulture) $this->backendCulture = sfContext::getInstance()->getUser()->getCulture(); else $this->backendCulture = null; $this->buildDictionary(true, 'en'); if ($this->culture != 'en') { $this->buildDictionary(); } } /** * Zwraca instacje wersji językowej * * @return Language */ public function getLanguage() { return $this->language; } public function getPhrasesCount() { return $this->phrasesCount; } protected function buildDictionary($baseBuild = false, $culture = null) { $this->phrasesCount = 0; if (null === $culture) { $culture = $this->language->getLanguage(); } foreach ($this->language->getTranslationFiles(false, $culture) as $file) { $fileName = pathinfo($file, PATHINFO_BASENAME); $catalogue = substr($fileName, 0, strpos($fileName, '.')); if (preg_match('/^(st|sm|app)[A-Za-z]*\.' . $culture . '\.xml$/', $fileName)) { $this->catalogues[$catalogue] = $catalogue; $xml = simplexml_load_file($file); foreach ($xml->file->body->{"trans-unit"} as $item) { $index = md5($item->source); if (!isset($this->phrases[$catalogue][$index]['phrase'])) { $this->phrasesCount++; } $this->phrases[$catalogue][$index]['phrase'] = (string)$item->source; $this->phrases[$catalogue][$index]['shop'] = (string)$item->target; if ($this->backendCulture == 'en_US' && isset($this->phrases[$catalogue][$index]['shop'])) { $this->phrases[$catalogue][$index]['backend_phrase'] = $this->phrases[$catalogue][$index]['shop']; } else { $this->phrases[$catalogue][$index]['backend_phrase'] = $this->phrases[$catalogue][$index]['phrase']; } if ($this->culture == 'pl') { $this->phrases[$catalogue][$index]['shop'] = (string)$item->source; } } } elseif (preg_match('/^(st|sm|app)[A-Za-z]*\.user\.' . $culture . '\.xml$/', $fileName)) { $xml = simplexml_load_file($file); foreach ($xml->file->body->{"trans-unit"} as $item) { $index = md5($item->source); if (isset($this->phrases[$catalogue][$index]) && !($this->culture == 'pl' && $culture == 'en')) if ($baseBuild && $this->culture != 'en') $this->phrases[$catalogue][$index]['shop'] = (string)$item->target; else $this->phrases[$catalogue][$index]['user'] = (string)$item->target; } } } } public function getCatalogues($onlyWithCache = false) { if ($onlyWithCache === true) return $this->cacheCatalogues; return $this->catalogues; } /** * Zwraca tablicę fraz * * @param string $catalogue Opcjonalny katalog tłumaczeń * @return array */ public function getPhrases($catalogue = null, $index = null) { if ($catalogue && $index) { return isset($this->phrases[$catalogue][$index]) ? array($catalogue => array($index => $this->phrases[$catalogue][$index])) : array(); } if ($catalogue) { return isset($this->phrases[$catalogue]) ? array($catalogue => $this->phrases[$catalogue]) : array(); } return $this->phrases; } public function search($phrase = null, $userPhraseTranslation = null, $searchCatalogue = null) { $result = array(); if ($phrase && mb_strlen($phrase) <= 2) { $phrase = '^' . $phrase . '$'; } if ($userPhraseTranslation && mb_strlen($userPhraseTranslation) <= 2) { $userPhraseTranslation = '^' . $userPhraseTranslation . '$'; } foreach ($this->phrases as $catalogue => $phrases) { if ($searchCatalogue !== null && $catalogue !== $searchCatalogue) continue; foreach ($phrases as $index => $phrases) { $translation = isset($phrases['user']) ? $phrases['user'] : $phrases['shop']; if ((!$phrase || preg_match('/' . $phrase . '/i', $phrases['phrase']) || preg_match('/' . $phrase . '/i', $phrases['backend_phrase'])) && (!$userPhraseTranslation || preg_match('/' . $userPhraseTranslation . '/i', $translation))) { $result[$catalogue][$index] = $phrases; } } } return $result; } public function getPhraseByIndex($catalogue, $index) { if (isset($this->phrases[$catalogue][$index])) return $this->phrases[$catalogue][$index]; return null; } public static function parseXliffFile($filePath) { $phrases = array(); if (!file_exists($filePath)) return $phrases; $xml = simplexml_load_file($filePath, null, LIBXML_NOCDATA); if (false === $xml) { return false; } foreach ($xml->file->body->{"trans-unit"} as $item) { $attributes = $item->attributes(); list($catalogue,) = explode('_', $attributes['id'], 2); $index = md5((string)$item->source); $phrases[$catalogue][$index]['phrase'] = (string)$item->source; $phrases[$catalogue][$index]['user'] = (string)$item->target; } return $phrases; } }