'); fclose($fh); $real_interface_size = sizeof($interface); // Now run through each language and recompile their lookup arrays. // if (is_dir($langdir.$transdir)) { if ($dh = opendir($langdir.$transdir)) { //If dir exists while (($file = readdir($dh)) !== false) { //read contents if ($file==".." or $file=="."){ } else if (substr($file,-3)==".po"){ //only files ending in .po language_log("-------------Compiling $file------------",0); $language = parseLanguage( $langdir.$transdir.$file, $interface ); if (!$fh = fopen($langdir.$compdir.$file.".inc","w")) { language_log( "ERROR: could not access $langdir $compdir - please check permissions",2 ); exit; } fwrite($fh, "'); fclose($fh); } else { //debug("File $file with unknown extension found in $info_dir"); } } closedir($dh); } else { //debug("$info_dir could not be opened - check permissions?"); } } else { //debug("$info_dir not found or is not a directory"); } //Do the same again, this time for the project specific language files // and instead of creating new compiled files // just add to whatever existing ones // if (is_dir($langdir.$prjtransdir)) { $interface = parseLanguageInterface($langdir.$prjtransdir.$lang_project_default.".po"); if (!$fh = fopen($langdir.$compdir."language_interface.inc","a")) { language_log("ERROR: could not access $langdir $compdir - please check permissions",2); exit; } fwrite($fh, "'); fclose($fh); if ($dh = opendir($langdir.$prjtransdir)) { //If dir exists while (($file = readdir($dh)) !== false) { //read contents if ($file==".." or $file==".") continue; if (substr($file,-3)==".po"){ //only files ending in .po language_log("----------------Compiling project specific $file---------------",0); $language = parseLanguage($langdir.$prjtransdir.$file, $interface); if (!$fh = fopen($langdir.$compdir.$file.".inc","a")) { language_log("ERROR: could not access $langdir $compdir - please check permissions",2); exit; } fwrite($fh, "'); fclose($fh); } else { //debug("File $file with unknown extension found in $info_dir"); } } closedir($dh); } else { //debug("$info_dir could not be opened - check permissions?"); } } else { //debug("$info_dir not found or is not a directory"); } //Lastly we will rename the files to their real names (remove the random prefix): if ($dh = opendir($langdir.$actual_compdir)) { while (($file = readdir($dh)) !== false) { if ($file==".." or $file=="." or (substr($file,0,strlen($randomness))!=$randomness)) continue; if (substr($file,-4)==".inc") { //only files ending in .inc rename ($langdir.$actual_compdir.$file, $langdir.$actual_compdir.substr($file,strlen($randomness))); } } } else { echo "no compile dir?!"; } } /*************************** * Have some of the files changed? **************************/ function languagesNeedsRebuild($langdir,$transdir,$prjtransdir, $compdir){ if (!file_exists($langdir.$compdir)){ //This directory does not exist - try to create one mkdir($langdir.$compdir,0733); return true; //and force an update since we need it } // Uncomment the following to speed up things when you know // that you will manually recompile the languages when updated... // // return false; $last_compile = 0; if (file_exists($langdir."last_compile_timer")) { $last_compile = filemtime($langdir."last_compile_timer"); } //This file gets touched each time a compile finishes if (is_dir($langdir.$transdir)) { if ($dh = opendir($langdir.$transdir)) { while (($file = readdir($dh)) !== false) { if ($file==".." or $file==".") continue; if (substr($file,-3)==".po"){ //only files ending in .po if (filemtime($langdir.$transdir.$file)>$last_compile) { closedir($dh); return true; } } } closedir($dh); } } if (is_dir($langdir.$prjtransdir)) { if ($dh = opendir($langdir.$prjtransdir)) { while (($file = readdir($dh)) !== false) { if ($file==".." or $file==".") continue; if (substr($file,-3)==".po"){ //only files ending in .po if (filemtime($langdir.$prjtransdir.$file)>$last_compile) { closedir($dh); return true; } } } closedir($dh); } } return false; //All checks say that nothing has changed. } /************************** * Parses a gettext .po-file into an indexed PHP array, * checking for inconsistencies if needed. * The $file is parsed and validated against $interface *************************/ function parseLanguage($file, $interface){ if (sizeof($interface)<1){ language_log("No interface defined for 'compileLanguages()'",2); exit; } $translation_file = file($file); $first_entry = true; $current_token_text=""; for ($i = 0;$i=$lang_log_level){ $fh = fopen($lang_log_file,"a"); fwrite($fh, date("Y-m-d H:i:s",time())." ".$msg." ".$message."\n"); fclose($fh); } } if (languagesNeedsRebuild($lang_language_dir, $lang_translations_dir, $lang_prj_translations_dir, $lang_compiled_dir) || get_str("compile_languages",true)){ buildLanguages($lang_language_dir,$lang_translations_dir,$lang_prj_translations_dir, $lang_compiled_dir); } // Define some variables (from constants) that the language files // can use in the translation: $PROJECT = PROJECT; if (!file_exists($lang_language_dir.$lang_compiled_dir."language_interface.inc")){ language_log("Could not load language interface.",2); echo "ERROR: Could not load language interface. This is a fatal error, exiting. "; flush; exit; } require_once($lang_language_dir.$lang_compiled_dir."language_interface.inc"); // Make a list of languages which the user prefers // (by looking at cookies and browser settings) // cookies have highest priority. if (isset($_COOKIE['lang'])){ $language_string = $_COOKIE['lang'].","; } else { $language_string = ''; } if (isset($_SERVER["HTTP_ACCEPT_LANGUAGE"])) { $language_string .= strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]); } // Find out which language to use by iterating through list // The list is comma-separated, so split it into an array of the following type: // Array ( // [0] => da // [1] => en-us;q=0.7 // [2] => en;q=0.3 // ) $client_languages=explode(",",$language_string); // A language is either defined as primary-secondary or primary. // It can also have a quality attribute set, // which orders the languages in a user preferred ordering. // Since this is usally the same order as the array indices // we just ignore this attribute (TODO: don't ignore this attribute) // A missing quality attribute means q=1 // Always include the project default as fallback // require_once( $lang_language_dir.$lang_compiled_dir.$lang_project_default.".po.inc" ); $language_in_use = $lang_project_default; // loop over languages that the client requests // for ($i=sizeof($client_languages)-1;$i>=0;$i--) { if ((strlen($client_languages[$i])>2) && (substr($client_languages[$i],2,1)=="_" || substr($client_languages[$i],2,1)=="-")) { // If this is defined as primary-secondary, represent it as xx_YY // $language = substr( $client_languages[$i],0,2)."_".strtoupper(substr($client_languages[$i],3,2) ); // And also check for the primary language // $language2 = substr($client_languages[$i],0,2); } else { // else just use xx // $language = substr($client_languages[$i],0,2); $language2 = null; } // If we have a translation for the language, include it // $file_name = $lang_language_dir.$lang_compiled_dir.$language.".po.inc"; if (file_exists($file_name)) { require($file_name); $language_in_use = $language; } if ($language2) { $file_name = $lang_language_dir.$lang_compiled_dir.$language2.".po.inc"; if (file_exists($file_name)) { require($file_name); $language_in_use = $language2; } } } // If you include this file, $language_in_use is now set // to the language actually being used ?>