/** * All known translation functions are covered. */ public function testAllKnownTranslationFunctionsAreCovered() { $extractor = new WordPressExtractor(); $translations = new Translations(); $translations->setDomain('test'); $translations = $extractor->fromDirectory($this->getResourcesPath() . 'commonSources', $translations); // file_put_contents($this->getResourcesPath() . 'commonSources.php', var_export($arrayCopy, true)); $poContent = $translations->toPoString(); // strip base path for better comparison $poContent = str_replace($this->getResourcesPath(), '', $poContent); $this->assertContains('"translate"', $poContent); $this->assertContains('"translate_with_gettext_context"', $poContent); $this->assertContains('"__"', $poContent); $this->assertContains('"_x"', $poContent); $this->assertContains('"_e"', $poContent); $this->assertContains('"_ex"', $poContent); $this->assertContains('"esc_attr__"', $poContent); $this->assertContains('"esc_attr_e"', $poContent); $this->assertContains('"esc_attr_x"', $poContent); $this->assertContains('"esc_html__"', $poContent); $this->assertContains('"esc_html_e"', $poContent); $this->assertContains('"esc_html_x"', $poContent); $this->assertContains('"_n-single"', $poContent); $this->assertContains('"_n-plural"', $poContent); $this->assertContains('"_nx-context"', $poContent); $this->assertContains('"_nx-single"', $poContent); $this->assertContains('"_nx-plural"', $poContent); $this->assertContains('"_n_noop-singular"', $poContent); $this->assertContains('"_n_noop-plural"', $poContent); $this->assertContains('"_nx_noop-context"', $poContent); $this->assertContains('"_nx_noop-singular"', $poContent); $this->assertContains('"_nx_noop-plural"', $poContent); }
/** * Handle an array of translations and append to the Translations instance. * * @param array $content * @param Translations $translations */ public static function handleArray(array $content, Translations $translations) { $content = current($content); $translations_info = isset($content['']) ? $content[''] : null; unset($content['']); if (isset($translations_info['domain'])) { $translations->setDomain($translations_info['domain']); } foreach ($content as $key => $message) { static::insertTranslation($translations, $key, $message); } }
/** * Handle an array of translations and append to the Translations instance * * @param array $content * @param Translations $translations */ public static function handleArray(array $content, Translations $translations) { $content = $content['messages']; $translations_info = isset($content['']) ? $content[''] : null; unset($content['']); if (isset($translations_info['domain'])) { $translations->setDomain($translations_info['domain']); } $context_glue = '\\u0004'; foreach ($content as $key => $message) { $key = explode($context_glue, $key); $context = isset($key[1]) ? array_shift($key) : ''; $original = array_shift($key); $plural = array_shift($message); $translation = array_shift($message); $plural_translation = array_shift($message); $entry = $translations->insert($context, $original, $plural); $entry->setTranslation($translation); $entry->setPluralTranslation($plural_translation); } }
/** * Handle an array of translations and append to the Translations instance. * * @param array $content * @param Translations $translations */ public static function extract(array $content, Translations $translations) { $messages = current($content); $headers = isset($messages['']) ? $messages[''] : null; unset($messages['']); if (!empty($headers['domain'])) { $translations->setDomain($headers['domain']); } if (!empty($headers['lang'])) { $translations->setLanguage($headers['lang']); } if (!empty($headers['plural-forms'])) { $translations->setHeader(Translations::HEADER_PLURAL, $headers['plural-forms']); } $context_glue = '\\u0004'; foreach ($messages as $key => $translation) { $key = explode($context_glue, $key); $context = isset($key[1]) ? array_shift($key) : ''; $translations->insert($context, array_shift($key))->setTranslation(array_shift($translation))->setPluralTranslations($translation); } }
/** * Extract the entries from a multidimensional array. * * @param array $messages * @param Translations $translations */ private static function fromArray(array $messages, Translations $translations) { if (!empty($messages['domain'])) { $translations->setDomain($messages['domain']); } if (!empty($messages['plural-forms'])) { $translations->setHeader(Translations::HEADER_PLURAL, $messages['plural-forms']); } foreach ($messages['messages'] as $context => $contextTranslations) { foreach ($contextTranslations as $original => $value) { if ($context === '' && $original === '') { self::extractHeaders(is_array($value) ? array_shift($value) : $value, $translations); continue; } $translation = $translations->insert($context, $original); if (is_array($value)) { $translation->setTranslation(array_shift($value)); $translation->setPluralTranslations($value); } else { $translation->setTranslation($value); } } } }