function convert_nls_file($section, $language, $data, $plugin = false)
{
    $import_dir = './import/';
    // clean file input
    $data = str_replace('define(', '', $data);
    $data = str_replace(');', '', $data);
    // singe quotes to double quotes (maybe not so good ...)
    $data = str_replace('\'', '"', $data);
    $data = str_replace(array("\r\n", "\r", "\n", "\t"), ' ', $data);
    $data = preg_replace("/[ ]{2,}/", " ", $data);
    $data = trim($data);
    //$data = str_replace(array("{ ", " }"), array("{", "}"), $data);
    // typo stuff
    $data = str_replace(array("} ,", "{ root:", "\", \"", "{ ", " }", "src."), array("},", "{root:", "\",\"", "{", "}", ""), $data);
    // cleanup master file
    //preg_match('/^{ "root": \{(.*)\},(.*)/', $data, $found);
    //preg_match('/^{ \'root\': \{(.*)\},(.*)/', $data, $found);
    preg_match('/^{[\'|"]{1}root[\'|"]{1}: \\{(.*)\\},(.*)/', $data, $found);
    //print_r($found);
    if (count($found) < 3) {
        preg_match('/^\\{root: \\{(.*)\\},(.*)/', $data, $found);
    }
    if (count($found) == 3) {
        echo "\n is master file \n";
        $data = '{' . trim($found[1]) . '}';
    }
    /*
    echo "\n data found \n";
    //print_r($found);
    //echo "\n\n";
    
    
    echo "\n data to parse \n";
    print_r($data);
    echo "\n\n";
    */
    $parsed = array();
    //parse_jsobj($data, $parsed);
    try {
        echo 'try ...';
        // @hack
        $data = str_replace(array("\\\\"), "*\\///*", $data);
        $data = str_replace(array("\\"), "*\\//*", $data);
        // @hack end
        parse_jsobj($data, $parsed);
    } catch (Exception $e) {
        echo 'Exception: ', $e->getMessage(), "\n";
        //echo "\n data to parse \n";
        //print_r($data);
        //echo "\n\n";
    }
    echo 'parsing ok ...';
    //print_r($parsed);
    $out = "<?php\n\n";
    foreach ($parsed as $key => $value) {
        //echo $key.' -- '.$value."\n";
        $out .= "\$lang['{$key}'] = '{$value}';\n";
    }
    $out .= "\n?>";
    $path = $import_dir . $language . '/';
    if (!is_dir($path)) {
        mkdir($path);
    }
    $path .= $section . '.php';
    //echo $path."\n";
    echo "\n write data to path: {$path} \n";
    //echo $out;
    //echo "\n\n";
    file_put_contents($path, $out);
    // generate old file format
    // /*
    if (empty($plugin)) {
        $old = 'aloha';
    } else {
        // @todo $plugin_path_name with array replacement (camelcase some times)
        //$old = 'com.gentics.aloha.plugins.'.$plugin_path_name;
        $old = 'com.gentics.aloha.plugins.' . ucfirst($plugin);
    }
    $path = $import_dir . $language . '/' . $old . '.php';
    echo "\nwrite data to old path: {$path} \n";
    file_put_contents($path, $out);
    // */
}
Exemple #2
0
function parse_jsdata($str, $term = "}")
{
    $str = trim($str);
    if (is_numeric($str[0] . "0")) {
        /* a number (int or float) */
        $newpos = comma_or_term_pos($str, $term);
        $num = trim(substr($str, 0, $newpos));
        $str = substr($str, $newpos + 1);
        /* discard num and comma */
        if (!is_numeric($num)) {
            throw new JsParserException('OOPSIE while parsing number: "' . $num . '"');
        }
        return array(trim($str), $num + 0);
    } else {
        if ($str[0] == '"' || $str[0] == "'") {
            /* string */
            $q = $str[0];
            $offset = 1;
            do {
                $pos = strpos($str, $q, $offset);
                $offset = $pos;
            } while ($str[$pos - 1] == '\\');
            /* find un-escaped quote */
            $data = substr($str, 1, $pos - 1);
            $str = substr($str, $pos);
            $pos = comma_or_term_pos($str, $term);
            $str = substr($str, $pos + 1);
            return array(trim($str), $data);
        } else {
            if ($str[0] == '{') {
                /* dict */
                $data = array();
                $str = parse_jsobj($str, $data);
                return array($str, $data);
            } else {
                if ($str[0] == '[') {
                    /* array */
                    $arr = array();
                    $str = substr($str, 1);
                    while (strlen($str) && $str[0] != $term && $str[0] != ',') {
                        $val = null;
                        list($str, $val) = parse_jsdata($str, ']');
                        $arr[] = $val;
                        $str = trim($str);
                    }
                    $str = trim(substr($str, 1));
                    return array($str, $arr);
                } else {
                    if (stripos($str, 'true') === 0) {
                        /* true */
                        $pos = comma_or_term_pos($str, $term);
                        $str = substr($str, $pos + 1);
                        /* discard terminator */
                        return array(trim($str), true);
                    } else {
                        if (stripos($str, 'false') === 0) {
                            /* false */
                            $pos = comma_or_term_pos($str, $term);
                            $str = substr($str, $pos + 1);
                            /* discard terminator */
                            return array(trim($str), false);
                        } else {
                            if (stripos($str, 'null') === 0) {
                                /* null */
                                $pos = comma_or_term_pos($str, $term);
                                $str = substr($str, $pos + 1);
                                /* discard terminator */
                                return array(trim($str), null);
                            } else {
                                if (strpos($str, 'undefined') === 0) {
                                    /* null */
                                    $pos = comma_or_term_pos($str, $term);
                                    $str = substr($str, $pos + 1);
                                    /* discard terminator */
                                    return array(trim($str), null);
                                } else {
                                    throw new JsParserException('Cannot figure out how to parse "' . $str . '" (term is ' . $term . ')');
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}