Esempio n. 1
0
function translate()
{
    global $phpc_locale_path;
    if (!is_admin()) {
        permission_error(__('Need to be admin'));
        exit;
    }
    $handle = opendir($phpc_locale_path);
    if (!$handle) {
        return soft_error("Error reading locale directory.");
    }
    $output_tag = tag('div', tag('h2', __('Translate')));
    while (($filename = readdir($handle)) !== false) {
        $pathname = "{$phpc_locale_path}/{$filename}";
        if (strncmp($filename, ".", 1) == 0 || !is_dir($pathname)) {
            continue;
        }
        $msgs_path = "{$pathname}/LC_MESSAGES";
        $hash = parse_po_file("{$msgs_path}/messages.po");
        if ($hash === FALSE) {
            print nl2br("Error reading '{$msgs_path}/messages.po', aborted.\n");
        } else {
            $out = "{$msgs_path}/messages.mo";
            write_mo_file($hash, $out);
        }
        $output_tag->add(tag('div', sprintf(__('Translated "%s"'), $filename)));
    }
    closedir($handle);
    return $output_tag;
}
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * @package php-msgfmt
 * @version $Id$
 * @copyright 2007 Matthias Bauer
 * @author Matthias Bauer
 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License 2.1
 * @license http://opensource.org/licenses/apache2.0.php Apache License 2.0
 */
/*
 * This is a command line PHP script to compile gettext PO files into gettext MO files.
 * Output should be equivalent to that of msgfmt --no-hash
 *  
 * Syntax: ./msgfmt.php file.po [-o output.mo]
 */
