1

I have example language file en.php

<?php

$language = array(
    "language_code" => "en",
    "language_name" => "English",


);

These language files replace replace certain things in template files to support multi-language.

Is there a way to add stuff into the array using PHP?
So I do something in the settings, e.g. add a new language value and the PHP adds this to the file en.php and saves it.

I wonder if this is possible because it can be quite complicated I guess. If it is possible, a slight suggestion to do so would be appreciated. I don't have much experience editing files in php.

If it would be better, I could change the language format to XML if that makes it easier.

Thanks

7
  • 1
    and yes XML is defiantly easier and faster and makes more sense to use. Commented Aug 19, 2011 at 12:31
  • Then the question would be How would I edit XML file? Commented Aug 19, 2011 at 12:32
  • and why don't you edit en.php itself? Commented Aug 19, 2011 at 12:34
  • Because there are more than one en.php language files, and I am a lazy administrator. Also, the PHP itself adds new language values when a new module is added to site. Commented Aug 19, 2011 at 12:35
  • @mkram0: then you will go and learn how to edit XML from web articles and books if you have. feel free to come back and ask if you got stack some any where. Commented Aug 19, 2011 at 12:35

2 Answers 2

5

Instead of storing data in php code (or XML), use JSON. Load the data with:

$json = file_get_contents('en.json');
if ($json === false) throw new Exception('Cannot read file!');
$language = json_decode($json, true); // true tells json_decode to export as array ;)

and store it with:

$json = json_encode($language);
if (file_put_contents('en.json', $json) === false) {
  throw new Exception('Can not store language');
}

If you really insist on storing your data as a php program, use the var_export function:

$phpCode = "<?php\n\n" . '$language = ' . var_export($language, true) . ';';
if (file_put_contents('en.php', $phpCode) === false) {
    throw new Exception('Can not store language');
}

There's no need to use XML, since it would be slower and more complicated than JSON. Also, you should generally not store translations in a (relational) database, since the round trip time and repeated query parsing and evaluation are prone to slow down your application.

Sign up to request clarification or add additional context in comments.

1 Comment

this is excelent and far easy. Thanks a lot
0

Why don't you introduce an xml file (or a database) where you store the possible languages and use that instead of the array you are describing?

1 Comment

what about the performance of this way when you load 20 or more values from DB

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.