Forum Moderators: coopster

Message Too Old, No Replies

translating strings

include('lang_strings.php') - can't redeclare functions

         

NCAnnie

1:09 pm on Mar 19, 2004 (gmt 0)

10+ Year Member



I am using separate language string files called e.g en_strings.php as a way of being able to translate my site according to the selected language. Each file has a couple of translating functions and a bunch of string variables.
I use include('lang_strings.php') on each page after readiang the language (Session) variable. My problem is that I can't use functions on my page, as they can't see the string variables. If I pass the language to a function and try to include the file again, it complains about redeclaring the functions. If I use include_once, it can't see the variables. Without functions, all is OK, but my code is getting very long and unwieldy.

Maybe the whole approach is wrong, but I do want to keep all the strings together for translation purposes later (and adding languages) Suggestions please.

ergophobe

5:38 pm on Mar 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I usually use constants and then templates, so the strings only need to be included at the top of the template and all works, but I you could do what you want by using arrays. Note that if these are large arrays, you may want to pass by reference (see my example below).

file: en_strings.inc.php
<?
if(!isset($strings['search_prompt']) {
$strings['search_prompt'] = "Search";
}
?>

file: site_header.inc.php
<?
include($lang."_strings.php");
?>

file: functions.inc.php
<?
function searchbox(&$strings) {
// pass by reference to reduce overhead

}

file: any_old_page.php

<?
echo "<p>" . $strings['search_prompt'] . "</p>";
echo search_box($strings);
?>