include dirname(__FILE__) . '/msgfmt-functions.php';
array_shift($argv);
$in = array_shift($argv);
$out = str_replace('.po', '.mo', $in);
if (array_shift($argv) == '-o') {
    $out = array_shift($argv);
}
$hash = parse_po_file($in);
if ($hash === FALSE) {
    print "Error reading '{$in}', aborted.\n";
} else {
    write_mo_file($hash, $out);
}
function ishtar_main()
{
    if (isset($_POST['submit'])) {
        if (function_exists('current_user_can') && !current_user_can('manage_options')) {
            die(__('Cheatin’ uh?'));
        }
    }
    include 'gettext.php';
    wp_enqueue_script('prototype');
    wp_print_scripts();
    /* For testing */
    $hash = parse_po_file(ISHTAR_BASEDIR . '/test_fuzzy.po');
    $full_hash = $hash;
    if (isset($_POST['filter']) && isset($_POST['filterterms']) && !empty($_POST['filterterms'])) {
        $filter_terms = $_POST['filterterms'];
        $temp = $hash;
        $hash = array();
        foreach ($temp as $k => $data) {
            if (stripos($data['msgid'], $filter_terms) !== FALSE || stripos(implode('', $data['msgstr']), $filter_terms) !== FALSE) {
                $hash[$k] = $data;
            }
        }
        //$hash= array_filter($hash, create_function('$a', 'return ishtar_filter($a, \''.addslashes($filter_terms).'\');'));
    }
    if (isset($_POST['finish'])) {
        //echo "<pre>".htmlspecialchars(var_export($_POST, TRUE))."</pre>";
        foreach ($_POST['ishtar'] as $k => $data) {
            // normalize newlines
            $s = $data['msgstr'];
            $s = str_replace(array("\r\n", "\r"), array("\n", "\n"), $s);
            $s = str_replace(array("\\n\n"), array("\\n"), $s);
            $s = stripslashes($s);
            //echo "<pre>before = ".htmlspecialchars(var_export($full_hash[$k], TRUE))."<br>after = ".htmlspecialchars(var_export($data, TRUE))."</pre>";
            $full_hash[$k]['msgstr'] = array($s);
            $hash[$k] = $full_hash[$k];
        }
        if (write_po_file($full_hash, ISHTAR_BASEDIR . '/test_fuzzy.po')) {
            $message = __('Successfully saved file.', ISHTAR_DOMAIN);
            $message_color = '#ddf';
        } else {
            $message = __('Error saving file.', ISHTAR_DOMAIN);
            $message_color = '#fdd';
        }
    }
    $paginate_num = 5;
    $paginate_count = sizeof($hash);
    $paginate_start = isset($_POST['ishtar_ps']) ? intval($_POST['ishtar_ps']) : 0;
    if (isset($_POST['saveprev'])) {
        $paginate_start -= $paginate_num;
    }
    if (isset($_POST['savenext'])) {
        $paginate_start += $paginate_num;
    }
    if (isset($_POST['savego'])) {
        $paginate_start = $paginate_num * intval($_POST['savegopage']);
    }
    $paginate_start = min(max($paginate_start, 0), $paginate_count - 1);
    $paginate_end = min($paginate_start + $paginate_num, $paginate_count);
    $controls = array();
    if ($paginate_start > 0) {
        $controls[] = '<input type="submit" name="saveprev" value="' . sprintf(__('Save and show previous %d', ISHTAR_DOMAIN), $paginate_num) . '">';
    }
    if ($paginate_start + $paginate_num < $paginate_count) {
        $controls[] = '<input type="submit" name="savenext" value="' . sprintf(__('Save and show next %d', ISHTAR_DOMAIN), min($paginate_num, $paginate_end)) . '">';
    }
    if ($paginate_count > $paginate_num) {
        $controls[] = __('Save and go to page:', ISHTAR_DOMAIN) . ' <input type="text" name="savegopage" size="3" value="' . floor($paginate_start / $paginate_num) . '"><input type="submit" name="savego" value="' . __('Go', ISHTAR_DOMAIN) . '">';
    }
    $controls[] = __('Filter:', ISHTAR_DOMAIN) . ' <input type="text" name="filterterms" value="' . htmlspecialchars($filter_terms) . '"><input type="submit" name="filter" value="' . __('Filter', ISHTAR_DOMAIN) . '">';
    $controls[] = '<input type="submit" name="finish" value="' . __('Finish', ISHTAR_DOMAIN) . '">';
    ?>
<script type="text/javascript" charset="utf-8">
// Guess m-width of font
var b=document.getElementsByTagName("body")[0];var d=document.createElement("div");var s=document.createElement("span");
d.appendChild(s);s.innerHTML="m";b.appendChild(d);var w=s.offsetWidth;var h=s.offsetHeight;b.removeChild(d);

var Textarea = Class.create();
Textarea.prototype = {
  initialize: function(el){
    this.el = el;
    this.rowLength = el.offsetWidth / w; //parseInt(el.getAttribute('cols'));
    this.lineHeight = h;
    this.rows = '';
    this.delta = this.lineHeight;
    this.el.setStyle({overflow: 'hidden'});
    //this.timer = setInterval(function(){this.checkValue()}.bind(this), 100);
    Event.observe(el, 'keyup', this.checkValue.bind(this));
    setTimeout(this.checkValue.bind(this), 50);
  },
  checkValue: function(){
    this.rowLength = this.el.offsetWidth / w; //parseInt(el.getAttribute('cols'));
    var value = this.el.value.split('\n');
    var rows = 0;
    for (var i=0, j=value.length; i<j; ++i) {
      var tempLength = value[i].length == 0 ? 1 : value[i].length;
      rows += Math.ceil( tempLength / this.rowLength );
    }
    if (this.rows !== rows) {
      this.rows = rows;
      this.resize();
    }
  },
  resize: function(){
    this.el.setStyle({height: this.lineHeight * this.rows + this.delta + 'px'});
  }
}

Event.observe(window, 'load', function(){
  // Init behaviors
  $$('.ishtar textarea').each(function(el){
    // init auto-resizing textarea
    new Textarea(el);
    // change listeners
    Event.observe(el, 'blur', function(){
      var c=el.up('tr');
      if (el.value == '') c.addClassName('ishtar-u'); else c.removeClassName('ishtar-u');
    });
  });
  $$('.ishtar input[type=checkbox]').each(function(el) {
    Event.observe(el, 'click', function(){
      var c=el.up('tr');
      if (el.checked) c.addClassName('ishtar-f'); else c.removeClassName('ishtar-f');
    });
  });
  $$('.ishtar textarea', '.ishtar input').each(function(el) {
    // Key listener
    Event.observe(el, 'keyup', function(e){
      if (!e) var e = window.event;
      var c = (e.keyCode ? e.keyCode : e.which);
      var ch = String.fromCharCode(c).toUpperCase();
      if (e.altKey && ch == 'N') {
        // alt-n pressed -> go to next
        var t = Event.element(e);
        var tr = t.up('tr');
        var r = $$('tr.ishtar-f', 'tr.ishtar-u');
        var ne;
        while (ne = tr.next()) { if (r.include(ne)) break; }
        if (ne) { ne.down('textarea').activate(); Event.stop(e); }
      }
    });
  });
  // Key listener
  Event.observe(document, 'keyup', function(e){
    if (!e) var e = window.event;
    var c = (e.keyCode ? e.keyCode : e.which);
    var ch = String.fromCharCode(c).toUpperCase();
    if (e.altKey && ch == 'N') {
      // alt-n pressed -> go to next
      var r = $$('tr.ishtar-f textarea', 'tr.ishtar-u textarea');
      r[0].activate();
      Event.stop(e);
    }
  });
});
</script>
<style type="text/css">
.ishtar { }
.ishtar table { width: 100%; }
.ishtar tbody textarea { width: 25em; } /* 25 "cols" */
.ishtar .ishtar-t { }
.ishtar .ishtar-f { background-color: #ffd; }
.ishtar .ishtar-u { background-color: #fee !important; }
.ishtar .ishtar-ref { font-size: 70%; }
.ishtar .ishtar-fuz { text-align: center; }
</style>
<?php 
    if ($message) {
        ?>
<div id="message" class="updated fade"><p><strong><?php 
        echo $message;
        ?>
</strong></p></div>
<?php 
    }
    ?>
<div class="wrap ishtar">
<h2><?php 
    _e('Ishtar', ISHTAR_DOMAIN);
    ?>
</h2>
<p><?php 
    _e('Alt-N: Go to <u>n</u>ext untranslated or fuzzy', ISHTAR_DOMAIN);
    ?>
</p>
<p><?php 
    _e(sprintf('Displaying entries %1$d &ndash; %2$d of %3$d.', $paginate_start, $paginate_end, $paginate_count), ISHTAR_DOMAIN);
    ?>
</p>
<form method="POST">
<input type="hidden" name="ishtar_ps" value="<?php 
    echo $paginate_start;
    ?>
">
<table>
<colgroup>
<col width="10%">
<col width="30%">
<col width="50%">
<col width="10%">
</colgroup>
<thead>
<tr><td colspan="4"> <?php 
    echo implode(' ', $controls);
    ?>
 </td></tr>
<tr> <th><?php 
    _e('reference', ISHTAR_DOMAIN);
    ?>
</th> <th><?php 
    _e('msgid', ISHTAR_DOMAIN);
    ?>
</th> <th><?php 
    _e('msgstr', ISHTAR_DOMAIN);
    ?>
</th> <th><?php 
    _e('fuzzy', ISHTAR_DOMAIN);
    ?>
</th> </tr>
</thead>
<tbody>
<?php 
    foreach (array_slice($hash, $paginate_start, $paginate_num) as $data) {
        $i = $data['idx'];
        $c = array();
        if (ishtar_is_fuzzy($data)) {
            $c[] = 'ishtar-f';
        }
        if (ishtar_is_untranslated($data)) {
            $c[] = 'ishtar-u';
        }
        ?>
<tr<?php 
        if (sizeof($c) > 0) {
            echo ' class="' . implode(' ', $c) . '"';
        }
        ?>
> <td><span class="ishtar-ctx"><?php 
        echo ishtar_html_msgctxt($data);
        ?>
</span><br><span class="ishtar-ref"><?php 
        echo ishtar_html_reference($data);
        ?>
</span></td> <td><?php 
        echo ishtar_html_msgid($data);
        ?>
</td> <td><textarea name="ishtar[<?php 
        echo $i;
        ?>
][msgstr]"><?php 
        echo ishtar_html_msgstr($data);
        ?>
</textarea></td> <td class="ishtar-fuz"><?php 
        if (ishtar_is_fuzzy($data)) {
            ?>
<label for="ishtar_<?php 
            echo $i;
            ?>
_fuzzy"><input type="checkbox" name="ishtar[<?php 
            echo $i;
            ?>
][flags]" id="ishtar_<?php 
            echo $i;
            ?>
_fuzzy" value="fuzzy" checked="checked"></label><?php 
        } else {
            ?>
-<?php 
        }
        ?>
</td> </tr>
<?php 
        $i++;
    }
    ?>
</tbody>
<?php 
    if (0) {
        ?>
<tfoot>
<tr><td colspan="4"> <?php 
        echo implode(' ', $controls);
        ?>
 </td></tr>
</tfoot>
<?php 
    }
    ?>
</table>
</form>
</div>
<?php 
}