function test_gp_array_zip() { $this->assertEquals(array(), gp_array_zip()); $this->assertEquals(array(), gp_array_zip(array())); $this->assertEquals(array(), gp_array_zip(array(), array(), array())); $this->assertEquals(array(array('baba')), gp_array_zip(array('baba'))); $this->assertEquals(array(), gp_array_zip(array('baba'), array(), array())); $this->assertEquals(array(array('baba', 'dyado')), gp_array_zip(array('baba'), array('dyado'))); $this->assertEquals(array(array('baba', 'dyado')), gp_array_zip(array('baba', 'boom'), array('dyado'))); $this->assertEquals(array(array(array('baba'), 'dyado')), gp_array_zip(array(array('baba'), 'boom'), array('dyado'))); }
public function warning_tags($original, $translation, $locale) { $tag_pattern = "(<[^>]*>)"; $tag_re = "/{$tag_pattern}/Us"; $original_parts = preg_split($tag_re, $original, -1, PREG_SPLIT_DELIM_CAPTURE); $translation_parts = preg_split($tag_re, $translation, -1, PREG_SPLIT_DELIM_CAPTURE); if (count($original_parts) > count($translation_parts)) { return __('Missing tags from translation.', 'glotpress'); } if (count($original_parts) < count($translation_parts)) { return __('Too many tags in translation.', 'glotpress'); } // We allow certain attributes to be different in translations. $translatable_attributes = array('title', 'aria-label'); $translatable_attr_regex = array(); foreach ($translatable_attributes as $key => $attribute) { // Translations should never need a quote in a translatable attribute. $attr_regex_single = '\\s*' . $attribute . '=\'[^\']+\'\\s*'; $translatable_attr_regex[$key] = '%' . $attr_regex_single . '|' . str_replace("'", '"', $attr_regex_single) . '%'; } $parts_tags = gp_array_zip($original_parts, $translation_parts); if (!empty($parts_tags)) { foreach ($parts_tags as $tags) { list($original_tag, $translation_tag) = $tags; $expected_error_msg = "Expected {$original_tag}, got {$translation_tag}."; $original_is_tag = preg_match("/^{$tag_pattern}\$/", $original_tag); $translation_is_tag = preg_match("/^{$tag_pattern}\$/", $translation_tag); if ($original_is_tag && $translation_is_tag && $original_tag != $translation_tag) { $original_tag = preg_replace($translatable_attr_regex, '', $original_tag); $translation_tag = preg_replace($translatable_attr_regex, '', $translation_tag); if ($original_tag != $translation_tag) { return $expected_error_msg; } } } } return true; }
function warning_tags($original, $translation, $locale) { $tag_pattern = "(<[^>]*>)"; $tag_re = "/{$tag_pattern}/Us"; $original_parts = preg_split($tag_re, $original, -1, PREG_SPLIT_DELIM_CAPTURE); $translation_parts = preg_split($tag_re, $translation, -1, PREG_SPLIT_DELIM_CAPTURE); if (count($original_parts) > count($translation_parts)) { return 'Missing tags from translation.'; } if (count($original_parts) < count($translation_parts)) { return 'Too many tags in translation.'; } foreach (gp_array_zip($original_parts, $translation_parts) as $tags) { list($original_tag, $translation_tag) = $tags; $expected_error_msg = "Expected {$original_tag}, got {$translation_tag}."; $original_is_tag = preg_match("/^{$tag_pattern}\$/", $original_tag); $translation_is_tag = preg_match("/^{$tag_pattern}\$/", $translation_tag); // translations should never need a quote in their title attribute if ($original_is_tag && $translation_is_tag && $original_tag != $translation_tag) { // we allow translations to have a different title tag $title_re_single = '\\s*title=\'[^\']+\'\\s*'; $title_re = '%' . $title_re_single . '|' . str_replace("'", '"', $title_re_single) . '%'; $original_tag = preg_replace($title_re, '', $original_tag); $translation_tag = preg_replace($title_re, '', $translation_tag); if ($original_tag != $translation_tag) { return $expected_error_msg; } } } return true; }
public function google_translate_batch($locale, $strings) { if (!$locale->google_code) { return new WP_Error('google_translate', sprintf("The locale %s isn't supported by Google Translate.", $locale->slug)); } $url = 'https://www.googleapis.com/language/translate/v2?key=' . $this->key . '&source=en&target=' . urlencode($locale->google_code); foreach ($strings as $string) { $url .= '&q=' . urlencode($string); } if (count($strings) == 1) { $url .= '&q='; } $response = wp_remote_get($url); if (is_wp_error($response)) { return $response; } $json = json_decode(wp_remote_retrieve_body($response)); if (!$json) { return new WP_Error('google_translate', 'Error decoding JSON from Google Translate.'); } if (isset($json->error)) { return new WP_Error('google_translate', sprintf('Error auto-translating: %1$s', $json->error->errors[0]->message)); } $translations = array(); if (!is_array($json->data->translations)) { $json->data->translations = array($json->data->translations); } $items = gp_array_zip($strings, $json->data->translations); if (!$items) { return new WP_Error('google_translate', 'Error merging arrays'); } foreach ($items as $item) { list($string, $translation) = $item; $translations[] = $this->google_translate_fix($translation->translatedText); } return $translations; }