function ajax_mts_translation_panel() { $poparser = new PoParser(); $mts_translations = get_option('mts_translations_' . MTS_THEME_NAME); //$this->options['translations']; $entries = $poparser->read(get_template_directory() . '/lang/default.po'); $i = 0; $page = empty($_POST['page']) ? 1 : (int) $_POST['page']; $search_query = empty($_POST['search']) ? '' : $_POST['search']; $strings_per_page = 20; $strings_tmp = array(); if ($search_query) { foreach ($entries as $string_id => $object) { $message = ''; foreach ($object['msgid'] as $line) { $message .= $line; } $value = empty($mts_translations[$message]) ? '' : $mts_translations[$message]; if (stristr($value, $search_query) !== false || stristr($message, $search_query) !== false) { $strings_tmp[$string_id] = $object; } } $entries = $strings_tmp; } $number = count($entries); $number_translated = 0; $this->mts_translation_pagination($number, $strings_per_page, $page); $form = ''; foreach ($entries as $string_id => $object) { $i++; $message = ''; foreach ($object['msgid'] as $line) { $message .= $line; } if (!empty($mts_translations[$message])) { $number_translated++; } if ($i > ($page - 1) * $strings_per_page && $i <= $page * $strings_per_page) { $reference = implode(' ', $object['reference']); $reference = implode(', ', explode(' ', $reference)); $value = empty($mts_translations[$message]) ? '' : $mts_translations[$message]; $form .= '<div class="translate-string-wrapper">'; // debug //echo '<!-- '.print_r($object,1).' -->'; $form .= '<label for="translate-string-' . $i . '">' . esc_html($message) . ' <span>(' . $reference . ')</span></label>'; //echo '<input type="text" name="'.$this->args['opt_name'].'[translations]['._wp_specialchars( $message, ENT_QUOTES, false, true ).']" id="translate-string-'.$i.'" value="'._wp_specialchars( $value, ENT_QUOTES, false, true ).'">'; $form .= '<textarea id="translate-string-' . $i . '" data-id="' . _wp_specialchars($message, ENT_QUOTES, false, true) . '" class="mts_translate_textarea">'; $form .= esc_textarea($value); $form .= '</textarea>'; $form .= '</div>'; } } echo $form; if ($number == 0) { $percent = 0; } else { $percent = $number_translated / $number * 100; } echo '<div class="translation_info">' . sprintf(__('Translated <span class="translated">%1$d</span> strings out of <span class="total">%2$d</span> <span class="percent">(%3$.2f%%)</span>', 'mythemeshop'), $number_translated, $number, $percent) . '</div>'; $this->mts_translation_pagination($number, $strings_per_page, $page); exit; // required for AJAX in WP }
/** * Test for reading previous unstranslated strings */ public function testPreviousUnstranslated() { $pofile = new PoParser(); $entries = $pofile->read(__DIR__ . '/pofiles/previous_unstranslated.po'); $expected = array('this is a string' => array('msgid' => array('this is a string'), 'msgstr' => array('this is a translation'), 'previous' => array('msgid' => array('this is a previous string'), 'msgstr' => array('this is a previous translation string')))); $this->assertEquals($entries, $expected); }