public function testParsePlural()
 {
     $path = CakePlugin::path('Translations') . 'Test/Files/plural.po';
     $result = PoParser::parse($path);
     $expected = array('count' => 3, 'translations' => array(array('locale' => 'en', 'domain' => 'plural', 'category' => 'LC_MESSAGES', 'key' => '%d post', 'value' => '1 post'), array('locale' => 'en', 'domain' => 'plural', 'category' => 'LC_MESSAGES', 'key' => '%d posts', 'value' => '1 post', 'single_key' => '%d post', 'plural_case' => 0), array('locale' => 'en', 'domain' => 'plural', 'category' => 'LC_MESSAGES', 'key' => '%d posts', 'value' => '%d many posts', 'single_key' => '%d post', 'plural_case' => 1)), 'settings' => array('domain' => 'plural'));
     $this->assertSame($expected, $result);
 }
Beispiel #2
0
 /**
  * Reads and parses a file
  *
  * @param string $filepath
  * @param array $options
  * @throws \Exception.
  * @return array. List of entries found in string po formatted
  */
 public static function parseFile($filepath, $options = array())
 {
     $parser = new PoParser(new FileHandler($filepath), $options);
     $parser->parse();
     return $parser;
 }
Beispiel #3
0
 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
 }
Beispiel #4
0
 public static function parse($file, $defaults = array())
 {
     $return = parent::parse($file, $defaults);
     $return['settings']['overwrite'] = false;
     return $return;
 }
 /**
  *  Test for reading previous unstranslated strings
  */
 public function testPreviousUnstranslated()
 {
     $parser = PoParser::parseFile(__DIR__ . '/pofiles/previous_unstranslated.po');
     $entries = $parser->getEntries();
     $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);
 }