Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have found the script. http://www.aota.net/forums/showthread.php?t=24155

My lang.php

<?php
$Lang = array(
'TEXT'      => "baer",
'GRET'      => "hallo",
'FACE'      => "face",
'HAPY'      => "happy",
'TOOL'      => "tool",
);

My edit.php

<?
// edit the values of an array ($Lang["key"] = "value";) in the file below
$array_file = "lang.php";

// if POSTed, save file
if ($_SERVER['REQUEST_METHOD'] == 'POST')
    {
    unset($_POST['submit']);  // remove the submit button from the name/value pairs
    $new_array = "";          // initialize the new array string (file contents)
    $is_post = true;          // set flag for use below

    // add each form key/value pair to the file
    foreach (array_keys($_POST) as $key)
        { $new_array .= "\$Lang[\'$key\'] => \"".trim($_POST[$key])."\",\n"; }

    // write over the original file, and write a backup copy with the date/time
    write_file($array_file, $new_array);
    write_file($array_file . date("_Y-m-d_h-ia"), $new_array);
    }

// write a file
function write_file($filename, $contents)
    {
    if (! ($file = fopen($filename, 'w'))) { die("could not open $filename for writing"); }
    if (! (fwrite($file, $contents))) { die("could not write to $filename"); }
    fclose($file);
    }

// read the array into $Lang[]
$file_lines = file($array_file);
$Lang = array();
foreach ($file_lines as $line)
    {
    list($b4_key, $key, $b4_value, $value) = explode('"', $line);
    if (preg_match("/Lang/", $b4_key))
        { $Lang[$key] = $value; }
    }
?>    
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Language Editor</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h1><?= $array_file ?></h1>
<? if (!isset($is_post)) { ?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
<? } ?>
<table>
<?
// loop through the $Lang array and display the Lang value (if POSTed) or a form element
foreach ($Lang as $key => $value)
    {
    echo "<tr><td>$key</td><td>";
    echo isset($is_post) ? "= \"$value\"": "<input type=\"text\" name=\"$key\" value=\"$value\">";
    echo "</td></tr>\n";
    }
    ?>
</table>
<? if (!isset($is_post)) { ?>
<p><input type="submit" name="submit" value="Save"></p>
</form>
<? } ?>
</body>
</html>

Unfortunately I can not read array and Keys from lang.php.

Script writer, writes: all lines in array.txt did do set $ Bid [] will be lost.

But I want lang.php after the change save as a PHP file that would go?

I want to create a drop-down list and load array with matching keys, change key text and save.

I thank you in advance for your help!

share|improve this question
    
Once again - "I have found the script and it doesn't work"... What have you tried to make it work? What output do you get? What do you want to get? –  David Jashi Jul 11 '13 at 5:46

1 Answer 1

up vote 0 down vote accepted

You shouldn't read lang.php file. You should include it.

Better way:

require_once('lang.php'); // or require_once($array_file);

and remove these lines:

// read the array into $Lang[]
$file_lines = file($array_file);
$Lang = array();
foreach ($file_lines as $line)
    {
    list($b4_key, $key, $b4_value, $value) = explode('"', $line);
    if (preg_match("/Lang/", $b4_key))
        { $Lang[$key] = $value; }
    }

# # # # # # # #

As I understand your file contents only one language, doesn't it? If no, will be a little modifications in the code.

<?
// edit the values of an array ($Lang["key"] = "value";) in the file below
$array_file = "lang.php";

// if POSTed, save file
if (isset($_POST['submit'])) {
    unset($_POST['submit']); // remove the submit button from the name/value pairs
    $is_post = true; // set flag for use below

    // write over the original file, and write a backup copy with the date/time
    write_file($array_file, $new_array);
    write_file($array_file . date("_Y-m-d_h-ia"), $new_array);

    file_put_contents($array_file, serialize(array(
        'timestamp' => time(), // after you can display this value in your preffered format
        'data' => serialize($_POST)
    )));
}

$Lang_content = @unserialize(@file_get_contents($array_file));
if (!array_key_exists('data', $Lang_content)) {
    $Lang_content = array(
        'timestamp' => 0, // or time()
        'data' => serialize(array())
    );
}

$Lang_template = array( // If you want
    'TEXT'      => "baer",
    'GRET'      => "hallo",
    'FACE'      => "face",
    'HAPY'      => "happy",
    'TOOL'      => "tool",
);

$Lang = array_merge(
    $Lang_template,
    unserialize($Lang_content['data'])
);

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Language Editor</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<h1><?= $array_file ?></h1>
<? if (!isset($is_post)) { ?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
    <? } ?>
    <table>
        <?
        // loop through the $Lang array and display the Lang value (if POSTed) or a form element
        foreach ($Lang as $key => $value) {
            echo "<tr><td>$key</td><td>";
            echo isset($is_post) ? "= \"$value\"" : "<input type=\"text\" name=\"$key\" value=\"$value\">";
            echo "</td></tr>\n";
        }
        ?>
    </table>
    <? if (!isset($is_post)) { ?>
    <p><input type="submit" name="submit" value="Save"></p>
</form>
<? } ?>
</body>
</html>
share|improve this answer
    
But as for me the lang.php is not a good idea. Just serialize language arrays and save to some file (not php). After you can read and unserialize and will get the same array. I think that is the better way. –  Vitaly Jul 11 '13 at 5:56
    
Hello Vitaly, Thanks for your tip does it work. But I want to save the lang.php as PHP, that would go? Now, after saving it deletes everything else. :-( –  rafig Jul 11 '13 at 6:04
    
I just read that is very exciting and I would love to do that with the serialize, unserialize. Can you write some code for me and so help me on the jumps? –  rafig Jul 11 '13 at 6:08
    
Read: $Lang = unserialize(@file_get_contents('LANG_FILE')); Write: file_put_contents('LANG_FILE', serialize($Lang)); –  Vitaly Jul 11 '13 at 6:38
    
To improve this you can also use the template Lang array: $Lang_template = array(/* TEMPLATE VALUES */); $Lang = array_merge($Lang_template, unserialize(@file_get_contents('LANG_FILE'))); If the lang file is corrupted (or just not exists) or doesn't have some variable then the Template values will be used. –  Vitaly Jul 11 '13 at 6:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.